Tuesday, June 12, 2018

ElasticSearch–Wait for index refresh

By default when you index a document in ElasticSearch it only becomes available for search operations after a certain time.  This is because ElasticSearch will refresh shards that have changed only after a certain interval(by default 1 second).

If this is not the behavior you want,  you can control when changes are made visible by setting the refresh parameter to one of the following values:

Empty string or true
Refresh the relevant primary and replica shards (not the whole index) immediately after the operation occurs, so that the updated document appears in search results immediately. This should ONLY be done after careful thought and verification that it does not lead to poor performance, both from an indexing and a search standpoint.
wait_for
Wait for the changes made by the request to be made visible by a refresh before replying. This doesn’t force an immediate refresh, rather, it waits for a refresh to happen. Elasticsearch automatically refreshes shards that have changed every index.refresh_interval which defaults to one second. That setting is dynamic. Calling the Refresh API or setting refresh to true on any of the APIs that support it will also cause a refresh, in turn causing already running requests with refresh=wait_for to return.
false (the default)
Take no refresh related actions. The changes made by this request will be made visible at some point after the request returns.

In general it is recommended to not change this parameter unless you have a very good reason to wait for the change to become visible. In that case prefer the usage of refresh=wait_for over refresh=true

More information: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

No comments: