jbdemonte / mongoose-elasticsearch-xp

A mongoose plugin that indexes models into Elasticsearch 2 / 5 / 6 and 7
93 stars 34 forks source link

Model.insertMany does not index to Elasticsearch #46

Open jeremythille opened 6 years ago

jeremythille commented 6 years ago

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?

trading-peter commented 5 years ago

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.

MumblesNZ commented 4 years ago

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