0

I tried to use mongoosastic search but it doesnt work

Job.js (Mongoose schema)

var mongoose = require('mongoose');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;

var JobSchema = Schema({
  title: { type: String, es_indexed:true },
  category: { type: Schema.Types.ObjectId, ref: 'Category', es_indexed:true},
  salary: { type: String, es_indexed:true },
});


JobSchema.plugin(timestamps);
JobSchema.plugin(mongoosastic, {
  hosts: [
    'localhost:9200'
  ]});
module.exports = mongoose.model('Job', JobSchema);

Routes.js

var express = require('express'); 
var app = express();       
var Job = require('../models/job');


Job.createMapping(function(err, mapping) {
  if (err) {
    console.log('error creating mapping (you can safely ignore this)');
    console.log(err);
  } else {
    console.log('mapping created!');
    console.log(mapping);
  }
});


app.post('/api/search/', function(req, res, next) {
  Job.search({query_string: {query: req.body.test}}, function(err, results) {
        if (err) return next(err);
        res.send(results);
    });
});

This is the data sets that already been saved in mongodb

    {
     "id": 12313,
     "title": "Engineer"
    },
    {
     "id": 13123,
     "title": "Doctor"  
    },
    {
     "id": 121243134,
     "title": "Software Engineer"
    }

When I tried to run the search and search like "Engineer" and I keep getting this result.

enter image description here

Updated for Curl

curl -XGET localhost:9200/_mapping

enter image description here

curl -XGET localhost:9200/_search

enter image description here

Jack Moscovi
  • 2,195
  • 7
  • 30
  • 49

1 Answers1

2

Since title is an analyzed String, its value is indexed using the standard analyzer, i.e. in lowercased form, so "Engineer" will be indexed and "engineer"

Try searching for "engineer" in lowercase instead.

UPDATE

Based on our discussion, it seems that the problem is simply that your Elasticsearch is empty. Since you have existing data in MongoDB, you need to make sure to call the synchronize() method on your model in order to index all your MongoDB collection inside Elasticsearch.

Val
  • 207,596
  • 13
  • 358
  • 360
  • Can you update your question with the mapping type that was created using `curl -XGET localhost:9200/_mapping` ? Also if you can ensure that something is at all present in Elasticsearch using `curl -XGET localhost:9200/_search` – Val Nov 24 '15 at 07:54
  • Your screenshots don't show the full thing. It's also better to paste real code than screenshot as it facilitates searching later on. Also it doesn't look like the schema you're showing in your question is the real one being used, since the real one seems to have more fields. – Val Nov 24 '15 at 08:02
  • The reason why I don't want to show everything is because I don't want to complicate things. – Jack Moscovi Nov 24 '15 at 08:04
  • What do you want me to do now? – Jack Moscovi Nov 24 '15 at 08:05
  • I added all the fields. that are needed. – Jack Moscovi Nov 24 '15 at 08:07
  • No worries. I understand. What do you see in the second query for `hits.total`? Also how do you make the request to `/api/search`? – Val Nov 24 '15 at 08:10
  • There you go, if a search for anything (i.e. `curl -XGET localhost:9200/_search`) yields 0 results, it means your ES doesn't contain anything and nothing was indexed from MongoDB. – Val Nov 24 '15 at 08:14
  • and this is the message, after mapping `{ acknowledged: true }` – Jack Moscovi Nov 24 '15 at 08:16
  • Yes, but that doesn't mean data was indexed. Creating a mapping and indexing data are two different operations that can be done at different times. You need to [index data](https://github.com/mongoosastic/mongoosastic#indexing) first. – Val Nov 24 '15 at 08:17
  • I already added `es_indexed: true` if you check my code above – Jack Moscovi Nov 24 '15 at 08:18
  • `es_indexed` only tells that you want to store that specific field in ES, but nothing gets indexed at that time. – Val Nov 24 '15 at 08:19
  • How do I indexed it, since in the documentation, it doesnt show anything. – Jack Moscovi Nov 24 '15 at 08:21
  • See the link I posted in my earlier comment: https://github.com/mongoosastic/mongoosastic#indexing – Val Nov 24 '15 at 08:21
  • this one only shows when saving a document to database and use es-indexed and another one is indexing to elastic search which I already did. – Jack Moscovi Nov 24 '15 at 08:23
  • Your ES seems to be empty though, so something must have happened during your indexing. Maybe is it related to your other question where you had [no Living connections](http://stackoverflow.com/questions/33869013/mongoosastic-error-no-living-connections-message-no-living-connections)? – Val Nov 24 '15 at 08:24
  • I already solved that living connection, I didnt run elasticsearch command. – Jack Moscovi Nov 24 '15 at 08:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96001/discussion-between-val-and-jack-moscovi). – Val Nov 24 '15 at 08:25
  • If you are busy I will get it back to you later. – Jack Moscovi Nov 24 '15 at 08:31
  • I'm in there, waiting for your response :) – Val Nov 24 '15 at 09:05
  • Hey, Im really sorry I was out just now, when are you free? – Jack Moscovi Nov 24 '15 at 12:43
  • I have a new question, if you could answer it would be good http://stackoverflow.com/questions/33913617/how-to-do-instant-search-with-mongoosastic – Jack Moscovi Nov 25 '15 at 10:34
  • Hey guys, it seems like you have got the solution specifically on how to index existing data from mongodb to es then i am wondering what was the real problem since i am here trying to use synchronize to index existing docs but nothing works, Es continue to be empty after synchronize operation get completed? Can anyone help.... – kasongoyo Aug 04 '16 at 09:30