我正在使用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对象?
最佳答案
问题是默认情况下,Elasticsearch不会索引或存储id字段:
{
"tweet" : {
"_id" : {
"index" : "not_analyzed",
"store" : true
}
}
}
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-id-field.html
问题将是如何将“store”:true参数传递到mongoosastic的createMapping函数中。您可能需要使用createMapping函数进行一些操作,自行修改代码或尝试在索引任何数据之前手动创建Elasticsearch映射。