乐闻世界logo
搜索文章和话题

How to get total index size in Elastic Search

1个答案

1

In Elasticsearch, there are multiple ways to obtain the total index size. Here, I will introduce two commonly used methods:

Method One: Using the _cat API

Elasticsearch provides a convenient API called the _cat API, which helps in viewing and managing various information within the cluster. To obtain the total size of all indices, you can use the _cat/indices API with parameters such as v (verbose mode) and h (to specify output columns). The specific command is as follows:

bash
curl -X GET "localhost:9200/_cat/indices?v&h=index,store.size"

This command lists all indices along with their storage sizes. If you only need the total sum of the storage sizes, you can use the following command:

bash
curl -X GET "localhost:9200/_cat/indices?h=store.size&format=json" | jq '[.[] | .store.size] | add'

Here, the jq tool is used to process the JSON output, summing the sizes of all indices to get the total.

Method Two: Using Cluster Stats API

Another API for obtaining cluster information is _cluster/stats. This API provides detailed statistics about the cluster status, including the total size of indices. The command to use this API is:

bash
curl -X GET "localhost:9200/_cluster/stats?human&pretty"

In the returned JSON, you can view the indices.store.size field, which represents the total storage size of all indices.

Example

Suppose we have an actual running Elasticsearch environment with several indices already stored. We can use either of the above methods to obtain the total index size. For example, the information obtained through the _cat/indices API might resemble:

shell
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open index_1 3a1kM-N4QMOU-9a8EvR4Ig 5 1 1000000 200 2.3gb 2.3gb green open index_2 9a4ki-56N4QMOU-8a9RvT4Ig 5 1 1500000 150 3.4gb 3.4gb

By executing the above command, you can see the sizes of individual indices and then manually or using a script calculate the total.

Conclusion

Using either of the above methods can effectively obtain the total index size in Elasticsearch. The choice depends on the level of detail required and personal preference. In practical work, understanding how to use these basic APIs is crucial as they are fundamental tools for daily management and monitoring of ES clusters.

2024年6月29日 12:07 回复

你的答案