Elasticsearch index template and dynamic mapping template (4)

One, dynamic mapping

Earlier we introduced mapping-related attributes. Careful friends may find that when we first started using ES, we might not know much about mapping, nor did we add mapping, why we can still add documents normally.

That's because ES can be dynamically mapped, and fields that are not encountered when adding documents can be dynamically added to the mapping. The following are some default mapping methods.

Value If missing, add type in mapping
null Null values ​​do not add the type to the mapping
true、false Add a boolean type
Floating point Add a floating type
Integer Add a long type
object Add an object type
array Determine the type based on the first element of the array
String May be mapped to date, double, long, text, keyword types

note:

  1. The number type is mapped to long instead of integer by default
  2. Strings can be mapped into multiple types, mainly depending on the content
  3. We can customize the format of string mapping to date
"mappings": {
    
    
    "date_detection": true,
    "dynamic_date_formats": ["MM/dd/yyyy"],
    "numeric_detection": true
  }

Two, custom dynamic mapping

If you feel that the default mapping method of ES has room for optimization for your own business, we can customize the mapping method in a custom dynamic mapping method.

For example, for the integer type field I want to query accurately, then we can set its index to false, if we do not want to participate in the score of a nearby description string, we can set its norms to false.

Some content we don't want to be searched at all, then we can set its enable setting to false. If we don't want to sort and other aggregation operations on a status field, we can set doc_values ​​to false.

Next we will look at 3 ways to add dynamic mapping.

2.1 Mapping according to field type

Matching according to the field type is controlled by the match_mapping_type attribute. When adding a document, the type parsed by ES's JsonParser is the type specified by match_mapping_type, and the corresponding mapping will be used.

Data type mapping

{
    
    
    "mappings": {
    
    
        "dynamic_templates": [
            {
    
    
                "dt_name_one": {
    
    
                    "match_mapping_type": "long",
                    "mapping": {
    
    
                        "type": "integer"
                    }
                }
            },
            {
    
    
                "dt_name_two": {
    
    
                    "match_mapping_type": "string",
                    "mapping": {
    
    
                        "type": "text",
                        "fields": {
    
    
                            "raw": {
    
    
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        ]
    }
}

2.2 Mapping based on field names

According to the field name mapping, match, unmatch, and match_pattern attributes are used to control.

Field name mapping match

{
    
    
    "mappings": {
    
    
        "dynamic_templates": [
            {
    
    
                "longs_as_strings": {
    
    
                    "match_mapping_type": "string",
                    "match": "long_*",
                    "unmatch": "*_text",
                    "mapping": {
    
    
                        "type": "long",
                        "enable": false
                    }
                }
            },
            {
    
    
                "longs_as_strings": {
    
    
                    "match_pattern": "regex",
                    "match": "^profit_\\d+$",
                    "mapping": {
    
    
                        "type": "long",
                        "index": false,
                        "norms": false,
                        "doc_values": false
                    }
                }
            }
        ]
    }
}

2.3 Mapping according to the field path

The path matching is specified using the path_match and path_unmatch attributes.

Path map matching

{
    
    
    "mappings": {
    
    
        "dynamic_templates": [
            {
    
    
                "dt_path_name": {
    
    
                    "path_unmatch": "user.*",
                    "path_match": "*.conceal",
                    "mapping": {
    
    
                        "type": "text",
                        "enable": false
                    }
                }
            }
        ]
    }
}

3. How to add dynamic mapping

There are two ways to add a dynamic index: the
first is to specify the dynamic mapping mapping when adding a mapping to the index.
The second is to specify in the index template.

We have already looked at the specification when adding index mapping, let's look at the specification when setting the index template.

Four, index template

{
    
    
  "order": 0,                            // 模板优先级,数字越大优先级越高
  "index_patterns": ["test*,user*"],     // 匹配该模板的索引名称正则
  "settings": {
    
    },                        // 索引设置
  "mappings": {
    
    },                        // 索引中各字段的映射定义
  "aliases": {
    
    }                          // 索引的别名
}

As shown above, it is the most common attribute in the index template:
order: When the index name matches multiple index templates, change to select
index_patterns with a larger order value : index name matching expression, for example, the name used is test The beginning can be matched to test*
settings: index configuration
mappings: default mapping

ES index template

{
    
    
    "order": 1,
    "index_patterns": [
        "test*",
        "user*"
    ],
    "settings": {
    
    
        "number_of_shards": 1
    },
    "mappings": {
    
    
        "_source": {
    
    
            "enabled": false
        },
        "dynamic_templates": [
            {
    
    
                "string_as_keyword": {
    
    
                    "match_mapping_type": "long",
                    "mapping": {
    
    
                        "type": "integer"
                    }
                }
            }
        ],
        "properties": {
    
    
            "host_name": {
    
    
                "type": "keyword"
            },
            "created_at": {
    
    
                "type": "date",
                "format": "yyyy-MM-dd"
            }
        }
    },
    "aliases": {
    
    
        "alias_name": {
    
    
            "filter": {
    
    
                "term": {
    
    
                    "user": "tim"
                }
            },
            "routing": "tim"
        }
    }
}
PUT /_template/template_name

Five, documentation

Dynamic mapping template

Index template

Guess you like

Origin blog.csdn.net/trayvontang/article/details/103551183