4

Im trying to use mongoosastic for searching purposes, but I keep getting 'No Living connections' error and mapping problem

Here's the code

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


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

});

JobSchema.plugin(mongoosastic);
module.exports = mongoose.model('Job', JobSchema);

routes.js

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('/search', function(req, res, next) {
    Job.search({query_string: {query: req.body.q}}, function(err, results) {
        if (err) return next(err);
        res.send(results);
    });
});

I keep getting this error,

enter image description here

Can anyone with experience in using mongoosastic tell,me how do i fix this problem?

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

3 Answers3

1

When adding the plugin to your JobSchema model, you need to pass a second parameter with how to connect to Elasticsearch:

JobSchema.plugin(mongoosastic, {
  hosts: [
    'localhost:9200'
  ]
});

You can also re-use an Elasticsearch client object if you have one ready.

var esClient = new elasticsearch.Client({host: 'localhost:9200'});
JobSchema.plugin(mongoosastic, {
  esClient: esClient
})
Val
  • 207,596
  • 13
  • 358
  • 360
1

I am also facing same issue but I solved now using below method

Its very simple : you have to install elastic search in your local or anywhere else

  1. Download elastic search http://www.elastic.co/downloads/elasticsearch
  2. Extract folder
  3. go to folder from cmd or Treminal find bin folder and inside that folder run elastic search script
  4. And in nodejs code by default it will link to localhost:9200 so no need to use new elasticsearch.Client({host: 'localhost:9200'}) , but if your elastic search deloyed some where else then you have to use above client method
Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40
  • Install with homebrew: "brew install elasticsearch". Open new tab to execute the elasticsearch server, cmd: "elasticsearch" – Ivor Scott Mar 12 '17 at 03:47
0

I think you have to create a client and pass it into the plugin, but use ip addresses that is what worked for me.

// http://127.0.0.1:9200 == http://localhost:9200

var esClient = new elasticsearch.Client({host: 'http://127.0.0.1:9200'});
JobSchema.plugin(mongoosastic, {
    esClient: esClient
})

Note: if you were not previously using elasticsearch you might have to re-index your documents (I had to, but I may have done something wrong)

Referencing this answer: https://stackoverflow.com/a/30204512/1146562

from that answer you can pass host straight into the plugin and do not have to create the client.

Community
  • 1
  • 1
gmaniac
  • 940
  • 1
  • 17
  • 33