Hello i am confuse in condition in elastic search. Code works fine if parameter is not empty if parameter is not given to method how can i handle this bool query.
def elastic_search(category=None):
client = Elasticsearch(host="localhost", port=9200)
query_all = {
'size': 10000,
'query': {
"bool": {
"filter": [
{
"match": {
"category": category
}
}]
},
}
}
resp = client.search(
index="my-index",
body=query_all
)
return resp
You need to use match_all
if category is None... simply build your query conditionally depending on what the value of category is.
Something like this should do
def elastic_search(category=None):
client = Elasticsearch(host="localhost", port=9200)
query_all = {
'size': 10000,
'query': {}
}
if category is None:
query_all['query']['match_all'] = {}
else:
query_all['query']['bool'] = {
"filter": [
{
"match": {
"category": category
}
}
]
}
resp = client.search(
index="my-index",
body=query_all
)
return resp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.