In below code I'm facing KeyError, even though my syntax seems fine.
data = {'id': 20720}
query = '{"query":{"bool":{"must":[{"term":{"status.keyword":"Running"}},{"term":{"id":{id}}}],"filter":[{"term":{"status.keyword":"Running"}}]}}}'
print(query.format(**data))
Traceback (most recent call last):
File ".\demo.py", line 3, in <module>
print(query.format(**data))
KeyError: '"query"'
Here I'm formatting Elasticsearch query, I don't wanted to use replace string because I have to iterate it multiple time if data contains more field.
I'm expecting below output.
{"query":{"bool":{"must":[{"term":{"status.keyword":"Running"}},{"term":{"id":20720}}],"filter":[{"term":{"status.keyword":"Running"}}]}}}
You need to double your curly braces except the ones around the id
variable
query = '{{"query":{{"bool":{{"must"... {{"term":{{"id":{id} }} }}...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments