Elasticsearch: Index template

Index template定义在创建新index时可以自动应用的settings和mappings。 Elasticsearch根据与index名称匹配的index模式将模板应用于新索引。这个对于我们想创建的一系列的Index具有同样的settings及mappings。比如我们希望每一天/月的日志的index都具有同样的设置。

Index template仅在index创建期间应用。 对index template的更改不会影响现有索引。 create index API请求中指定的设置和映射会覆盖索引模板中指定的任何设置或映射。

你可以在代码中加入像C语言那样的block注释。你可以把这个注释放在出来开头 “{”和结尾的“}”之间的任何地方。

定义一个Index template

我们可以使用如下的接口来定义一个index template:

PUT /_template/<index-template>

我们可以使用_template这个终点来创建,删除,查看一个index template。下面,我们来举一个例子:

    PUT _template/logs_template
    {
      "index_patterns": "logs-*",
      "order": 1,
      "settings": {
        "number_of_shards": 4,
        "number_of_replicas": 1
      },
      "mappings": {
        "properties": {
          "@timestamp": {
            "type": "date"
          }
        }
      }
    }

在上面,我们可以看到,我们定义了一个叫做logs_template的index template。它的index_patterns定义为“logs-*”,说明,任何以“logs-”为开头的任何一个index将具有在该template里具有的settings及mappings属性。这里的“order”的意思是:如果索引与多个模板匹配,则Elasticsearch应用此模板的顺序。该值为1,表明有最先合并,如果有更高order的template,这个settings或mappings有可能被其它的template所覆盖。

下面,我们来测试一下我们刚定义的index template:

PUT logs-2019-03-01

在这里,我们创建了一个叫做logs-2019-03-01的index。我们使用如下的命令来检查被创建的情况:

GET logs-2019-03-01

显示的结果为:

    {
      "logs-2019-03-01" : {
        "aliases" : { },
        "mappings" : {
          "properties" : {
            "@timestamp" : {
              "type" : "date"
            }
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1567652671032",
            "number_of_shards" : "4",
            "number_of_replicas" : "1",
            "uuid" : "Dz5rqRS4SEyLM_gf5eEolQ",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "logs-2019-03-01"
          }
        }
      }
    }

证如上所示,我们已经成功创建了一个我们想要的index,并且它具有我们之前定义的settings及mappings。

Index template和alias

我们甚至可以为我们的index template添加index alias:

    PUT _template/logs_template
    {
      "index_patterns": "logs-*",
      "order": 1,
      "settings": {
        "number_of_shards": 4,
        "number_of_replicas": 1
      },
      "mappings": {
        "properties": {
          "@timestamp": {
            "type": "date"
          }
        }
      },
      "aliases": {
        "{index}-alias" : {}
      }
    }

在上面,我们已经创立了一个叫做{index}-alias的别名。这里的{index}就是实际生成index的文件名来代替。我们下面用一个例子来说明:

PUT logs-2019-04-01

我们创建一个叫做logs-2019-04-01的index, 那么它同时生成了一个叫做logs-2019-04-01-alias的别名。我们可以通过如下的命令来检查:

GET logs-2019-04-01-alias

显示的结果是:

    {
      "logs-2019-04-01" : {
        "aliases" : {
          "logs-2019-04-01-alias" : { }
        },
        "mappings" : {
          "properties" : {
            "@timestamp" : {
              "type" : "date"
            }
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1567653644605",
            "number_of_shards" : "4",
            "number_of_replicas" : "1",
            "uuid" : "iLf-j_G2T4CYcHCqwz32Ng",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "logs-2019-04-01"
          }
        }
      }
    }

Index匹配多个template

多个索引模板可能与索引匹配,在这种情况下,设置和映射都合并到索引的最终配置中。 可以使用order参数控制合并的顺序,首先应用较低的顺序,并且覆盖它们的较高顺序。 例如:

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

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

以上的template_1将禁用存储_source,但对于以te *开头的索引,仍将启用_source。 注意,对于映射,合并是“深度”的,这意味着可以在高阶模板上轻松添加/覆盖特定的基于对象/属性的映射,而较低阶模板提供基础。

我们可以来创建一个例子看看:

    PUT test10

    GET test10

显示的结果是:

    {
      "test10" : {
        "aliases" : { },
        "mappings" : { },
        "settings" : {
          "index" : {
            "creation_date" : "1567654333181",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "iEwaQFl9RAKyTt79PduN-Q",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "test10"
          }
        }
      }
    }

如果我们创建另外一个不是以 “te”开头的index,我们可以看看如下的情况:

    PUT my_test_index
    GET my_test_index

显示的结果是:

    {
      "my_test_index" : {
        "aliases" : { },
        "mappings" : {
          "_source" : {
            "enabled" : false
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1567654713059",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "aSsIZMT2RyWKT44G2dF2zg",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "my_test_index"
          }
        }
      }
    }

显然在mappings里显示source是被禁止的。

如果对于两个templates来说,如果order是一样的话,我们可能陷于一种不可知论的合并状态。在实际的使用中必须避免。

查询Index template接口

我们可以通过如下的接口来查询已经被创建好的index template:

GET /_template/<index-template>

比如:

GET _template/logs_template

显示的结果是:

    {
      "logs_template" : {
        "order" : 1,
        "index_patterns" : [
          "logs-*"
        ],
        "settings" : {
          "index" : {
            "number_of_shards" : "4",
            "number_of_replicas" : "1"
          }
        },
        "mappings" : {
          "properties" : {
            "@timestamp" : {
              "type" : "date"
            }
          }
        },
        "aliases" : {
          "{index}-alias" : { }
        }
      }
    }

显示的内容就是我们之前已经创建的那个index template。

你也可以通过如下的方式来同时查询多个template的情况:

    GET /_template/template_1,template_2
    GET /_template/temp*
    GET /_template

删除一个index template

在之前的练习中,我们匹配“*”,也就是我们以后所有的创建的新的index将不存储source,这个显然不是我们所需要的。我们需要来把这个template进行删除。删除一个template的接口如下:

DELETE /_template/<index-template>

那么针对我们的情况,我们可以使用如下的命令来删除我们不需要的template:

    DELETE _template/template_1
    DELETE _template/template_2

这样我们删除了我们刚才创建的两个templates。

参考:
【1】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-get-template.html
【2】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-delete-template.html
【3】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-templates.html

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/12085103.html

时间: 2024-08-02 11:00:55

Elasticsearch: Index template的相关文章

Index Template和Dynamic Template

目录 什么是Index Template Index Template的工作方式 什么是Index Template Index Template - 帮助你设定Mappings 和 Settings,并按照一定的规则,自动匹配到新创建的索引之上 模板仅在一个索引被新创建时,才会产生作用.修改模板不会影响到自己创建的索引 可以设定多个模板,这些设置会被"merge"在一起 可以指定"order"的数值,控制"mergine"的过程 Index T

Elasticsearch Index API & Aggregations API & Query DSL

这篇小菜给大家演示和讲解一些Elasticsearch的API,如在工作中用到时,方便查阅. 一.Index API 创建索引库 curl -XPUT 'http://127.0.0.1:9200/test_index/' -d '{     "settings" : {       "index" : {       "number_of_shards" : 3,       "number_of_replicas" : 1

elasticsearch index 之 create index(二)

创建索引需要创建索引并且更新集群index matedata,这一过程在MetaDataCreateIndexService的createIndex方法中完成.这里会提交一个高优先级,AckedClusterStateUpdateTask类型的task.索引创建需要即时得到反馈,异常这个task需要返回,会超时,而且这个任务的优先级也非常高.下面具体看一下它的execute方法,这个方法会在master执行任务时调用,这个方法非常长,主要完成以下三个功能:更新合并request,template

elasticsearch index 之 put mapping

mapping机制使得elasticsearch索引数据变的更加灵活,近乎于no schema.mapping可以在建立索引时设置,也可以在后期设置.后期设置可以是修改mapping(无法对已有的field属性进行修改,一般来说只是增加新的field)或者对没有mapping的索引设置mapping.put mapping操作必须是master节点来完成,因为它涉及到集群matedata的修改,同时它跟index和type密切相关.修改只是针对特定index的特定type. 在Action su

elasticsearch index 之merge

merge是lucene的底层机制,merge过程会将index中的segment进行合并,生成更大的segment,提高搜索效率.segment是lucene索引的一种存储结构,每个segment都是一部分数据的完整索引,它是lucene每次flush或merge时候形成.每次flush就是将内存中的索引写出一个独立segment的过程.所以随着数据的不断增加,会形成越来越多的segment.因为segment是不可变的,删除操作不会改变segment内部数据,只是会在另外的地方记录某些数据删

ElasticSearch Index API &amp;&amp; Mapping

ElasticSearch  NEST Client 操作Index var indexName="twitter"; var deleteIndexResponse = client.DeleteIndex(indexName);                var createIndexResponse = client.CreateIndex(indexName);                var getIndexResponse = client.GetIndex(in

elasticsearch index 之 Mapping

Lucene索引的一个特点就filed,索引以field组合.这一特点为索引和搜索提供了很大的灵活性.elasticsearch则在Lucene的基础上更近一步,它可以是 no scheme.实现这一功能的秘密就Mapping.Mapping是对索引各个字段的一种预设,包括索引与分词方式,是否存储等,数据根据字段名在Mapping中找到对应的配置,建立索引.这里将对Mapping的实现结构简单分析,Mapping的放置.更新.应用会在后面的索引fenx中进行说明. 首先看一下Mapping的实现

elasticsearch index 功能源码概述

从本篇开始,对elasticsearch的介绍将进入数据功能部分(index),这一部分包括索引的创建,管理,数据索引及搜索等相关功能.对于这一部分的介绍,首先对各个功能模块的分析,然后详细分析数据索引和搜索的整个流程. 这一部分从代码包结构上可以分为:index, indices及lucene(common)几个部分.index包中的代码主要是各个功能对应于lucene的底层操作,它们的操作对象是index的shard,是elasticsearch对lucene各个功能的扩展和封装.indic

elasticsearch index 之 create index(-)

从本篇开始,就进入了Index的核心代码部分.这里首先分析一下索引的创建过程.elasticsearch中的索引是多个分片的集合,它只是逻辑上的索引,并不具备实际的索引功能,所有对数据的操作最终还是由每个分片完成.创建索引的过程,从elasticsearch集群上来说就是写入索引元数据的过程,这一操作只能在master节点上完成.这是一个阻塞式动作,在加上分配在集群上均衡的过程也非常耗时,因此在一次创建大量索引的过程master节点会出现单点性能瓶颈,能够看到响应过程很慢. 在开始具体源码分析之