1. Put Mapping /提交映射

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

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

贡献者 : Le-Mon

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

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"
    }
  }
}'

| | 创建一个名叫 twitter 的索引,在 tweet 包含一个字段 message . | | | 使用 PUT mapping API 增加一个新的类型: user. | | | 使用 PUT mapping API 向 tweet增加user_name 字段. |

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

1.1. Multi-index /多索引

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

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

1.2. Updating field mappings/更新字段映射

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

可以将新属性properties 添加到Object数据类型字段中。 新的多字段multi-fields可以添加到现有字段。 可以禁用doc_values ,但不能启用。 可以更新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 
    }
  }
}'

| | 创建有一个 first 字段的, Object datatype 字段, 和一个 user_id 字段. | | | 加入 last字段 在 name object 字段下. | | | 更新 ignore_above 设置 默认: 0. |

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

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

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

Tip唯一可以免除此规则的参数 - 它们可以设置为每个字段上的不同值 - 可以在“字段在映射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"
    }
  }
}'

| | 创建一个两种类型的索引,它们都包含一个具有相同映射的文本字段。 | | | 尝试更新 search_analyzertype_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"
    }
  }
}'

| | 添加update_all_types参数会更新type_one和type_two中的文本字段。 |

Copyright © Kilvn 2021. all right reserved,powered by Gitbook最后更新时间: 2021-06-08 20:22:42

results matching ""

    No results matching ""