Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.