# Bool 查询

与其他查询的bool组合相匹配的文档的查询。布尔查询映射到**Lucene**的**BooleanQuery**。它使用一个或者多个布尔条件来构建，每个条件都有一个类型出现，出现的类型有:

| 事件            | 描述                                                                                                                                                |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| **must**      | 子句（查询）必须出现在匹配的文档中，并将有助于得分。                                                                                                                        |
| **filter**    | 子句（查询）必须出现在匹配的文档中。 然而不像**must**此查询的分数将被忽略。                                                                                                        |
| **should**    | 子句（查询）应出现在匹配文档中。 在布尔查询中不包含**must**或**filter**子句，一个或多个**should**子句必须有相匹配的文件。 匹配**should**条件的最小数目可通过设&#x7F6E;***minimum\_should\_match***&#x53C2;数。 |
| **must\_not** | 子句（查询）不能出现在匹配的文档中。                                                                                                                                |

> 重要：
>
> **Bool在过滤器上下文中查询**
>
> 如果该查询是在过滤器的上下文中使用，它的**should**子句则至少有一个**should**子句需要相匹配

Bool Query 也支持 disable\_coord 参数(默认为 false)。基本上，坐标相似性基于文档包含的所有查询项的分数来计算得分因子。 可查阅 Lucene 的 BooleanQuery 了解更多详情。

Bool Query 遵循 more-matches-is-better 的方法，所以每个匹配 must 或 should 条件的分数将被累加在一起，从而将最终 \_score 提供给每个文档。

```
POST _search{
  "query": {
    "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,
      "boost" : 1.0
    }
  }}
```

## 使用bool.filter得分

这种情况下指定的查询如使用filter元素对得分没有影响-得分以返回0 。 分数仅受指定查询的影响。 举例来说，当给定的全部文档status字段包&#x542B;***active***&#x5C5E;性时，以下所述三个查询返回所有文档 。\
第一个查询分配比分0的所有文档，因此需指定为没有进球的查询：

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

第二个**Bool**查询包含一个**match\_all**查询，为所有文件分配一个1.0的得分。

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

第三个**constant\_score**查询的作用与上述第二个例子完全相同。 **constant\_score**查询分配由过滤器所匹配的所有文档一个1.0的得分。

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

## 使用命名查询来看看哪些子句匹配

如果你需要知道与 bool 查询返回的所有文档相匹配的此查询中的子句，你可以使用命名查询给每个条件分配一个名称。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/35query-dsldslfang-shi-cha-8be229/355fu-he-cha-8be228-compound-queries/bool-cha-xun.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
