# Term 查询

**term query**（项查询）是查找包含在反向索引中指定的**确切项**的文档。

例如：

```
POST _search
{
  "query": {
    "term" : { "user" : "Kimchy" } ①
  }
}
```

| ① | 在反向索引的*user*类型中查询确切项包含*Kimchy*的文档。 |
| - | ---------------------------------- |

可以指定 *boost* 参数，用以使此**term query** （项查询）比另一个查询具有更高的相关性分数。

例如：

```
GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": {
              "value": "urgent",
              "boost": 2.0 ①
            }
          }
        },
        {
          "term": {
            "status": "normal"  ②
          }
        }
      ]
    }
  }
}
```

| ① | 紧急查询子句有一个 *boost 2.0*，意味着它比正常查询子句重要两倍。 |
| - | -------------------------------------- |
| ② | *normal* 子句拥有默认中立的 *boost 2.0 。*       |

**为什么 term query （项查询）与我的文档不匹配？**

字符串字段可以是 **text**（文本类型：作为完整文本处理，如电子邮件正文）、 **keyword** （关键字：视为精确值，如电子邮件地址或邮政编码）、 **Exact values**（精确值：如数字，日期和关键字）在字段中指定的精确值添加到倒排索引中确保它可以被搜索。

然而，文本字段是被分析过的。这意味着它们的值首先通过 [**analyzer**](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)（分析器）以产生项列表，然后将其添加到倒排索引。

有很多方法可以分析文本：默认 [**standard analyzer**](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html)（标准分析器） 删除大多数标点符号，将文本分成单个单词，并将其减少。例如，标准分析器会将字符串 “Quick Brown Fox！” 改成项 \[quick，brown，fox] 。

这个分析过程使得可以在一大块全文中搜索单个单词。

**term query**（项查询）在字段的倒排索引中查找一个**确切的项**，它并不知道关于字段 **analyzer** （分析器）的任何事。这就确保在使用 **keyword** （关键字）、数字、日期等字段查找值是有用的。当查询全文字段的时候，使用 [**match query**](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) （匹配查询）来替代，它可以清楚的知道字段如何被分析的。

为了演示，尝试下面的例子。首先，创建一个索引，指定字段映射，并索引文档：

```
PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "full_text": {
          "type":  "text" ①
        },
        "exact_value": {
          "type":  "keyword" ②
        }
      }
    }
  }
}
PUT my_index/my_type/1
{
  "full_text":   "Quick Foxes!", ③
  "exact_value": "Quick Foxes!"  ④
}
```

| ① | *full\_text*（全文本） 字段是**text**（文本） 类型并且将被分析。      |
| - | ------------------------------------------------ |
| ② | exact\_value （精确值） 字段是**keyword**（关键字）类型并且不会被分析。 |
| ③ | *full\_text*（全文本）倒排索引将会包含项列表：*\[quick,foxes]*。   |
| ④ | exact\_value （精确值）倒排索引将会包含精确项：*\[Quick Foxes!]*。 |

现在，对比一下**term query**（项查询）和**match query**（匹配查询）的结果集：

```
GET my_index/my_type/_search
{
  "query": {
    "term": {
      "exact_value": "Quick Foxes!" ①
    }
  }
}
GET my_index/my_type/_search
{
  "query": {
    "term": {
      "full_text": "Quick Foxes!" ②
    }
  }
}
GET my_index/my_type/_search
{
  "query": {
    "term": {
      "full_text": "foxes" ③
    }
  }
}
GET my_index/my_type/_search
{
  "query": {
    "match": {
      "full_text": "Quick Foxes!" ④
    }
  }
}
```

① 该查询匹配到结果，是因为*exact\_value* （精确值）字段包含精确项 *Quick Foxes!*。

② 该查询没有匹配到结果，是因为*full\_text*（全文本）字段仅包含项 *quick*和*foxes*。并不包含精确项 *Quick Foxes!* 。

③ 项 *foxes*通过**term query**（项查询）匹配到*full\_text*（全文本）字段。

④**match query**（匹配查询）首先在*full\_text*（全文本）字段上分析查询字符串，然后查看文档是否包含*quick*或者*foxes*或者*quick foxes*。


---

# 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/354zhu-yu-ji-bie-cha-8be228-term-level-queries/term-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.
