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

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

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

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

Multi-index /多索引

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

PUT /{index}/_mapping/{type}
{ body }
  • {index}接受多个 index和通配符.

  • {type}是更新的 type 名称.

  • {body}包含需要更新的字段等.

Updating field mappings/更新字段映射

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

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

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

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

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

但是这样会成功

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

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

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允许您指定一个或多个用逗号分隔的字段。您也可以使用通配符。字段名可以是以下任何一个:

选择作者的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包含的字段的映射返回的全名。当请求可以引用到多个字段时,这非常有用。

其他选项

Last updated