字段映射规则:
JSON datatype | Elasticsearch datatype |
null | No field is added. |
true or false | boolean field |
floating point number | float field |
integer | long field |
object | object field |
array | Depends on the first non-null value in the array. |
string | Either a date field (if the value passes date detection), a double or longfield (if the value passes numeric detection) or a text field, with a keyword sub-field. |
日期监测:
如果date_detection 开启了
The default value for dynamic_date_formats is:
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
Dynamic templates:
能够识别的类型:
- boolean: when true or false are encountered.
- date: when date detection is enabled and a string is found that matches any of the configured date formats.
- double :for numbers with a decimal part.
- long :for numbers without a decimal part.
- object :for objects, also called hashes.
- String: for character strings.
仅当字段包含具体值(非空或空数组)时,才会添加动态字段映射。 这意味着如果在dynamic_template中使用了null_value选项,则只会在具有该字段的具体值的第一个文档被索引之后应用它。
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
]
}
}
}
PUT my_index/_doc/1
{
"my_integer": 5,
"my_string": "Some string"
}
PUT my_index/_doc/2
{
"my_a": null
}
my_a 为空值 所以并没有定义字段属性。
match and unmatch
match参数使用模式匹配字段名称,而unmatch使用模式排除匹配匹配的字段。
以下示例匹配名称以long_开头的所有字符串字段(以_text结尾的字符串除外)并将它们映射为长字段:
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"longs_as_strings": {
"match_mapping_type": "string",
"match": "long_*",
"unmatch": "*_text",
"mapping": {
"type": "long"
}
}
}
]
}
}
}
PUT my_index/_doc/1
{
"long_num": "5",
"long_text": "foo"
}
match_pattern:
match_pattern参数调整match参数的行为,以便它支持字段名称上的完整Java正则表达式匹配,而不是简单的通配符,例如:
"match_pattern": "regex",
"match": "^profit_\d+$"
path_match and path_unmatch:
path_match和path_unmatch参数的工作方式与匹配和不匹配相同,但是在字段的完整虚线路径上操作,而不仅仅是最终名称,例如,some_object.*.some_field。
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}
}
PUT my_index/_doc/1
{
"name": {
"first": "Alice",
"middle": "Mary",
"last": "White"
}
}
{name}和{dynamic_type}占位符将在映射中替换为字段名称和检测到的动态类型。 以下示例将所有字符串字段设置为使用与该字段同名的分析器,并为所有非字符串字段禁用doc_values:
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"named_analyzers": {
"match_mapping_type": "string",
"match": "*",
"mapping": {
"type": "text",
"analyzer": "{name}"
}
}
},
{
"no_doc_values": {
"match_mapping_type":"*",
"mapping": {
"type": "{dynamic_type}",
"doc_values": false
}
}
}
]
}
}
}
PUT my_index/_doc/1
{
"english": "Some English text",
"count": 5
}
| The english field is mapped as a string field with the english analyzer. |
| The count field is mapped as a long field with doc_values disabled. |