Elasticsearch dynamic template

Keywords: Big Data ElasticSearch search engine

1, Dynamic mapping

When we first used ES, we probably didn't know much about mapping, and we didn't add mapping. Why can we add documents normally. That's because ES can dynamically map. When adding documents, fields that do not exist can be dynamically added to mapping. The following are some default mapping methods.

|Value | if missing, add type in mapping|
|Null | null values do not add types to mapping|
|true, false | add a boolean type|
|Floating point number | add a floating type|
|Integer | add a long type|
|Object | add an object type|
|Array | determines the type according to the first element of the array|
|String | may be mapped to date, double, long, text, keyword types|
be careful:

Number types are mapped to long instead of integer by default
Strings can be mapped into many types, mainly depending on the content
We can customize the format of string mapping to date

"mappings": {
    "date_detection": true,
    "dynamic_date_formats": ["MM/dd/yyyy"],
    "numeric_detection": true
  }
 

2, Custom dynamic mapping

If we think that the default mapping method of ES still has room for optimization for our business, we can customize the dynamic mapping method to customize the mapping method.

Three ways to customize

1. mapping according to field type

Match by field type_ mapping_ Type attribute. When adding a document, the type parsed by ES's JsonParser is match_mapping_type, the corresponding mapping will be used

{
    "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. mapping by field name

Match, unmatch and match are used for field name mapping_ The pattern attribute.

{
    "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
                    }
                }
            }
        ]
    }
}

3. mapping according to field path

Matching by path uses path_match,path_unmatch property.

{
    "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 mapping of dynamic mapping when adding mapping for the index.
The second is to specify in the index template.

We have already seen the specification when adding index mapping. Let's take a look at the specification when setting the index template.

4, Index template

{
  "order": 0,                            // Template priority. The higher the number, the higher the priority
  "index_patterns": ["test*,user*"],     // The name of the index that matches the template
  "settings": {},                        // Index settings
  "mappings": {},                        // Mapping definition of each field in the index
  "aliases": {}                          // Alias for index
}

As shown above, this is the most common attribute in the index template:
Order: when the index name matches multiple index templates, select the one with a larger order value
index_patterns: the index name matching expression. For example, all the expressions whose names start with test can be matched to test*
settings: index configuration
mappings: default mapping

 {
    "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"
        }
    }
}

Posted by cornelombaard on Sat, 20 Nov 2021 10:46:15 -0800