Skip to content

Instantly share code, notes, and snippets.

@saurabh-hirani
Last active August 5, 2018 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabh-hirani/e8cbc96844307a41ff4bc8aa8ebd7459 to your computer and use it in GitHub Desktop.
Save saurabh-hirani/e8cbc96844307a41ff4bc8aa8ebd7459 to your computer and use it in GitHub Desktop.
For an ES host - call the _mappings API and get the type count for each index
import sys
import json
import requests
# The following find function is a minor edit of the function posted here
# https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-python-dictionaries-and-lists
def find(key, value):
for k, v in value.iteritems():
if k == key and not isinstance(v, dict) and not isinstance(v, list):
yield v
elif isinstance(v, dict):
for result in find(key, v):
yield result
elif isinstance(v, list):
for d in v:
for result in find(key, d):
yield result
def get_index_type_count(es_host):
try:
response = requests.get('https://%s/_mapping/' % es_host)
except Exception as ex:
print('Failed to get response - %s' % ex)
sys.exit(1)
indices_mapping_data = response.json()
output = {}
for index, mapping_data in indices_mapping_data.iteritems():
output[index] = len(list(find('type', mapping_data)))
return output
if __name__ == '__main__':
print json.dumps(get_index_type_count(sys.argv[1]), indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment