# Terms 查询

筛选所拥有的字段能够匹配提供的多个项（**not analyzed**）中任意一个的文档。

例如：

```
GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "terms" : { "user" : ["kimchy", "elasticsearch"]}
            }
        }
    }
}
```

**terms Query**（多项查询）常常别名后和 **in** 配合作为筛选器名字的简单用法（*5.0.0*已经弃用）。

## ***Terms*****（多项）查找机制**

当需要用大量的项来指定项过滤器时，它可以有助于从索引中的文档中提取这些项值。一个具体的例子是过滤由你的追随者推送的tweet。在项过滤器中指定的用户ID的数量可以很多。在这种情况下，使用项过滤器的术语查找机制是有意义的。

多项查找机制支持以下项目：

| 选项      | 说明                                      |
| ------- | --------------------------------------- |
| index   | **index**（索引）被用来提取项的值。默认当前**index**（索引） |
| type    | **type**（类型）被用来提取项的值。                   |
| id      | 用文档**id （ID）**&#x6765;提取项的值。            |
| path    | 指定为路径的字段，以获取**term**（项）筛选器的实际值。         |
| routing | 检索外部**term**（项）文档时要使用的自定义路由值。           |

项过滤器的值将从具有指定类型和索引中指定标识的文档中的字段获取。在内部，执行一个获取请求需要从指定的路径获取值。目前这个功能的工作需要存&#x50A8;*\_source*。\
此外，如果“引用”项数据不大，请考虑使用具有单个分片的索引，并在所有节点中完全复制。如果可能，项查找过滤器将更乐意在本地节点上执行 **get** 请求，以减少对网络的需求。

## Terms （多项）查找 **twitter （推特）**&#x793A;例

首先，我们索引**ID**为2的用户的信息，特别是其关注者，索引来自**ID** 为1的用户的推文。最后，我们搜索与用户2的关注者匹配的所有tweet。

```
PUT /users/user/2
{
    "followers" : ["1", "3"]
}
PUT /tweets/tweet/1
{
    "user" : "1"
}
GET /tweets/_search
{
    "query" : {
        "terms" : {
            "user" : {
                "index" : "users",
                "type" : "user",
                "id" : "2",
                "path" : "followers"
            }
        }
    }
}
```

外部项文档的结构还可以包括内部对象的数组，例如：

```
curl -XPUT localhost:9200/users/user/2 -d '{
 "followers" : [
   {
     "id" : "1"
   },
   {
     "id" : "2"
   }
 ]
}'
```

在这种情况下，查找路径将是*followers.id*。


---

# 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/terms-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.
