Search code examples
pythonelasticsearch

Get all field names in an index elasticsearch python


I am trying to search a particular string against all indexes and fields in elastic search. I was able to get all the indices and search a particular field against the string. How can I get the list of field names in a particular index?

    def check_name_query(self):
    es = Elasticsearch("http://localhost:9200")
    indexlist = es.indices.get_alias()
    print "indices:", indexlist
    text_val = self.textin.text
    for ind in es.indices.get('*'):
        print ind
        res = es.search(index=ind, body={'query': {'wildcard': {'name': "*" + text_val + "*"}}})
        print res['hits']['hits']

This code gets all indices and checks if there is a field name with the value in textinput. Ideally i want to get check against all fields. Thanks in advance!


Solution

  • Try:

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch()
    indices_names = []
    for elem in es.cat.indices(format="json"):
        indices_names.append( elem['index'] )
    
    dict_index_fields = {}
    
    for index in indices_names: 
        mapping = es.indices.get_mapping(index)
        dict_index_fields[index] = []
        for field in mapping[index]['mappings']['properties']:
            dict_index_fields[index].append(field)