Mayank Jha Mayank Jha - 1 month ago 9
Python Question

Passing index as a parameter in the python query for elasticsearch

I have a piece of python query to retrieve data from elasticsearch:-

es=Elasticsearch(['http://localhost:9200'])
res = es.search(index="index1", doc_type="log",size=1000, from_=0, body={ "query": {
"match": {
....Match condition
}
}
}})

Is there any way we can pass the index as parameter i.e assign the value of index1 outside the query and then use it for extracting results?

Answer

I believe you do not want to hard code the index value. IF that is the case you can always use format

'{0}'.format(*args, **kwargs)

In your case you can write like this:

res = es.search(index='{0}'.format(index1), doc_type="log",size=1000, from_=0, body={ "query": { "match": { ....Match condition } } }})
Comments