Mongo '_id' field not being added in ElasticSearch
The issue is that Elasticsearch does not index or store the id field by default:
_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
The issue will be how to pass the "store": true parameters into the createMapping function of mongoosastic. You may need to play around a bit with the createMapping function, modify the code on your own or try to create the Elasticsearch mapping by hand prior to indexing any data.
There is no build-in way to do it with mongoosastic
.
I always use map
function and spread syntax(...) to join _id
with all other fields:
let results = results.map(r => { return { '_id': r._id, ...r._source} } )
Another option is to to use hydration, but it will be much slower as it will do additional query against MongoDB
.