0
votes

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

1

1 Answers

2
votes

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.