2015-11-23 49 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」 –