You can try this, Get Field Mapping API
def unique_preserving_order(sequence):
"""
Preserving Order
:param sequence: object list
:return: new list from the set’s contents
"""
seen = set()
return [x for x in sequence if not (x in seen or seen.add(x))]
get es index fields recursively
def get_fields_recursively(dct, field_types=None):
if dct and 'properties' in dct:
fields = []
for key, ndct in dct.get('properties').items():
if 'properties' in ndct:
for nkey, nd in ndct.items():
if nkey == 'properties':
field = get_fields_recursively(ndct)
if field_types:
for f in field:
prop = ndct.get('properties').get(f)
if prop and prop.get('type') in field_types:
ff = '{0}.{1}'.format(key, f)
# fields.append(f)
fields.append(ff)
else:
# if not key.startswith('@'):
# _fields = field + ['{0}.{1}'.format(key, f) for f in field]
_fields = ['{0}.{1}'.format(key, f) for f in field]
fields.extend(_fields)
continue
continue
if field_types:
if ndct.get('type') in field_types and not key.startswith('@'):
fields.append(key)
else:
if not key.startswith('@'):
fields.append(key)
return fields
else:
return dct
get fields from index mappings, also you can filter fields by types, ex. text fields or numerical fields
def get_mapping_fields(self, field_type=None, index=None, params={}):
"""
:param field_type: es field types, filter fields by type
:param index: elastic index name
:param params: mapping additional params
:return: fields
<https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html>
- http://eshost:9200/_mapping
- http://eshost:9200/_all/_mapping
- http://eshost:9200/index_name/_mapping
"""
_fields = []
_mapping = self.esclient.indices.get_mapping(index=index, params=params)
for idx_mapping in _mapping.values():
mapping = idx_mapping.get('mappings')
if 'system' in mapping:
mapping = mapping.get('system')
else:
mapping = mapping.get('doc')
fields = get_fields_recursively(mapping, field_type)
if fields:
_fields.extend(fields)
return list(unique_preserving_order(_fields))