创建或更新映射
1.0 版引入
如果要创建或向索引添加映射和字段,可以使用 put mapping API 操作。对于现有映射,此操作会更新映射。
不能使用此操作更新已映射到索引中现有数据的映射。您必须首先使用所需的映射创建新索引,然后使用重新索引 API 操作将旧索引中的所有文档映射到新索引。如果希望在重新索引时避免停机,可以使用别名。
端点
PUT /<target-index>/_mapping
PUT /<target-index1>,<target-index2>/_mapping
路径参数
唯一必需的路径参数是与映射关联的索引。如果不指定索引,将收到错误。可以指定单个索引,也可以指定多个用逗号分隔的索引,如下所示
PUT /<target-index>/_mapping
PUT /<target-index1>,<target-index2>/_mapping
查询参数
(可选)您可以添加查询参数以发出更具体的请求。例如,要在响应中跳过任何缺失或已关闭的索引,可以将 ignore_unavailable
查询参数添加到请求中,如下所示
PUT /sample-index/_mapping?ignore_unavailable
下表定义了 put mapping 查询参数
参数 | 数据类型 | 描述 |
---|---|---|
allow_no_indices | 布尔型 | 是否忽略不匹配任何索引的通配符。默认值为 true 。 |
expand_wildcards | 字符串 | 将通配符表达式扩展到不同的索引。多个值用逗号分隔。可用值包括 all (匹配所有索引)、open (匹配开放索引)、closed (匹配已关闭索引)、hidden (匹配隐藏索引)和 none (不接受通配符表达式),其中 none 必须与 open 、closed 或两者结合使用。默认值为 open 。 |
ignore_unavailable | 布尔型 | 如果为 true,OpenSearch 不会在响应中包含缺失或关闭的索引。 |
cluster_manager_timeout | 时间 | 等待连接到集群管理器节点的时间。默认值为 30s 。 |
timeout | 时间 | 等待响应返回的时间。默认值为 30s 。 |
write_index_only | 布尔型 | OpenSearch 是否只将映射更新应用于写入索引。 |
请求正文字段
properties
请求正文必须包含 properties
,其中包含所有要创建或更新的映射。
{
"properties":{
"color":{
"type": "text"
},
"year":{
"type": "integer"
}
}
}
dynamic
通过将 dynamic
请求正文字段设置为 strict
,可以使文档结构与索引映射的结构匹配,如下例所示
{
"dynamic": "strict",
"properties":{
"color":{
"type": "text"
}
}
}
请求示例
以下请求为 sample-index
索引创建新映射
PUT /sample-index/_mapping
{
"properties": {
"age": {
"type": "integer"
},
"occupation":{
"type": "text"
}
}
}
示例响应
成功后,响应返回 "acknowledged": true
。
{
"acknowledged": true
}