elasticsearch bool组合查询,
# bool组合查询 # filter:过滤,不参与打分 # must:如果有多个条件,这些条件都必须满足 and与 # should:如果有多个条件,满足一个或多个即可 or或 # must_not:和must相反,必须都不满足条件才可以匹配到 !非
布尔查询 与匹配其他查询的布尔组合的文档相匹配的查询。bool查询映射到Lucene BooleanQuery。它是使用一个或多个布尔子句构建的,每个子句都有一个类型化的事件。发生的类型是: 发生 描述 must 该条款(查询)必须出现在匹配的文件,并将有助于得分。 filter 子句(查询)必须出现在匹配的文档中。然而不像 must查询的分数将被忽略。Filter子句在过滤器上下文中执行,这意味着评分被忽略,子句被考虑用于高速缓存。 should 子句(查询)应该出现在匹配的文档中。如果 bool查询位于查询上下文中并且具有mustor filter子句,则bool即使没有should查询匹配,文档也将匹配该查询 。在这种情况下,这些条款仅用于影响分数。如果bool查询是过滤器上下文 或者两者都不存在,must或者filter至少有一个should查询必须与文档相匹配才能与bool查询匹配。这种行为可以通过设置minimum_should_match参数来显式控制 。 must_not 子句(查询)不能出现在匹配的文档中。子句在过滤器上下文中执行,意味着评分被忽略,子句被考虑用于高速缓存。因为计分被忽略,0所有文件的分数被返回。 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" } } ] } } }
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。