【发布时间】:2015-03-16 00:25:04
【问题描述】:
我正在使用 ElasticSearch 和 mongoosastic 在我们的 MongoDB 和 ElasticSearch 之间同步数据。
我遇到的问题是,当我对 ElasticSearch 中的数据运行搜索时,它没有返回对象的“_id”属性。很可能是因为我错误地告诉它,但我找不到任何文档。
我告诉 mongoosastic 从 Mongo 同步到 ElasticSearch 的对象的虚拟示例如下所示:
var userSchema = new mongoose.Schema({
name: {type:String,es_indexed:true},
phone: {type:String,es_indexed:true},
settings:{type:[String],es_indexed:true,index:'not_analyzed'}
}, {id: true});
userSchema.plugin(mongoosastic,settings.elasticSearch);
var User = mongoose.model('User', userSchema);
User.createMapping(function(err, mapping){
if(err){
console.log('error creating User mapping (you can safely ignore this)');
console.log(err);
}else{
console.log('User mapping created!');
console.log(mapping);
}
});
当我在 ElasticSearch 上运行 _search 时,我得到具有以下结构的结果
{
"_index": "users",
"_type": "user",
"_id": "54b3ea9619160d0b0080d751",
"_score": 1,
"_source": {
"name": "John Smith",
"phone": "JohnSmith@gmail.com",
"settings": []
}
}
任何想法如何将 mongo 对象的 _id 获取到 _source 对象中?
【问题讨论】:
-
你真的需要
_source吗?"_id": "54b3ea9619160d0b0080d751"不是相关ID吗? -
这是一个旧帖子,但对于想知道的人来说,不需要在
_source对象中有一个id。_id应该是相关的 id。如果不是这样,你的 mongodb 和 elasticsearch 不同步。如果您检查 mongoosastic 源代码的水合作用,他们正在使用这个_id从 mongodb 检索数据。
标签: mongodb elasticsearch mongoose elasticsearch-plugin mongoosastic