ES自定义索引模板

1.官方网站

1.1模板只在创建索引时应用。更改模板对现有索引没有影响。

1.2使用create index API的优先级高于模板的优先级。

2.具体实例:

PUT /_template/template_1
{
  "template": "te*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}

定义一个名为template_1的模板,模板模式为te*。设置和映射将应用于任何与te*模板匹配的索引名。

验证:

1.创建1个索引名称为test:

PUT /test

2.获取test,创建索引模板成功。
GET /test

{
  "test" : {
    "aliases" : { },
    "mappings" : {
      "type1" : {
        "_source" : {
          "enabled" : false
        },
        "properties" : {
          "created_at" : {
            "type" : "date",
            "format" : "EEE MMM dd HH:mm:ss Z YYYY"
          },
          "host_name" : {
            "type" : "keyword"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1575360509771",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "L7tWIlj7Ty6VcKC7JdvLiA",
        "version" : {
          "created" : "6060099"
        },
        "provided_name" : "test"
      }
    }
  }
}

3.创建含有别名的索引模板

PUT _template/template_2
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "aliases" : {
        "alias1" : {},
        "alias2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        },
        "{index}-alias" : {} 
    }
}

{index}占位符,将被替换为模板在创建索引时应用到的实际索引名

4.删除索引模板:

delete  /_template/template_1

5.获取所有的索引模板

GET /_template

5.2.获取索引模板通配符匹配

GET /_template/te*

6.索引模板是否存在

HEAD /_template/template_1

7.多个索引模板可能匹配一个索引,在这种情况下,设置和映射都合并到索引的最终配置中。可以使用order参数控制合并的顺序,先应用较低的顺序,然后用较高的顺序覆盖它们。相同的order值,

则合并的结果不确定。

PUT /_template/template_1
{
    "index_patterns" : ["*"],
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}

PUT /_template/template_2
{
    "index_patterns" : ["te*"],
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : true }
        }
    }
}

比如test索引的_source仍然是启用的,后面覆盖前面,这样做的机制是高阶模板上添加/覆盖,而低阶模板提供了基础。

8.为模板添加版本号:

PUT /_template/template_1
{
    "index_patterns" : ["*"],
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "version": 123
}

9.获取模板的版本号:

GET /_template/template_1?filter_path=*.version

 

猜你喜欢

转载自www.cnblogs.com/glblog/p/11976518.html
今日推荐