Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 654
Philomath Avatar asked Oct 21 '25 15:10

Philomath


1 Answers

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) 
like image 156
Lupanoide Avatar answered Oct 24 '25 05:10

Lupanoide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!