Elastic Search

Elasticsearch Clear Cache

“Caching is the most basic and powerful feature when you need to improve the speed of data fetching from various sources. Caching will always hold, whether it’s applications reading data from the database or multiple pipelines writing data to a specific source.

Although caching plays a vital role in data retrieval, it can lead to errors if the incorrect result is stored in the cache. This can lead to the applications accessing the data from the cache receiving false results.

Therefore, this tutorial will show you how you can clear the cache from your Elasticsearch cluster using various API endpoints.”

Let’s get to it, shall we?

Elasticsearch Cache Types

Elasticsearch supports three main types of cache:

  1. Node Query Cache
  2. Shard Data Cache
  3. Field Data Cache

Node Query Cache

The Node Query Cache is an LRU cache accessible by all shards on a given node. This cache type stores the results of queries used in the filter context. Elasticsearch will remove the cached results based on the least used values (LRU).

Shard Data Cache

This cache type is scoped on a shard-by-shard basis. Like Node Query Cache, the Shard Data Cache uses the LRU eviction mode. In addition, this cache type stores the results of frequently access queries.

Field Data Cache

Field Data Cache, on the other hand, is used for sorting and aggregation operations. This allows Elasticsearch to perform these operations quickly and save memory.

NOTE: Keep in mind that Elasticsearch configures and manages the caching rules internally. Therefore, you will rarely need to alter caching rules manually.

Elasticsearch Clear Cache API

Like most operations in Elasticsearch, we use an API endpoint to perform cache flush operations.

The request syntax is as shown:

POST /_cache/clear

Before using this API endpoint, ensure you have the manage privileges on the target index, data stream, or alias.

Example 1- Elasticsearch Clear Specific Cache Type

The example below illustrates how to clear a specific cache type. The supported cache types include:

  1. fielddata
  2. fuery
  3. request

For example, to clear fielddata cache, run:

curl -XPOST "http://localhost:9200/_cache/clear?fielddata=true" -H "kbn-xsrf: reporting"

You should get an output as shown:

{
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
}
}

To clear query cache, run:

curl -XPOST "http://localhost:9200/_cache/clear?query=true" -H "kbn-xsrf: reporting"

Resulting output:

{
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
}
}

Finally, to clear the request cache, run the query:

curl -XPOST "http://localhost:9200/_cache/clear?request=true" -H "kbn-xsrf: reporting"

Output:

{
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
}
}

Example 2 – Clear Cache for Specific Index

Instead of clearing the cache for all data streams and indicies using the _cache/clear API, you can specify a specific index you wish to clear, as shown in the syntax below:

POST /<target>/_cache/clear

Where target represents the name of the index you wish to use.

For example, to clear the query cache in the earthquake index, we can run:

curl -XPOST "http://localhost:9200/earthquake/_cache/clear?request=true" -H "kbn-xsrf: reporting"

The resulting output is as shown:

{
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
}
}

Example 3 – Clear Cache for Specific Fields

To remove only the cache for specific fields in a given index, you can use the fields parameter and specify the fields whose cache you wish to clear as comma-separated values.

For example, in the earthquake index, we have fields such as Latitude, Magnitude, Longitude, etc.

To clear the cache of these fields, we can run:

curl -XPOST "http://localhost:9200/earthquake/_cache/clear?fields=Latitude,Magnitude,Longitude" -H "kbn-xsrf: reporting"

The request above should clear the cache for the specified fields and return an example output as shown:

{
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
}
}

Closing

Hooray. In this article, you learn about Elasticsearch cache, various cache types in Elasticsearch, and more. You also discovered how you could clear cache for an Elasticsearch index, clear specific cache types, the clear cache for particular fields, and more.

Stay tuned for more!

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