ElasticSearch 动态模板 使用方法原创
  # ElasticSearch 动态模板 使用方法
# 1. 什么是动态模板
动态模板(Dynamic Templates)是 Elasticsearch 中的一种功能,用于在文档插入时根据字段的名称、数据类型或路径自动应用特定的映射。它允许用户在不预先定义字段映射的情况下,自动将新字段映射为特定的类型或配置。
# 2. 动态模板的作用
动态模板主要有以下作用:
- 简化索引创建:无需提前定义所有字段,可以动态生成映射。
 - 提高灵活性:可以根据字段名、数据类型等条件灵活应用不同的映射规则。
 - 自动管理字段:自动设置字段类型、分词器等属性。
 
# 3. 动态模板的使用步骤
# 3.1 创建索引并定义动态模板
在创建索引时,可以在 mappings 部分定义 dynamic_templates,例如:
PUT /my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      },
      {
        "long_fields": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "long"
          }
        }
      },
      {
        "custom_template": {
          "match": "custom_*",
          "mapping": {
            "type": "text",
            "analyzer": "custom_analyzer"
          }
        }
      }
    ]
  }
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
在这个示例中,我们定义了三个动态模板:
- strings_as_keywords:将所有字符串类型的字段映射为 
keyword类型。 - long_fields:将所有 
long类型的字段映射为long类型。 - custom_template:将所有字段名匹配 
custom_*的字段映射为text类型,并使用custom_analyzer进行分析。 
# 3.2 更新索引的动态模板
如果需要更新已有索引的动态模板,可以使用 PUT 请求更新映射:
PUT /my_index/_mapping
{
  "dynamic_templates": [
    {
      "updated_template": {
        "match": "new_field_*",
        "mapping": {
          "type": "text",
          "analyzer": "new_analyzer"
        }
      }
    }
  ]
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3.3 插入文档并验证
插入文档后,Elasticsearch 会根据动态模板自动应用映射:
POST /my_index/_doc
{
  "name": "John",
  "age": 30,
  "custom_field": "some text",
  "new_field_example": "another text"
}
 1
2
3
4
5
6
7
2
3
4
5
6
7
使用 GET 请求查看映射:
GET /my_index/_mapping
 1
可以看到动态模板已经被应用到相应的字段上。
# 4. 动态模板的匹配规则
动态模板的匹配规则主要包括以下几种:
- match:根据字段名进行匹配,可以使用通配符。
 - unmatch:排除特定字段名。
 - match_mapping_type:根据字段的数据类型进行匹配。
 - path_match:根据字段的路径进行匹配。
 - path_unmatch:排除特定路径的字段。
 
# 5. 动态模板的优先级
当多个动态模板可以应用于同一字段时,Elasticsearch 按照定义顺序应用第一个匹配的模板。因此,定义动态模板时需要注意顺序,以确保正确的模板被应用。
# 6. 示例代码
以下是一个完整的示例代码,展示了如何使用动态模板:
PUT /my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      },
      {
        "long_fields": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "long"
          }
        }
      },
      {
        "custom_template": {
          "match": "custom_*",
          "mapping": {
            "type": "text",
            "analyzer": "custom_analyzer"
          }
        }
      }
    ]
  }
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
插入文档并验证:
POST /my_index/_doc
{
  "name": "John",
  "age": 30,
  "custom_field": "some text"
}
 1
2
3
4
5
6
2
3
4
5
6
查询映射:
GET /my_index/_mapping
 1
上次更新: 6/21/2025