ElasticSearch 索引模板

template

Posted by Tillend on April 3, 2019

索引模板

索引模块是按索引创建的模块,用于控制所有与索引相关的方面。

索引配置

索引级别设置可以根据具体索引进行对应设置。相关设置为:

更改闭合索引上的静态或动态索引设置可能会导致不正确的设置,如果不删除并重新创建索引,则无法纠正这些设置。

相关设置详见Index Modules

模板操作

ES的模板CRUD可由ES REST APIKibana Dev Tools进行操作

创建模板

REST API

1
2
3
4
5
6
7
8
9
10
11
12
13
curl -XPUT localhost:9200/_template/template_name -d
{  
    "template" : "example-*",
    "order":1,
    "settings" : {  
        "number_of_shards" : 1  
    },  
    "mappings" : {  
        "type1" : {  
            "_source" : {"enabled" : false }  
        }  
    }  
}

Dev Tools

PUT _template/template_name
{  
    "template" : "example-*",
    "order":1,
    "settings" : {  
        "number_of_shards" : 1  
    },  
    "mappings" : {  
        "type1" : {  
            "_source" : {"enabled" : false }  
        }  
    }  
}

查看模板

REST API

1
curl -XGET localhost:9200/_template/template_name

Dev Tools

GET _template/template_name

删除模板

REST API

1
curl -XDELETE localhost:9200/_template/template_name

Dev Tools

DELETE _template/template_name

索引模板示例

  • template为模板匹配规则
  • order为模板优先级(当索引同时匹配多个模板,先应用数值小的模板,再以数值大的模板配置进行覆盖)
  • properties中指定特定类型,否则则会自动匹配为dynamic_templates中的textkeyword类型
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
  "kong_template" : {
    "order" : 1,
    "version" : 60001,
    "index_patterns" : [
      "kong-*"
    ],
    "settings" : {
      "index" : {
        "refresh_interval" : "5s"
      }
    },
    "mappings" : {
      "_default_" : {
        "dynamic_templates" : [
          {
            "message_field" : {
              "path_match" : "message",
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "text",
                "norms" : false
              }
            }
          },
          {
            "string_fields" : {
              "match" : "*",
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "text",
                "norms" : false,
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          }
        ],
        "properties" : {
          "@timestamp" : {
            "type" : "date"
          },
          "@version" : {
            "type" : "keyword"
          },
          "request.headers.remoteip" : {
            "type" : "ip"
          },
          "geoip" : {
            "dynamic" : true,
            "properties" : {
              "ip" : {
                "type" : "ip"
              },
              "location" : {
                "type" : "geo_point"
              },
              "latitude" : {
                "type" : "half_float"
              },
              "longitude" : {
                "type" : "half_float"
              }
            }
          }
        }
      }
    },
    "aliases" : { }
  }
}