gpt4 book ai didi

mongodb - 未在ElasticSearch中添加Mongo '_id'字段

转载 作者:行者123 更新时间:2023-12-02 23:38:02 25 4
gpt4 key购买 nike

我正在使用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字段:

_id

Each document indexed is associated with an id and a type. The _id field can be used to index just the id, and possible also store it. By default it is not indexed and not stored (thus, not created).

The _id field can be enabled to be indexed, and possibly stored, using the appropriate mapping attributes:


{
"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映射。

关于mongodb - 未在ElasticSearch中添加Mongo '_id'字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28000215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com