Elastic Search

Elasticsearch Refresh

Elasticsearch allows you to refresh one or multiple indices in the cluster using the refresh API. A refresh will enable you to update the index and reload any recent operations performed in the index. For example, if a session removes are add data to a specific index, you can use the refresh API to reload those changes in your sessions.”

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.

POST <target>/_refresh
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.

curl -XPOST "http://localhost:9200/kibana_sample_data_logs/_refresh" -H "kbn-xsrf: reporting"

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:

POST /_refresh
GET /_refresh

For example,

curl -XPOST "http://localhost:9200/_refresh/" -H "kbn-xsrf: reporting"

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list