2

I am trying to get a list of all of my Elasticsearch indices that have 0 documents.

My approach was to query _cat/indices

curl -GET 'http://localhost:9200/_cat/indices' -d '{
    "query": { "match": { "docs.count": "0" } }
}'

But it does not seem to work as I see indices with more than 0 documents being listed.

Cat indices documentation can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/1.6/cat-indices.html

Any suggestions?

Thank you.

halpsb
  • 1,106
  • 2
  • 18
  • 28
  • Why don't you `grep` for the result instead? – Andrei Stefan Oct 28 '15 at 17:08
  • In fact what I am trying to do is to later remove these indices from my cluster (the ones that have 0 documents). The only characteristic they have in common is their matching number of documents. Would I be able to `grep` on `docs.count` ? – halpsb Oct 28 '15 at 19:36

2 Answers2

1

I feel awk and cat API would be a better choice. You can use the below command to get what you are looking for -

curl -GET 'http://localhost:9200/_cat/indices' 2>/dev/null | awk ' ($6 == 0) {print $3}'
Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77
0

use this bash script |tee to output.txt if you want

eg

sh list.sh | tee output.sh

uncomment xdelete if you want to delete it

#!/bin/bash

# Elasticsearch URL
ES_URL="http://localhost:9200"

api_timeout=5m


# Get a list of all indices
indices=$(curl -s "${ES_URL}/_cat/indices" | awk '{print $3}')

# Loop through each index and check if it has zero documents
for index in $indices; do
  doc_count=$(curl -s "${ES_URL}/${index}/_count" | jq -r '.count')
  if [ "$doc_count" -eq 0 ]; then
    echo "${index}"
    # curl -X DELETE "${ES_URL}/${index}?master_timeout=${api_timeout}"
  fi
done
Maulzey
  • 4,100
  • 5
  • 22
  • 30