In this article, we will dissect the Elasticsearch refresh API and learn how we can perform refresh operations within a cluster.
Let’s explore.
Request Syntax
The following shows the syntax of the request using the refresh API.
GET <target>/_refresh
The refresh API supports both the GET and POST HTTP methods.
NOTE: Keep in mind that Elasticsearch will perform a refresh operation in the background at the interval of 30 seconds.
Example – Refresh a Specific Index
The following example shows how to use the refresh API to reload the changes in the kibana_sample_data_logs index.
The query above should return a message as shown:
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
}
}
Example 2 – Refresh All Indices in the Cluster
We can also perform a refresh on all the data streams and indices in a given cluster by calling the _refresh API without parameters.
The request syntax is as shown:
GET /_refresh
For example,
The API should return the number of refreshed indices and data streams as:
"_shards": {
"total": 14,
"successful": 14,
"failed": 0
}
}
Refresh operations are considered resource intensive, especially on indices with large amounts of data. Hence, it is recommended to use the default periodic refresh in Elasticsearch. However, if you need to ensure the latest refresh, you can use the refresh=wait_for parameter to allow the search request to wait until the periodic refresh.
Conclusion
In this article, you learned how to use the refresh API in elasticsearch to fetch the recent operations on a given index or data stream.