2015-11-23 44 views
4

我试着使用mongoosastic供搜索,但一直收到“没有生活连接”的错误和映射问题Mongoosastic - {[错误:没有生活连接]消息:“没有生活连接”}

下面的代码

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

我不断收到此错误,

enter image description here

任何有使用mongoosastic经验的人都告诉我,我该如何解决这个问题?

+1

可能是愚蠢的问题,而是你的elasticsearch服务运行? –

+0

@EvaldasBuinauskas我需要运行它吗? –

+1

那么,根据我的理解,mongoosastic应该将它索引到Elasticsearch。所以你必须安装它,作为服务运行并配置你的代码来连接它。至少这就是我理解它如何看待文档。 –

回答

0

当添加插件到你的JobSchema模式,你需要通过与如何连接到Elasticsearch第二个参数:

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

您还可以,如果你有一个准备重新使用Elasticsearch客户端对象。

var esClient = new elasticsearch.Client({host: 'localhost:9200'}); 
JobSchema.plugin(mongoosastic, { 
    esClient: esClient 
}) 
+0

@JackMoscovi你有没有碰运气? – Val

0

我认为你必须创建一个客户端,并传递到插件,但使用的IP地址是为我工作。

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

注意:如果你以前没有使用elasticsearch您可能必须重新索引文档(我有,但我可能做错了)

引用这个答案:https://stackoverflow.com/a/30204512/1146562

从这个答案你可以直接传递主机到插件,而不必创建客户端。

1

,我也面临着同样的问题,但我解决了,现在用下面的方法

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

  1. 下载弹性搜索http://www.elastic.co/downloads/elasticsearch
  2. 提取文件夹
  3. 去从CMD或Treminal找到文件夹bin文件夹并在该文件夹内运行弹性搜索脚本
  4. 而在的NodeJS代码默认情况下它会链接到本地​​主机:9200所以没有必要使用 新elasticsearch.Client({主持人:“本地主机:9200”}),但如果你的弹性搜索deloyed一些别的地方,那么你必须使用上面客户端方法
+0

用自制软件安装:“brew install elasticsearch”。打开新标签执行elasticsearch服务器,cmd:“elasticsearch” –