加载中

显式映射

您比 Elasticsearch 更了解自己的数据,因此虽然动态映射在入门时很有用,但在某些时候,您将需要指定自己的显式映射。

您可以在创建索引时和向现有索引添加字段时创建字段映射。

您可以使用 create index API 来创建带有显式映射的新索引。

				PUT /my-index-000001
					{
  "mappings": {
    "properties": {
      "age":    { "type": "integer" },
      "email":  { "type": "keyword"  },
      "name":   { "type": "text"  }
    }
  }
}
		
  1. 创建一个 age 字段,类型为 integer
  2. 创建一个 email 字段,类型为 keyword
  3. 创建一个 name 字段,类型为 text

您可以使用 update mapping API 向现有索引添加一个或多个新字段。

以下示例添加了 employee-id 字段,这是一个 keyword 字段,其 index 映射参数值为 false。这意味着 employee-id 字段的值会被存储,但不会被索引,也不能用于搜索。

				PUT /my-index-000001/_mapping
					{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false
    }
  }
}
		

除了支持的映射参数外,您不能更改现有字段的映射或字段类型。更改现有字段可能会导致已索引的数据失效。

如果您需要更改数据流支撑索引(backing indices)中某个字段的映射,请参阅更改数据流的映射和设置

如果您需要更改其他索引中某个字段的映射,请使用正确的映射创建一个新索引,并将数据重新索引 (reindex) 到该索引中。

重命名字段会使以旧字段名称索引的数据失效。请改用 alias 字段来创建替代字段名称。

您可以使用 get mapping API 来查看现有索引的映射。

				GET /my-index-000001/_mapping
		

API 返回以下响应

{
  "my-index-000001" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "email" : {
          "type" : "keyword"
        },
        "employee-id" : {
          "type" : "keyword",
          "index" : false
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}
		

如果您只想查看一个或多个特定字段的映射,可以使用 get field mapping API。

如果您不需要索引的完整映射,或者您的索引包含大量字段,这会很有用。

以下请求检索 employee-id 字段的映射。

				GET /my-index-000001/_mapping/field/employee-id
		

API 返回以下响应

{
  "my-index-000001" : {
    "mappings" : {
      "employee-id" : {
        "full_name" : "employee-id",
        "mapping" : {
          "employee-id" : {
            "type" : "keyword",
            "index" : false
          }
        }
      }
    }
  }
}
		
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.