Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MikeFE
Last active February 10, 2017 07:15
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 MikeFE/9768069954808644a509b443b6244631 to your computer and use it in GitHub Desktop.
Save MikeFE/9768069954808644a509b443b6244631 to your computer and use it in GitHub Desktop.
""" Dump a set of user-friendly text files outlining all our ElasticSearch type mappings """
import json
type_props = []
def add_prop_and_remove_non_en_i18n(prop):
""" Do some extra logic to remove superfluous i18n fields that aren't 'en' """
if 'i18n' in prop and 'i18n.en' not in prop:
return
type_props.append(prop)
def handle_props(props, prefix=''):
for prop, mapping in props.items():
prop = prefix + prop
if 'properties' in mapping: # nested type, recursively get its properties too..
handle_props(mapping['properties'], prop + '.')
else:
add_prop_and_remove_non_en_i18n(prop)
def handle_type(mapping):
if 'properties' in mapping:
handle_props(mapping['properties'])
if __name__ == '__main__':
# mappings.txt is a file with JSON in it from querying ES for its mappings
# e.g. curl -XGET 'http://localhost:9200/_mapping?pretty=true' > mappings.txt
with open('mappings.txt') as f:
mappings = json.load(f)
for name, mapping in mappings['atom']['mappings'].items():
fh = open('type_mappings/' + name + '.txt', 'w')
fh.write('%s\n-----\n' % name)
handle_type(mapping)
for prop in sorted(type_props):
fh.write(prop + '\n')
fh.close()
type_props = []
@MikeFE
Copy link
Author

MikeFE commented Feb 10, 2017

If the index you're pulling from isn't named "atom" you'll need to edit line 32 to reflect that. Further, this script is assuming a folder called type_mappings is created under the ./ (current working directory) so it can dump the text files there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment