[英]Elastic search condition
您好,我对弹性搜索的条件感到困惑。 如果参数不为空,代码工作正常如果参数没有提供给方法我如何处理这个布尔查询。
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
如果 category 为 None,您需要使用match_all
...只需根据 category 的值有条件地构建您的查询。
这样的事情应该做
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.