ElasticSearch 索引模板

索引模板

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

索引配置

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

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

相关设置详见Index Modules

模板操作

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

ElasticSearch 索引模板

创建模板

REST API

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

curl -XGET localhost:9200/_template/template_name

Dev Tools

GET _template/template_name

删除模板

REST API

curl -XDELETE localhost:9200/_template/template_name

Dev Tools

DELETE _template/template_name

索引模板示例

  • template为模板匹配规则
  • order为模板优先级(当索引同时匹配多个模板,先应用数值小的模板,再以数值大的模板配置进行覆盖)
  • properties中指定特定类型,否则则会自动匹配为dynamic_templates中的textkeyword类型
{
  "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" : { }
  }
}