加载中

更新映射 API 示例

本页面提供了如何使用 更新映射 API 在创建索引后修改索引映射的示例。

您可以了解如何:

更新映射 API 需要现有的数据流或索引。以下 创建索引 API 请求创建了一个不带映射的 publications 索引。

				PUT /publications
		

以下更新映射 API 请求向 publications 索引添加了 title(一个新的 text 字段)。

				PUT /publications/_mapping
					{
  "properties": {
    "title": { "type": "text" }
  }
}
		

更新映射 API 可以在单个请求中应用于多个数据流或索引。例如,您可以同时更新 my-index-000001my-index-000002 索引的映射。

				
					# Create the two indices
				PUT /my-index-000001
				PUT /my-index-000002
					# Update both mappings
				PUT /my-index-000001,my-index-000002/_mapping
					{
  "properties": {
    "user": {
      "properties": {
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}
		

您可以使用更新映射 API 向现有的 object 字段添加新属性。

首先,创建一个带有 name 对象字段和内部 first 文本字段的索引

				PUT /my-index-000001
					{
  "mappings": {
    "properties": {
      "name": {
        "properties": {
          "first": {
            "type": "text"
          }
        }
      }
    }
  }
}
		

然后,使用更新映射 API 向 name 字段添加一个新的内部 last 文本字段

				PUT /my-index-000001/_mapping
					{
  "properties": {
    "name": {
      "properties": {
        "last": {
          "type": "text"
        }
      }
    }
  }
}
		

多字段 (Multi-fields) 允许您以不同的方式索引同一个字段。您可以使用更新映射 API 来更新 fields 映射参数并为现有字段启用多字段。

警告

如果在您添加多字段时索引(或数据流)中已经包含文档,则这些文档将不会具有新多字段的值。您可以使用 update by query API 来填充新的多字段。

要查看其工作原理,请尝试以下示例。

使用 创建索引 API 创建一个包含 city text 字段的索引

				PUT /my-index-000001
					{
  "mappings": {
    "properties": {
      "city": {
        "type": "text"
      }
    }
  }
}
		

city 启用多字段

				PUT /my-index-000001/_mapping
					{
  "properties": {
    "city": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    }
  }
}
		

并非所有映射参数都是可更新的,但像 ignore_above 这样的一些参数是可以更改的。

创建一个 ignore_above: 20 的索引

				PUT /my-index-000001
					{
  "mappings": {
    "properties": {
      "user_id": {
        "type": "keyword",
        "ignore_above": 20
      }
    }
  }
}
		

ignore_above 更新为 100

				PUT /my-index-000001/_mapping
					{
  "properties": {
    "user_id": {
      "type": "keyword",
      "ignore_above": 100
    }
  }
}
		

不能 更改现有字段的字段类型。相反,请使用所需的映射创建一个新索引并 重索引 (reindex) 您的数据。

创建一个包含 long 类型 user_id 字段的索引

				PUT /my-index-000001
					{
  "mappings": {
    "properties": {
      "user_id": {
        "type": "long"
      }
    }
  }
}
		

索引一些文档

POST /my-index-000001/_doc?refresh=wait_for
{
  "user_id": 12345
}

POST /my-index-000001/_doc?refresh=wait_for
{
  "user_id": 12346
}
		

创建一个将 user_id 字段设为 keyword 类型的新索引

				PUT /my-new-index-000001
					{
  "mappings": {
    "properties": {
      "user_id": {
        "type": "keyword"
      }
    }
  }
}
		

重索引数据

				POST /_reindex
					{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001"
  }
}
		

不能 直接重命名字段。相反,请使用 field alias(字段别名)

创建一个包含 user_identifier 字段的索引

				PUT /my-index-000001
					{
  "mappings": {
    "properties": {
      "user_identifier": {
        "type": "keyword"
      }
    }
  }
}
		

添加 user_id 别名

				PUT /my-index-000001/_mapping
					{
  "properties": {
    "user_id": {
      "type": "alias",
      "path": "user_identifier"
    }
  }
}
		
© . 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.