lqshow / notes

Cheat Sheet
10 stars 2 forks source link

Elasticsearch Bool Query #17

Open lqshow opened 6 years ago

lqshow commented 6 years ago

Overview

布尔查询是最常用的组合查询,不仅将多个查询条件组合在一起,并且将查询的结果和结果的评分组合在一起,所有子查询之间的逻辑关系是与(and)

Bool查询包括四种子句: must,filter, should, must_not。 布尔查询的四个子句,都可以是数组字段

key desc
must 文档必须匹配must查询条件(相当于and)
should 文档应该匹配should子句查询的一个或多个,默认情况下,匹配的文档必须满足其中一个子查询条件,可通过修改minimum_should_match数量 (相当于or)
must_not 文档不能匹配该查询条件 (相当于not)
filter 过滤器,文档必须匹配该过滤条件,跟must子句的唯一区别是,filter不影响查询的score

Example

{
    "bool" : {
        "must" : {
            "term" : { "user" : "kimchy" }
        },
        "filter": {
            "term" : { "tag" : "tech" }
        },
        "must_not" : {
            "range" : {
                "age" : { "from" : 10, "to" : 20 }
            }
        },
        "should" : [
            {  "term" : { "tag" : "wow" } },
            {  "term" : { "tag" : "elasticsearch" } }
        ],
        "minimum_should_match" : 1
    }
}