es模板

Index Templatesedit

Index templates allow you to define templates that will automatically be applied to new indices created. The templates include both settings and mappings, and a simple pattern template that controls if the template will be applied to the index created. For example:

curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'

Defines a template named template_1, with a template pattern of te*. The settings and mappings will be applied to any index name that matches the te* template.

It is also possible to include aliases in an index template as follows:

curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    },
    "aliases" : {
        "alias1" : {},
        "alias2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        },
        "{index}-alias" : {} 
    }
}
'

the {index} placeholder within the alias name will be replaced with the actual index name that the template gets applied to during index creation.

Deleting a Templateedit

Index templates are identified by a name (in the above case template_1) and can be deleted as well:

curl -XDELETE localhost:9200/_template/template_1

Getting templatesedit

Index templates are identified by a name (in the above case template_1) and can be retrieved using the following:

curl -XGET localhost:9200/_template/template_1

You can also match several templates by using wildcards like:

curl -XGET localhost:9200/_template/temp*
curl -XGET localhost:9200/_template/template_1,template_2

To get list of all index templates you can run:

curl -XGET localhost:9200/_template/

Templates existsedit

Used to check if the template exists or not. For example:

curl -XHEAD -i localhost:9200/_template/template_1

The HTTP status code indicates if the template with the given name exists or not. A status code 200means it exists, a 404 it does not.

Multiple Template Matchingedit

Multiple index templates can potentially match an index, in this case, both the settings and mappings are merged into the final configuration of the index. The order of the merging can be controlled using the order parameter, with lower order being applied first, and higher orders overriding them. For example:

curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'

curl -XPUT localhost:9200/_template/template_2 -d '
{
    "template" : "te*",
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : true }
        }
    }
}
'

The above will disable storing the _source on all type1 types, but for indices of that start with te*, source will still be enabled. Note, for mappings, the merging is "deep", meaning that specific object/property based mappings can be added/overridden on higher order templates, with lower order templates providing the basis.

参考官网

tips:有时候修改了配置文件参数,也修改了线上参数还是出现报错,有可能是模板没改,导致每次新生成索引的时候报错,比如:单机的时候

number_of_replicas不为0,生成的索引就会报错。

查看模板:curl -XGET 'http://10.0.120.39:9200/_template/webstat_download_tmp'

原文地址:https://www.cnblogs.com/mikeluwen/p/8031760.html