根据dynamic_templates
中的顺序有优先级,但是numeric_detection
和dynamic_templates
之间是否有优先级?
模板按顺序处理 — 第一个匹配的模板获胜。
可以肯定地说dynamic_templates
的优先级低于(或更高)numeric_detection
吗?
numeric_detection
不是模板。这是field mapping
.当您指定多个templates
时,将按顺序处理它们。
我尝试了以下练习来确认numeric detection
和dynamic templates
之间的优先级。
映射:
{
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "short"
}
}
}
],
"numeric_detection": true
}
如果它检测到一个数字,它有两个选项。一个设置为long
,通过启用numeric_Detection
说,另一个如果long
将其设置为short
,则由dynamic_templates
处理。
然后索引以下数据
{
"whoami":"25"
}
然后我检查了映射。我找到了这个。
"properties": {
"whoami": {
"type": "short"
}
}
然后,我尝试禁用dynamic field
映射。
{
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "short"
}
}
}
],
"numeric_detection": false
}
这一次,whoami
存储为text
.
所以优先级如下。
- 动态字段映射 - 最高
- 动态模板映射 - 最低
dynamic field
标识必须应用的规则。一旦规则被dynamic field mapping
应用,那么自定义规则就会被应用dynamic template mapping
感谢 Gibbs 的帮助,我多次阅读了该文档并测试了更多演示。
我认为可以肯定地说,一旦指定了以下条件,dynamic templates
就会生效。
- Elasticsearch 检测到的数据类型,带有 match_mapping_type。
- 字段的名称,带有"匹配"和"不匹配"或"match_pattern"。
- 通往田野的完整虚线路径,带有path_match和path_unmatch。
虽然第一个条件让我感到困惑(吉布斯帮助消除了这种困惑),但"数据类型"应该是field mapping
之后的类型。
至于字段映射规则,我们可以在动态字段映射中检查详细信息。
日期字段(如果值通过
- 字符串
日期检测)、双精度或长字段(如果值通过数字检测)或带有关键字子字段的文本字段。
演示将为我们提供使用match
指定动态模板uid
keyword
:
GET my_index/_mapping
PUT my_index
{
"mappings": {
"numeric_detection": true,
"dynamic_templates": [
{
"strings_as_keywords": {
"match": "uid",
"mapping": {
"type": "keyword",
"ignore_above": 256
}
}
}
]
}
}
POST my_index/_doc/1
{
"uid": "123"
}