ElasticSearch is designed as an eventual consistent system, meaning that there is a time gap between the moment a document is indexed and when it becomes available in your search results. This is because ElasticSearch puts documents first in an in-memory buffer. At specified time intervals(by default every second), a refresh operation is triggered that copies the in-memory buffer content to a newly created segment, making the data available for search.
You can start playing around with the refresh interval or trigger the refresh operation manually but in general this is not a good idea.
A better alternative is to use the refresh parameter when inserting/updating your document. This parameter brings you into control when changes made by a request become visible for search.
You have 3 possible options:
- false: this is the default value, changes to a document are only visible after the next refresh operation
- true: this forces a refresh in the affected shards
- wait_for: the request will wait until the next refresh operation completes
As a general recommendation, use false whenever possible, use true only for development purposes and wait_for if you really want to wait for the updated data.