Skip to content

Instantly share code, notes, and snippets.

@dtaivpp
Created February 10, 2026 14:19
Show Gist options
  • Select an option

  • Save dtaivpp/6a35bc1439b6451e3198140d45be0055 to your computer and use it in GitHub Desktop.

Select an option

Save dtaivpp/6a35bc1439b6451e3198140d45be0055 to your computer and use it in GitHub Desktop.
Count and avg size of ES segments

This script I've used to help me work out how many shards an index on my cluster has and the average shard size for that index.

Fetch the data

curl -XGET 'localhost:9200/_cat/segments?v&h=index,shard,segment,size,docCount,deleted&pretty' -o index-shards.txt

Calculate the Average Size of Shards

awk '
NR>1 {
  idx=$1
  size=$4

  if (size ~ /gb$/) {
    sub(/gb/, "", size)
    size_gb=size
  } else if (size ~ /mb$/) {
    sub(/mb/, "", size)
    size_gb=size/1024
  } else if (size ~ /kb$/) {
    sub(/kb$/, "", size)
    size_gb = size / (1024 * 1024)
  } else {
    next
  }

  cnt[idx]++
  sum_gb[idx]+=size_gb
}
END {
  for (i in cnt)
    printf "%s\t%d\t%.2fGB\n", i, cnt[i], sum_gb[i]/cnt[i]
}' index-shards.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment