{ "query": { "bool": { "must": { "match": { "title": "quick" }}, "must_not": { "match": { "title": "lazy" }}, "should": [ { "match": { "title": "brown" }}, { "match": { "title": "dog" }} ] } } }
- must: 文档必须完全匹配条件
- should: should下面会带一个以上的条件,至少满足一个条件,这个文档就符合should
- must_not: 文档必须不匹配条件
-
match
最简单的一个match例子:
查询和"我的宝马多少马力"这个查询语句匹配的文档。
{ "query": { "match": { "content" : { "query" : "我的宝马多少马力" } } } }
上面的查询匹配就会进行分词,比如"宝马多少马力"会被分词为"宝马 多少 马力", 所有有关"宝马 多少 马力", 那么所有包含这三个词中的一个或多个的文档就会被搜索出来。
并且根据lucene的评分机制(TF/IDF)来进行评分。match_phrase
比如上面一个例子,一个文档"我的保时捷马力不错"也会被搜索出来,那么想要精确匹配所有同时包含"宝马 多少 马力"的文档怎么做?就要使用 match_phrase 了
{ "query": { "match_phrase": { "content" : { "query" : "我的宝马多少马力" } } } }
完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。
{ "query": { "match_phrase": { "content" : { "query" : "我的宝马多少马力", "slop" : 1 } } } }
multi_match
如果我们希望两个字段进行匹配,其中一个字段有这个文档就满足的话,使用multi_match
{ "query": { "multi_match": { "query" : "我的宝马多少马力", "fields" : ["title", "content"] } } }
但是multi_match就涉及到匹配评分的问题了。
-
控制精度编辑
所有
must
语句必须匹配,所有must_not
语句都必须不匹配,但有多少should
语句应该匹配呢? 默认情况下,没有should
语句是必须匹配的,只有一个例外:那就是当没有must
语句的时候,至少有一个should
语句必须匹配。就像我们能控制
match
查询的精度 一样,我们可以通过minimum_should_match
参数控制需要匹配的should
语句的数量, 它既可以是一个绝对的数字,又可以是个百分比:GET /my_index/my_type/_search { "query": { "bool": { "should": [ { "match": { "title": "brown" }}, { "match": { "title": "fox" }}, { "match": { "title": "dog" }} ], "minimum_should_match": 2 } } }
- 参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/bool-query.html
- https://www.cnblogs.com/yjf512/p/4897294.html