Query DSL - Boolean query

Boolean

Boolean query

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence. The occurrence types are:

一个文档匹配的查询,通过与其他查询的布尔组合进行匹配。布尔查询映射到Lucene BooleanQuery。它是使用一个或多个布尔子句构建的,每个子句都具有出现类型。出现类型为:

OccurDescription
mustThe clause (query) must appear in matching documents and will contribute to the score.
filterThe clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.
shouldThe clause (query) should appear in the matching document.
must_notThe clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.
发生描述
must子句(查询)必须出现在匹配的文档中,并将有助于得分。这是and的语义。
filter子句(查询)必须出现在匹配的文档中。但是与must查询不同的是得分将被忽略。Filter子句在filter上下文中执行,这意味着计分被忽略,并且子句被考虑用于缓存。
should子句(查询)可能出现在匹配的文档中。这是or的语义。
must_not子句(查询)不得出现在匹配的文档中。子句在过滤器上下文中执行,这意味着计分被忽略,并且子句被视为用于缓存。由于计分被忽略,因此所有文档返回的分数为0。这是not的语义。

The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document.

bool查询采用的是“更好匹配”的方法,因此每个匹配项mustshould子句的得分将加在一起,以提供每个文档最终的_score结果。

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.0
    }
  }
}

Copy as cURLView in Console

Using minimum_should_match

使用minimum_should_match

You can use the minimum_should_match parameter to specify the number or percentage of should clauses returned documents must match.

您可以使用该minimum_should_match参数指定should返回的文档必须匹配的子句的数量或百分比。

If the bool query includes at least one should clause and no must or filter clauses, the default value is 1. Otherwise, the default value is 0.

如果bool查询包含至少一个should子句,而没有mustfilter子句,则默认值为1。否则,默认值为0

For other valid values, see the minimum_should_match parameter.

有关其他有效值,请参见 minimum_should_match参数

Scoring with bool.filter

评分与bool.filter

Queries specified under the filter element have no effect on scoring — scores are returned as 0. Scores are only affected by the query that has been specified. For instance, all three of the following queries return all documents where the status field contains the term active.

filter元素下指定的查询对得分没有影响-得分返回0。分数仅受指定查询的影响。例如,以下所有三个查询返回status字段包含active的所有文档。

This first query assigns a score of 0 to all documents, as no scoring query has been specified:

这个查询由于未指定任何得分查询,因此查询为所有文档分配分数0

GET _search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "status": "active"
        }
      }
    }
  }
}

Copy as cURLView in Console

This bool query has a match_all query, which assigns a score of 1.0 to all documents.

这个bool查询具有一个match_all查询,该查询为所有文档分配分数1.0

GET _search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "status": "active"
        }
      }
    }
  }
}

Copy as cURLView in Console

This constant_score query behaves in exactly the same way as the second example above. The constant_score query assigns a score of 1.0 to all documents matched by the filter.

constant_score查询的行为与上面的第二个示例完全相同。该constant_score查询为过滤器匹配的所有文档分配分数1.0

GET _search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "status": "active"
        }
      }
    }
  }
}

Copy as cURLView in Console

Using named queries to see which clauses matchededit

If you need to know which of the clauses in the bool query matched the documents returned from the query, you can use named queries to assign a name to each clause.

如果需要了解布尔查询中的哪个子句与从查询返回的文档相匹配,则可以使用 命名查询为每个子句分配名称。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值