Mongoosastic - { [Error: No Living connections] message: 'No Living connections' }

隐身守侯 提交于 2020-01-13 03:50:17

问题


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,

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


回答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



回答2:


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
})



回答3:


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.



来源:https://stackoverflow.com/questions/33869013/mongoosastic-error-no-living-connections-message-no-living-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!