Boolean Query

Boolean Query

Bool查询对应Lucene中的BooleanQuery,它由一个或者多个子句组成,每个子句都有特定的类型。

  • must : 返回的文档必须满足must子句的条件,并且参与计算分值

  • filter : 返回的文档必须满足filter子句的条件。但是不会像Must一样,参与计算分值

  • should : 返回的文档可能满足should子句的条件。在一个Bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回。

    • minimum_should_match参数定义了至少满足几个子句。
  • must_nout : 返回的文档必须不满足must_not定义的条件。

    • 如果一个查询既有filter又有should,那么至少包含一个should子句。
    • bool查询也支持禁用协同计分选项disable_coord。一般计算分值的因素取决于所有的查询条件。

    • bool查询也是采用more_matches_is_better的机制,因此满足must和should子句的文档将会合并起来计算分值。

curl -X POST "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user.id" : "11" }
      },
      "filter": {
        "term" : { "tags" : "production" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tags" : "env1" } },
        { "term" : { "tags" : "deployed" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}
'
{
  "query": {
    "bool": {
      "must": [
          {
            "match_phrase": {
              "title": {"query":"Research of detection method of silkworm", "slop": 3}
            }
          }
        ],
        "should":[
            {"match": {
              "author.name": {"query": "Wang ming qi", "_name": "author_name"}
            }}
          ],
        "filter": [
          { "ids": {"values": [41, 42]}},
          {"range": {"year": {"gte": 2000}}}
        ]
    }
  }
}
原文地址:https://www.cnblogs.com/Mint-diary/p/14609178.html