Put Mapping /提交映射

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

译文链接 : Put Mapping /提交映射

贡献者 : Le-Mon

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

  1. curl -XPUT 'localhost:9200/twitter ?pretty' -d'
  2. {
  3. "mappings": {
  4. "tweet": {
  5. "properties": {
  6. "message": {
  7. "type": "text"
  8. }
  9. }
  10. }
  11. }
  12. }'
  13. curl -XPUT 'localhost:9200/twitter/_mapping/user ?pretty' -d'
  14. {
  15. "properties": {
  16. "name": {
  17. "type": "text"
  18. }
  19. }
  20. }'
  21. curl -XPUT 'localhost:9200/twitter/_mapping/tweet ?pretty' -d'
  22. {
  23. "properties": {
  24. "user_name": {
  25. "type": "text"
  26. }
  27. }
  28. }'

| Put Mapping /提交映射 - 图1 | 创建一个名叫 twitter 的索引,在 tweet 包含一个字段 message . | | Put Mapping /提交映射 - 图2 | 使用 PUT mapping API 增加一个新的类型: user. | | Put Mapping /提交映射 - 图3 | 使用 PUT mapping API 向 tweet增加user_name 字段. |

有关 mapping 更多的信息请在该章节查看.

Multi-index /多索引

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

  1. PUT /{index}/_mapping/{type}
  2. { body }
  • {index} 接受多个 index 和通配符.
  • {type} 是更新的 type 名称.
  • {body}包含需要更新的字段等.

Updating field mappings/更新字段映射

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

可以将新属性properties 添加到Object数据类型字段中。 新的多字段multi-fields可以添加到现有字段。 可以禁用doc_values ,但不能启用。 可以更新ignore_above 参数。

For example:

  1. curl -XPUT 'localhost:9200/my_index ?pretty' -d'
  2. {
  3. "mappings": {
  4. "user": {
  5. "properties": {
  6. "name": {
  7. "properties": {
  8. "first": {
  9. "type": "text"
  10. }
  11. }
  12. },
  13. "user_id": {
  14. "type": "keyword"
  15. }
  16. }
  17. }
  18. }
  19. }'
  20. curl -XPUT 'localhost:9200/my_index/_mapping/user?pretty' -d'
  21. {
  22. "properties": {
  23. "name": {
  24. "properties": {
  25. "last": {
  26. "type": "text"
  27. }
  28. }
  29. },
  30. "user_id": {
  31. "type": "keyword",
  32. "ignore_above": 100
  33. }
  34. }
  35. }'

| Put Mapping /提交映射 - 图4 | 创建有一个 first 字段的, Object datatype 字段, 和一个 user_id 字段. | | Put Mapping /提交映射 - 图5 | 加入 last字段 在 name object 字段下. | | Put Mapping /提交映射 - 图6 | 更新 ignore_above 设置 默认: 0. |

每个 mapping 参数指定是否可以在现有字段上更新其设置。

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

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

Tip唯一可以免除此规则的参数 - 它们可以设置为每个字段上的不同值 - 可以在“字段在映射typesedit 中共享”一节中找到。

例如 失败操作:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -d'
  2. {
  3. "mappings": {
  4. "type_one": {
  5. "properties": {
  6. "text": {
  7. "type": "text",
  8. "analyzer": "standard"
  9. }
  10. }
  11. },
  12. "type_two": {
  13. "properties": {
  14. "text": {
  15. "type": "text",
  16. "analyzer": "standard"
  17. }
  18. }
  19. }
  20. }
  21. }'
  22. curl -XPUT 'localhost:9200/my_index/_mapping/type_one ?pretty' -d'
  23. {
  24. "properties": {
  25. "text": {
  26. "type": "text",
  27. "analyzer": "standard",
  28. "search_analyzer": "whitespace"
  29. }
  30. }
  31. }'

| Put Mapping /提交映射 - 图8 Put Mapping /提交映射 - 图9 | 创建一个两种类型的索引,它们都包含一个具有相同映射的文本字段。 | | Put Mapping /提交映射 - 图10 | 尝试更新 search_analyzertype_one 字段时抛出异常 "Merge failed with failures...". |

但是这样会成功

  1. curl -XPUT 'localhost:9200/my_index/_mapping/type_one?update_all_types &pretty' -d'
  2. {
  3. "properties": {
  4. "text": {
  5. "type": "text",
  6. "analyzer": "standard",
  7. "search_analyzer": "whitespace"
  8. }
  9. }
  10. }'

| Put Mapping /提交映射 - 图11 | 添加update_all_types参数会更新type_one和type_two中的文本字段。 |