mongoose-elasticsearch-xp icon indicating copy to clipboard operation
mongoose-elasticsearch-xp copied to clipboard

Model.insertMany does not index to Elasticsearch

Open jeremythille opened this issue 7 years ago • 2 comments
trafficstars

await Model.insertMany([documents])

This works, all the documents get saved to Mongo, but nothing gets indexed in ES. I believe this is a technical limitation, not a bug, but it is seriously annoying. Instead of this one line, one has to build a for...of loop, saving and indexing the documents one by one. So, for 100 documents, this requires 100 DB calls instead of one...

I haven't tried it, but I suspect the same goes with Model.deleteMany()?

Is there a workaround, or will this be fixed either by you or a future Mongoose version?

jeremythille avatar Jul 19 '18 13:07 jeremythille

I had the same problem some days ago and used this:

await Model.esSynchronize({ /* query to target only inserted documents */ });

That's only one line more and worked like a charm. I added a field to every inserted document, so that I can easily target them with a query statement in esSynchronize.

Here is the relevant part of the docs

Hope that helps.

trading-peter avatar Sep 10 '18 15:09 trading-peter

@pkaske got any advice for the deleteMany() step? I'm guessing esSynchronize is only good for adding documents, not removing them?

MumblesNZ avatar Feb 27 '20 08:02 MumblesNZ