【转】Elasticsearch-bool组合查询

使用实例
rs = []
rs.append({"match": {"grzh": grzh}})
rs.append({"match_phrase": {"ywlx": "缴"}})
detailRes, detailTotal = elSearch.searchDataSearch("transaction", 0, 100,
rs, [{"rq": {"order": "desc"}}, {"hjyf": {"order": "desc"}}], "must")

if detailRes and len(detailRes) > 0:
ids = []
for item in detailRes:
ids.append(item["_id"])
result = hbase.getRows("transaction" if dataType < 2 else "debit", ids)
if result != None and len(result) > 0:
for key, item in result:
if dataType == 1:
lastDetail["payemnh"] = item["jyrq:"].split()[0]
lastDetail["depmny"] = float(item["credit_sum:"])
else:
lastDetail["payemnh"] = ""
lastDetail["depmny"] = ""

-----------------------------------------------------------------------------------------------------------------------------

# bool组合查询
# filter:过滤,不参与打分
# must:如果有多个条件,这些条件都必须满足 and与
# should:如果有多个条件,满足一个或多个即可 or或
# must_not:和must相反,必须都不满足条件才可以匹配到 !非


GET 51jobs/job/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"salary": 6666
}

}
}
}
}

# select * from job where salary=6666 or salary=7777
# 查询所有数据,筛选出工资等于6666或者7777的数据
GET 51jobs/job/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"terms": {
"salary": [6666,7777]
}

}
}
}
}
# 查询salary等于6666或者title等于python、salary不等于7777、salary不等于8888

GET 51jobs/job/_search
{
"query": {
"bool": {
"should": [
{"term": {
"salary": {
"value": 6666
}
}},
{"term": {
"title": {
"value": "python"
}
}}
],
"must_not": [
{"term": {
"salary": {
"value": 7777
}
}},
{"term": {
"salary": {
"value": 8888
}
}}
]
}
}
}

# salary等于6666或者title等于python salary不等于9999 title不等于redis

GET 51jobs/job/_search
{
"query": {
"bool": {
"should": [
{"term": {
"salary": {
"value": 6666
}
}},
{"term": {
"title": {
"value": "python"
}
}}
],
"must_not": [
{"term": {
"salary": {
"value": 9999
}
}},
{
"term": {
"title": {
"value": "redis"
}
}
}
]
}
}
}
# 查询title为python或者(title为搜索并且salary等于6666)的数据
GET 51jobs/job/_search
{
"query": {
"bool": {
"should": [
{"term": {
"title": {
"value": "python"
}
}},
{
"bool": {
"must": [
{"term": {
"title": {
"value": "搜索"
}
}},
{
"term": {
"salary": {
"value": 6666
}
}
}
]
}

}
]
}
}
}


# 添加空值测试数据
PUT nulldb/test/_bulk
{"index":{"_id":1}}
{"tags":["IT","python"]}
{"index":{"_id":2}}
{"tags":["java","python"]}
{"index":{"_id":3}}
{"tags":null}
{"index":{"_id":4}}
{"tags":["IT","php"]}
{"index":{"_id":5}}
{"tags":[null,"python"]}


# 处理null空值
# 过滤空值
# exists 处理值不为空或者值不为null的数据
GET nulldb/test/_search
{
"query": {
"bool": {
"filter": {
"exists": {
"field": "tags"
}
}
}
}
}

# 筛选出tags值为空或者没有tags属性的数据
GET nulldb/test/_search
{
"query": {
"bool": {
"must_not": [
{
"exists":{
"field":"tags"
}
}
]
}
}
}

---------------------
作者:纳尔逊皮卡丘
来源:CSDN
原文:https://blog.csdn.net/zhaobig/article/details/78502805
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/UUUz/p/11170799.html