💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Put Mapping /提交映射 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html) 译文链接 : [Put Mapping /提交映射](/pages/viewpage.action?pageId=4882803)[](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html) 贡献者 : [Le-Mon](/display/~tanwen) 提交映射允许提交自定义的类型映射至一个新的索引,或者增加一个新的类型至一个存在的索引,或者增加某个存在类型的字段。 ``` curl -XPUT 'localhost:9200/twitter ?pretty' -d' { "mappings": { "tweet": { "properties": { "message": { "type": "text" } } } } }' curl -XPUT 'localhost:9200/twitter/_mapping/user ?pretty' -d' { "properties": { "name": { "type": "text" } } }' curl -XPUT 'localhost:9200/twitter/_mapping/tweet ?pretty' -d' { "properties": { "user_name": { "type": "text" } } }' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO125-1) | 创建一个名叫 `twitter` 的索引,在 tweet 包含一个字段 `message` . | | [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO125-2) | 使用 PUT mapping API 增加一个新的类型: `user`. | | [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](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 "Mapping") 更多的信息请在该章节查看. ## Multi-index /多索引 PUT mapping API 可以用一个请求支持多个索引.格式如下 ``` PUT /{index}/_mapping/{type} { body } ``` * `{index}` 接受多个 [index](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html "Multiple Indices") 和通配符. * `{type}` 是更新的 type 名称. * `{body} `包含需要更新的字段等. ## Updating field mappings/更新字段映射 一般来说,现有字段的映射不能更新。 这个规则有一些例外。 例如: 可以将新属性[`properties`](https://www.elastic.co/guide/en/elasticsearch/reference/current/properties.html "properties") 添加到[Object](https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html "Object datatype")数据类型字段中。 新的多字段[multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html "fields")可以添加到现有字段。 可以禁用[`doc_values`](https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html "doc_values") ,但不能启用。 可以更新[`ignore_above`](https://www.elastic.co/guide/en/elasticsearch/reference/current/ignore-above.html "ignore_above") 参数。 For example: ``` curl -XPUT 'localhost:9200/my_index ?pretty' -d' { "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": { "type": "text" } } }, "user_id": { "type": "keyword", "ignore_above": 100 } } }' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](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 "Object datatype") 字段, 和一个 `user_id` 字段. | | [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO126-2) | 加入 `last`字段 在 `name` object 字段下. | | [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](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 "Mapping parameters") 参数指定是否可以在现有字段上更新其设置。 ## Conflicts between fields in different types/多字段/类型的冲突 同一索引中具有相同名称的两个不同类型(type)的的字段必须具有相同的映射,因为它们在内部由相同的字段支持。 尝试更新存在于多个类型中的字段的映射参数将抛出异常,除非您指定update_all_types参数,否则将在同一索引中的同一个名称的所有字段上更新该参数。 ![Tip](https://img.kancloud.cn/de/e3/dee3f82dc73d84dc4c0ec544b7f0dda7_66x58.jpg)唯一可以免除此规则的参数 - 它们可以设置为每个字段上的不同值 - 可以在“[字段在映射typesedit ](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html#field-conflicts "Fields are shared across mapping typesedit")中共享”一节中找到。 例如 失败操作: ``` curl -XPUT 'localhost:9200/my_index?pretty' -d' { "mappings": { "type_one": { "properties": { "text": { "type": "text", "analyzer": "standard" } } }, "type_two": { "properties": { "text": { "type": "text", "analyzer": "standard" } } } } }' curl -XPUT 'localhost:9200/my_index/_mapping/type_one ?pretty' -d' { "properties": { "text": { "type": "text", "analyzer": "standard", "search_analyzer": "whitespace" } } }' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO127-1) [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO127-2) | 创建一个两种类型的索引,它们都包含一个具有相同映射的文本字段。 | | [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](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' { "properties": { "text": { "type": "text", "analyzer": "standard", "search_analyzer": "whitespace" } } }' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#CO128-1) | 添加update_all_types参数会更新type_one和type_two中的文本字段。 |