Elasticearch weird behavior for queries right after insertion

I am writing some integration tests for an application using Elasticsearch, and I am experiencing strange behavior. If I insert a document and immediately ask, I get different results every time. I suspect that although the insert itself will return, the indexing itself does not occur synchronously, and because of this, the query will experiment with the race condition with unpredictable results.

If so: is there a way to synchronize, so when I run my queries, I know that they are ready and successful.

More: I use elasticsearch embedded, and the query is a simple filter. The only strange thing is that I use template files for the document model.

EDIT: I even tried to get the document by ID after insertion, but the queries still return random results (unless I put the "Dream" thread for a few seconds).

+6
source share
1 answer

From the Elasticsearch docs for the index API :

refresh

To update the index immediately after the operation, so that the document appears in the search results immediately, the update parameter can be set to true. Setting this parameter to true should ONLY after a thorough check and verification that this will not lead to poor performance, both with indexing and in terms of search. Please note: receiving a document using the get API is completely real-time.

This is why my queries returned weird results. Since indexing is sometimes not yet complete. Alternatively, you can update not as part of the insertion using the _refresh :

 $ curl -XPOST 'http://localhost:9200/twitter/_refresh' 
+10
source

Source: https://habr.com/ru/post/957366/


All Articles