I'm trying to build dynamically a query for ElasticSearch, here is my code:
import json
query_string = "person human"
query = {}
query['query'] = {}
query['query']['bool'] = {}
query['query']['bool']['must'] = {}
must_string = []
term_string = {}
term_string['term'] = {}
term_string['term']["labels.Name"] = ""
for term in query_string.split():
term_string['term']["labels.Name"] = term
must_string.append(term_string)
query['query']['bool']['must'] = must_string
print(json.dumps(query))
I was expecting an output like this:
{"query": {"bool": {"must": [{"term": {"labels.Name": "person"}}, {"term": {"labels.Name": "human"}}]}}}
but instead i'm getting this :
{"query": {"bool": {"must": [{"term": {"labels.Name": "human"}}, {"term": {"labels.Name": "human"}}]}}}
Note the duplicated value in labels.Name
.
There is a problem when I'm appending in the list and that's overwriting the previous value.