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

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

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

创建一个名叫 twitter 的索引,在tweet包含一个字段message .

使用 PUT mapping API 增加一个新的类型: user.

使用 PUT mapping API 向 tweet增加user_name 字段.

有关 mappingarrow-up-right 更多的信息请在该章节查看.

Multi-index /多索引

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

  • {index}接受多个 indexarrow-up-right和通配符.

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

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

Updating field mappings/更新字段映射

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

可以将新属性propertiesarrow-up-right 添加到Objectarrow-up-right数据类型字段中。 新的多字段multi-fieldsarrow-up-right可以添加到现有字段。 可以禁用doc_valuesarrow-up-right ,但不能启用。 可以更新ignore_abovearrow-up-right 参数。

For example:

创建有一个 first 字段的, Object datatypearrow-up-right 字段, 和一个user_id 字段.

加入 last字段 在name object 字段下.

更新ignore_above 设置 默认: 0.

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

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

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

创建一个两种类型的索引,它们都包含一个具有相同映射的文本字段。

尝试更新search_analyzertype_one 字段时抛出异常 "Merge failed with failures...".

但是这样会成功

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

Get Mapping /获取映射

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

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:

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

Get Field Mapping /获取字段映射

获取单个字段的映射配置

响应是(假设文本是默认字符串字段):

多个索引,类型和字段

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

具体说明:

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

Full names

完整路径,包括任何父对象名称,字段是(例如user.idarrow-up-right)的一部分。

Field names

没有路径的字段的名称(例如id for "user" : { "id" : 1 } })。

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

返回:

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

其他选项

include_defaults

添加include_defaults =true的查询字符串会导致包括默认值的响应,通常抑制。

Last updated

Was this helpful?