# \_index，\_uid，\_type，\_id 元字段

## Identity meta-fields（身份的元字段）

| [\_index](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index-field.html) | 文档所属的索引。                                                                                                             |
| --------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| [\_uid](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-uid-field.html)     | 复合字段包含&#x4E86;**\_type** &#x548C;**\_id**。                                                                           |
| [\_type](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-type-field.html)   | 文档的[mapping type（映射类型 ）](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html#mapping-type)。 |
| [\_id](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html)       | 文档 ID。                                                                                                               |

### \_index field

当跨多个索引执行查询时，有时需要添加仅与某些索引相关联的查询子句。该 \_index 字段允许在索引文档上进行索引的匹配。它可以在 term，terms 查询，aggregations（聚合） ， scripts（脚本） ， sorting（排序）中使用 :

> 注意 :
>
> &#x8BE5;**\_index**被表现为虚拟字段 - 它不会作为实际字段添加到**Lucene**索引。这意味着您可以使用**term** 或**terms** 查询（或任何重写到**term**查询的查询，如**match**，**query\_string**或**simple\_query\_string**查询）**\_index**字段，但不支持**prefix（前缀）**，**wildcard（通配符）**，**regexp（正则表达式）**&#x6216;**fuzzy（模糊查询）**。

```
# Example documents
curl -XPUT 'localhost:9200/index_1/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document in index 1"
}
'
curl -XPUT 'localhost:9200/index_2/my_type/2?refresh=true&pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document in index 2"
}
'
curl -XGET 'localhost:9200/index_1,index_2/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "_index": ["index_1", "index_2"] # 1
    }
  },
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index", # 2
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_index": { # 3
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "index_name": {
      "script": {
        "lang": "painless",
        "inline": "doc[\u0027_index\u0027]" # 4
      }
    }
  }
}
'
```

| 1 | 查&#x8BE2;**\_index**字段     |
| - | -------------------------- |
| 2 | 聚&#x5408;**\_index**字段     |
| 3 | &#x5728;**\_index**字段上排序   |
| 4 | 在脚本中访&#x95EE;**\_index**字段 |

### \_type field

每个索引的文档都与一个 [**\_type**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-type-field.html) 以及一个 [**\_id**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-id-field.html)相关联。 为了使类型名称快速搜索，[**\_type**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-type-field.html)字段被索引。

&#x8BE5;**\_type** 字段的值可以在查询，聚合，脚本以及排序时访问：

> 也就是表的概念

```
# Example documents
curl -XPUT 'localhost:9200/my_index/type_1/1?pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with type 1"
}
'
curl -XPUT 'localhost:9200/my_index/type_2/2?refresh=true&pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with type 2"
2
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "_type": [ "type_1", "type_2" ] # 1
    }
  },
  "aggs": {
    "types": {
      "terms": {
        "field": "_type", # 2
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_type": { # 3
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "type": {
      "script": {
        "lang": "painless",
        "inline": "doc['_type']" # 4
      }
    }
  }
}
'
```

| 1 | &#x5728;**\_type**字段上查询   |
| - | ------------------------- |
| 2 | &#x5728;**\_type**字段上聚合   |
| 3 | &#x5728;**\_type**字段上排序   |
| 4 | 在脚本中访&#x95EE;**\_type**字段 |

返回结果:

```
{
  "took": 42,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "my_index",
        "_type": "type_2",
        "_id": "2",
        "_score": null,
        "fields": {
          "type": [
            "type_2"
          ]
        },
        "sort": [
          "type_2"
        ]
      },
      {
        "_index": "my_index",
        "_type": "type_1",
        "_id": "1",
        "_score": null,
        "fields": {
          "type": [
            "type_1"
          ]
        },
        "sort": [
          "type_1"
        ]
      }
    ]
  },
  "aggregations": {
    "types": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "type_1",
          "doc_count": 1
        },
        {
          "key": "type_2",
          "doc_count": 1
        }
      ]
    }
  }
}
```

### \_uid field

每个索引的文档都与一个 [**\_type**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-type-field.html)和一个 [**\_id**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-id-field.html)相关联。

这些值组合为 **{type}＃{id}** 并作为 **\_uid** 字段编入索引。

&#x8BE5;**\_uid** 字段的值可以在**queries**（查询），**aggregations**（聚合），**scripts**（脚本）以及**sorting**（排序）时访问 :

```
# Example documents
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with ID 1"
}
'
curl -XPUT 'localhost:9200/my_index/my_type/2?refresh=true&pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with ID 2"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "_uid": [ "my_type#1", "my_type#2" ] # 1
    }
  },
  "aggs": {
    "UIDs": {
      "terms": {
        "field": "_uid", # 2
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_uid": { # 3
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "UID": {
      "script": {
         "lang": "painless",
         "inline": "doc['_uid']" # 4
      }
    }
  }
}
'
```

| 1 | &#x5728;**\_uid**字段上查询 (也可以参考 [**ids query**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-ids-query.html)) |
| - | ------------------------------------------------------------------------------------------------------------------------------------ |
| 2 | &#x5728;**\_uid**字段上聚合                                                                                                               |
| 3 | &#x5728;**\_uid**字段上排序                                                                                                               |
| 4 | 在脚本中访&#x95EE;**\_uid**字段                                                                                                             |

## \_id field <a href="#title-text" id="title-text"></a>

每个索引的文档都与一个 **\_type** 和一个 **\_id**相关联。

&#x8BE5;**\_id** 字段不被索引，因为它的值可以从 **\_uid** 字段自动导出。

&#x8BE5;**\_id** 字段的值可以在某些查询（ **term , terms , match , query\_string , simple\_query\_string** ）中访问，但不能在**aggregations**（聚合），**scripts**（脚本）或**sorting**（排序）中使用，而应使用 **\_uid** 字段代替 :

```
# Example documents
PUT my_index/my_type/1
{
  "text": "Document with ID 1"
}

PUT my_index/my_type/2&refresh=true
{
  "text": "Document with ID 2"
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] # 1
    }
  }
}
```

| 1 | &#x5728;**\_id**字段上查询（参见[**ids**查询](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-ids-query.html)） |
| - | -------------------------------------------------------------------------------------------------------------------------- |


---

# 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/mapping/322meta-fieldsff08-yuan-zi-duan-ff09/3221-index-uid-type-id-yuan-zi-duan.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.
