Elasticsearch-复合查询之bool查询

前言

本文基于elasticsearch7.3.0版本

格式

Elasticsearch bool查询对应Lucene BooleanQuery, 格式如下

{
    
    
    "query":{
    
    
        "bool":{
    
    
            "must":[

            ],
            "should":[

            ],
            "must_not":[

            ],
            "filter":[

            ],
            "minimum_should_match":0
        }
    }
}
  • must: 必须匹配,查询上下文,加分
  • should: 应该匹配,查询上下文,加分
  • must_not: 必须不匹配,过滤上下文,过滤
  • filter: 必须匹配,过滤上下文,过滤

bool查询采用了一种匹配越多越好的方法,因此每个匹配的must或should子句的分数将被加在一起,以提供每个文档的最终得分

minimum_should_match的默认值

minimum_should_match默认值分为两种情况

  • 至少包含一个should且无must和filter子句时,默认值为1
  • 除去以上情况,默认值为0

实例

POST _search
{
    
    
    "query":{
    
    
        "bool":{
    
    
            "must":{
    
    
                "term":{
    
    
                    "user":"kimchy"
                }
            },
            "filter":{
    
    
                "term":{
    
    
                    "tag":"tech"
                }
            },
            "must_not":{
    
    
                "range":{
    
    
                    "age":{
    
    
                        "gte":10,
                        "lte":20
                    }
                }
            },
            "should":[
                {
    
    
                    "term":{
    
    
                        "tag":"wow"
                    }
                },
                {
    
    
                    "term":{
    
    
                        "tag":"elasticsearch"
                    }
                }
            ],
            "minimum_should_match":1,
            "boost":1
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28988969/article/details/107468704
今日推荐