2

我正在使用 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 对象中?

4

1 回答 1

0

问题是 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 映射。

于 2015-01-17T16:29:37.200 回答