【问题标题】:Mongo '_id' field not being added in ElasticSearch未在 ElasticSearch 中添加 Mongo '_id' 字段
【发布时间】: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


【解决方案1】:

问题是 Elasticsearch 默认不索引或存储 id 字段:

_id

每个被索引的文档都与一个 id 和一个类型相关联。 _id 字段可用于仅索引 id,也可以存储它。经过 默认它没有被索引并且没有被存储(因此,没有被创建)。

_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 映射。

【讨论】:

    猜你喜欢
    • 2017-12-27
    • 1970-01-01
    • 2017-12-31
    • 2013-06-11
    • 2016-08-14
    • 2014-08-27
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多