0

我正在使用ElasticSearch和mongoosastic来同步MongoDB和ElasticSearch之间的数据。未在ElasticSearch中添加Mongo'_id'字段

我遇到的问题是,当我对ElasticSearch中的数据执行搜索时,它不返回对象的'_id'属性。很可能是因为我错误地告诉它,但我找不到任何文档。

,我告诉mongoosastic从蒙戈同步到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": "[email protected]", 
      "settings": [] 
     } 
    } 

任何想法如何获得mongo对象的_id到_source对象?

+0

你真的需要'_source'吗?是否“_id”:“54b3ea9619160d0b0080d751”'不是相关的ID? – pickypg

回答

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

问题将是如何通过“存储” :真正的参数进入mongoosastic的createMapping函数。您可能需要使用createMapping函数玩一下,自行修改代码或尝试在索引任何数据之前手动创建Elasticsearch映射。