# 提交/获取/获取字段->映射

## 提交/获取/获取字段映射

### Put Mapping /提交映射

提交映射允许提交自定义的类型映射至一个新的索引，或者增加一个新的类型至一个存在的索引，或者增加某个存在类型的字段。

```
curl -XPUT 'localhost:9200/twitter ?pretty' -d'  ＃ 1
{
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "text"
        }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/twitter/_mapping/user ?pretty' -d'  ＃ 2
{
  "properties": {
    "name": {
      "type": "text"
    }
  }
}'
curl -XPUT 'localhost:9200/twitter/_mapping/tweet ?pretty' -d'  ＃ 3
{
  "properties": {
    "user_name": {
      "type": "text"
    }
  }
}'
```

| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO125-1) | 创建一个名叫 `twitter` 的索引，在tweet包含一个字段`message` .  |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO125-2) | 使用 PUT mapping API 增加一个新的类型： `user`.          |
| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/3.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO125-3) | 使用 PUT mapping API 向 `tweet`增加`user_name` 字段. |

有关 [mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) 更多的信息请在该章节查看.

#### Multi-index /多索引

PUT mapping API 可以用一个请求支持多个索引.格式如下

```
PUT /{index}/_mapping/{type}
{ body }
```

* `{index}`接受多个 [index](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html)和通配符.
* `{type}`是更新的 type 名称.
* `{body}`包含需要更新的字段等.

## Updating field mappings/更新字段映射 <a href="#putmapping-ti-jiao-ying-she-updatingfieldmappings-geng-xin-zi-duan-ying-she" id="putmapping-ti-jiao-ying-she-updatingfieldmappings-geng-xin-zi-duan-ying-she"></a>

一般来说，现有字段的映射不能更新。 这个规则有一些例外。 例如：

可以将新属性[`properties`](https://www.elastic.co/guide/en/elasticsearch/reference/current/properties.html) 添加到[Object](https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html)数据类型字段中。\
新的多字段[multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)可以添加到现有字段。\
可以禁用[`doc_values`](https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html) ，但不能启用。\
可以更新[`ignore_above`](https://www.elastic.co/guide/en/elasticsearch/reference/current/ignore-above.html) 参数。

For example:

```
curl -XPUT 'localhost:9200/my_index ?pretty' -d' ＃ 1
{
  "mappings": {
    "user": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "text"
            }
          }
        },
        "user_id": {
          "type": "keyword"
        }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/my_index/_mapping/user?pretty' -d'
{
  "properties": {
    "name": {
      "properties": {
        "last": {   ＃ 2
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 ＃ 3
    }
  }
}'
```

| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO126-1) | 创建有一个 `first` 字段的,  [Object datatype](https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html) 字段, 和一个`user_id` 字段. |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO126-2) | 加入 `last`字段 在`name` object 字段下.                                                                                                         |
| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/3.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO126-3) | 更新`ignore_above` 设置 默认: 0.                                                                                                              |

每个 [mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html)参数指定是否可以在现有字段上更新其设置。

#### Conflicts between fields in different types/多字段/类型的冲突

同一索引中具有相同名称的两个不同类型(type)的的字段必须具有相同的映射，因为它们在内部由相同的字段支持。 尝试更新存在于多个类型中的字段的映射参数将抛出异常，除非您指定update\_all\_types参数，否则将在同一索引中的同一个名称的所有字段上更新该参数。

```
curl -XPUT 'localhost:9200/my_index?pretty' -d'
{
  "mappings": {
    "type_one": {
      "properties": {
        "text": {    ＃ 1
          "type": "text",
          "analyzer": "standard"
        }
      }
    },
    "type_two": {
      "properties": {
        "text": {  ＃ 2
          "type": "text",
          "analyzer": "standard"
        }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/my_index/_mapping/type_one ?pretty' -d' ＃ 3
{
  "properties": {
    "text": {
      "type": "text",
      "analyzer": "standard",
      "search_analyzer": "whitespace"
    }
  }
}'
```

| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO127-1)[![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO127-2) | 创建一个两种类型的索引，它们都包含一个具有相同映射的文本字段。                                              |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/3.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO127-3)                                                                                                                                                                                                     | 尝试更新`search_analyzer`对 `type_one` 字段时抛出异常 `"Merge failed with failures..."`. |

但是这样会成功

```
curl -XPUT 'localhost:9200/my_index/_mapping/type_one?update_all_types &pretty' -d' ＃ 1
{
  "properties": {
    "text": {
      "type": "text",
      "analyzer": "standard",
      "search_analyzer": "whitespace"
    }
  }
}'
```

| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO128-1) | 添加update\_all\_types参数会更新type\_one和type\_two中的文本字段。 |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |

### Get Mapping /获取映射

The get mapping API allows to retrieve mapping definitions for an index or index/type.

```
curl -XGET 'localhost:9200/twitter/_mapping/tweet?pretty'
```

## Multiple Indices and Types <a href="#getmapping-huo-qu-ying-she-multipleindicesandtypes" id="getmapping-huo-qu-ying-she-multipleindicesandtypes"></a>

The get mapping API can be used to get more than one index or type mapping with a single call. General usage of the API follows the following syntax: `host:port/{index}/_mapping/{type}` where both `{index}` and `{type}` can accept a comma-separated list of names. To get mappings for all indices you can use `_all` for `{index}`. The following are some examples:

```
curl -XGET 'localhost:9200/_mapping/tweet,kimchy?pretty'
curl -XGET 'localhost:9200/_all/_mapping/tweet,book?pretty'
```

If you want to get mappings of all indices and types then the following two examples are equivalent:

```
curl -XGET 'localhost:9200/_all/_mapping?pretty'
curl -XGET 'localhost:9200/_mapping?pretty'
```

### Get Field Mapping /获取字段映射

获取单个字段的映射配置

```
curl -XGET 'localhost:9200/twitter/_mapping/tweet/field/message?pretty'
```

响应是（假设文本是默认字符串字段）：

```
{
   "twitter": {
      "mappings": {
         "tweet": {
            "message": {
               "full_name": "message",
               "mapping": {
                  "message": {
                     "type": "text",
                     "fields": {
                        "keyword": {
                           "type": "keyword",
                           "ignore_above": 256
                        }
                     }
                  }
               }
            }
         }
      }
   }
}
```

#### 多个索引,类型和字段

```
curl -XGET 'localhost:9200/twitter,kimchy/_mapping/field/message?pretty'
curl -XGET 'localhost:9200/_all/_mapping/tweet,book/field/message,user.id?pretty'
curl -XGET 'localhost:9200/_all/_mapping/tw*/field/*.id?pretty'
```

get API可以用来获取字段映射,多字段映射多类型映射,多索引映射的一个调用。一般使用API遵循以下语法:`host:port/{index}/{type}/_mapping/field/{field},{index}, {type} 和 {field}`可以代表以逗号分隔的名称或通配符。为所有索引到映射可以使用\_all代替`{index}`。以下是一些例子:

```
curl -XGET 'localhost:9200/twitter,kimchy/_mapping/field/message?pretty'
curl -XGET 'localhost:9200/_all/_mapping/tweet,book/field/message,user.id?pretty'
curl -XGET 'localhost:9200/_all/_mapping/tw*/field/*.id?pretty'
```

#### 具体说明:

获取映射API允许您指定一个或多个用逗号分隔的字段。您也可以使用通配符。字段名可以是以下任何一个：

| Full names  | 完整路径，包括任何父对象名称，字段是（例如[`user.id`](http://user.id/)）的一部分。 |
| ----------- | ------------------------------------------------------- |
| Field names | 没有路径的字段的名称（例如id for "user" : { "id" : 1 } }）。           |

选择作者的id字段,你可以使用它的全名`author.id`。名字将返回字段`author.name`:

```
curl -XGET "http://localhost:9200/publications/_mapping/article/field/author.id,abstract,name"
```

返回:

```
{
   "publications": {
      "article": {
         "abstract": {
            "full_name": "abstract",
            "mapping": {
               "abstract": { "type": "text" }
            }
         },
         "author.id": {
            "full_name": "author.id",
            "mapping": {
               "id": { "type": "text" }
            }
         },
         "name": {
            "full_name": "author.name",
            "mapping": {
               "name": { "type": "text" }
            }
         }
      }
   }
}
```

请注意，响应总是使用请求中指定的相同字段作为键。在每一个进入的full\_name包含的字段的映射返回的全名。当请求可以引用到多个字段时，这非常有用。

#### 其他选项

| `include_defaults` | 添加include\_defaults =true的查询字符串会导致包括默认值的响应，通常抑制。 |
| ------------------ | ------------------------------------------------ |


---

# 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/33-apis/341suo-yin-api/ti-4ea4-huo-53d6-huo-qu-zi-duan-ying-she.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.
