Elasticsearch布尔查询——bool
Posted 穷开心y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch布尔查询——bool相关的知识,希望对你有一定的参考价值。
布尔查询允许我们利用布尔逻辑将较小的查询组合成较大的查询。
1、查询返回包含“mill”和“lane”的所有的账户
curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘ { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }‘
在上面的例子中,bool must语句指明了,对于一个文档,所有的查询都必须为真,这个文档才能够匹配成功。
2、查询返回地址中包含“mill”或者“lane”的所有的账户
curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘ { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }‘
在上面的例子中,bool should语句指明,对于一个文档,查询列表中,只要有一个查询匹配,那么这个文档就被看成是匹配的。
3、查询返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户
curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘ { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }‘
在上面的例子中, bool must_not语句指明,对于一个文档,查询列表中的的所有查询都必须都不为真,这个文档才被认为是匹配的。
我们可以在一个bool查询里一起使用must、should、must_not。此外,我们可以将bool查询放到这样的bool语句中来模拟复杂的、多等级的布尔逻辑。
下面这个例子返回40岁以上并且不生活在ID(daho)的人的账户:
curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘ { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }‘
以上是关于Elasticsearch布尔查询——bool的主要内容,如果未能解决你的问题,请参考以下文章
小白学习-ElasticSearch教程 -文档查询之bool查询
Elasticsearch:以更简单的方式编写具有逻辑条件的 Elasticsearch 查询 - query_string