Dynamic(动态映射)
dynamic 参数指定新检测到的字段是否可以动态添加到映射中。它接受下表中列出的参数。
| 参数 | 描述 |
|---|---|
true |
指定新字段可以动态添加到映射中。默认值为 true。 |
false |
指定新字段不能动态添加到映射中。如果检测到新字段,则不会索引或可搜索,但可以从 _source 字段中检索。 |
strict |
抛出异常。检测到新字段时,索引操作会失败。 |
strict_allow_templates |
如果新字段匹配映射中预定义的动态模板,则添加新字段。 |
示例:创建 dynamic 设为 true 的索引
通过发送以下请求,创建一个
dynamic设为true的索引:
PUT testindex1
{
"mappings": {
"dynamic": true
}
}
通过发送以下请求,索引一个包含对象字段
patient的文档,其中包含两个字符串字段:
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
}
}
通过发送以下请求,确认映射按预期工作:
GET testindex1/_mapping
对象字段 patient 及其两个子字段 name 和 id 已添加到映射中,如下响应所示:
{
"testindex1": {
"mappings": {
"dynamic": "true",
"properties": {
"patient": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
示例:创建 dynamic 设为 false 的索引
通过发送以下请求,创建一个具有显式映射且
dynamic设为false的索引:
PUT testindex1
{
"mappings": {
"dynamic": false,
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
通过发送以下请求,索引一个包含对象字段
patient的文档,其中包含两个字符串字段以及额外的未映射字段:
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}
通过发送以下请求,确认映射按预期工作:
GET testindex1/_mapping
以下响应显示,新字段 room 和 floor 未被添加到映射中,映射保持不变:
{
"testindex1": {
"mappings": {
"dynamic": "false",
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
}
通过发送以下请求,从文档中获取未映射的字段
room和floor:
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}
以下请求搜索字段 room 和 floor:
POST testindex1/_search
{
"query": {
"term": {
"room": "room1"
}
}
}
响应未返回任何结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
示例:创建 dynamic 设为 strict 的索引
通过发送以下请求,创建一个具有显式映射且
dynamic设为strict的索引:
PUT testindex1
{
"mappings": {
"dynamic": "strict",
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
通过发送以下请求,索引一个包含对象字段
patient的文档,其中包含两个字符串字段以及额外的未映射字段:
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}
注意,会抛出异常,如下响应所示:
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [room] within [_doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [room] within [_doc] is not allowed"
},
"status": 400
}
示例:创建 dynamic 设为 strict_allow_templates 的索引
通过发送以下请求,创建一个具有预定义动态模板且
dynamic设为strict_allow_templates的索引:
PUT testindex1
{
"mappings": {
"dynamic": "strict_allow_templates",
"dynamic_templates": [
{
"strings": {
"match": "room*",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
通过发送以下请求,索引一个包含对象字段
patient的文档,其中包含两个字符串字段以及一个新字段room,该字段匹配其中一个动态模板:
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1"
}
索引成功,因为新字段 room 匹配了动态模板。然而,对于新字段 floor,索引会失败,因为它不匹配任何动态模板且未显式映射,如下响应所示:
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}