Elasticsearch: Index template

settings and define mappings Index template when creating a new index can be automatically applied. Elasticsearch according to the index matches the name of the index mode apply a template to a new index. For this series we want to create the Index has the same settings and mappings. For example, we hope every day index log / month have the same settings.

Index template created only applied during the index. Index template changes will not affect the existing index. create index API specified in the request and the mapping settings override any settings specified index or mapping template.

You can add comments as block like C language code. You can put out at the beginning of this comment on "{" and ends with "}" anywhere in between.

Index template definition of a

We can define an index template uses the following interfaces:

PUT /_template/<index-template>

We can use _template this end to create, delete, view an index template. Below, we give an example:

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

In the above, we can see that we define the index template called logs_template of. It index_patterns defined as "logs- *", explained, any any index to "logs-" is beginning to have have the template in the settings and mappings property. Here the "order" means: if the index with multiple templates match, Elasticsearch the order in this template. A value of 1 indicates that there are first combined, if there is a higher order of the template, the settings or possible mappings are covered by the other template.

Here, we have to test index template we just defined:

PUT logs-2019-03-01

Here, we have created index called the logs-2019-03-01. We use the following command to check the situation was created:

GET logs-2019-03-01

The results showed as follows:

    {
      "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"
          }
        }
      }
    }

Card shown above, we have successfully created a index we want, and it has settings and mappings we defined earlier.

Index template和alias

We can even add to our index alias 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"
          }
        }
      },
      "aliases": {
        "{index}-alias" : {}
      }
    }

In the above, we've created a {index} -alias alias called. Here {index} is the actual name of the generated index file instead. Here we use an example to illustrate:

PUT logs-2019-04-01

We create an index logs-2019-04-01 is called, it simultaneously generates an alias called logs-2019-04-01-alias of. We can be checked by the following command:

GET logs-2019-04-01-alias

The results show that:

    {
      "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 to match multiple template

A plurality of templates may index matches the index, in this case, is provided and mapping are merged into a final configuration index. Sequence may be combined using the order parameter control, the first application of a lower order and higher-order covering them. E.g:

    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 }
        }
    }

More template_1 disabled storage _source, but with te *Index beginning, will enable _source. Note that for the mapping, the merger is "depth", which means that you can easily add on high-templates / coverage based on object / attribute mapping specific, and provide the basis for lower-order template.

We can create a look at an example:

    PUT test10
     
    GET test10

The results show that:

    {
      "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"
          }
        }
      }
    }

If we are not to create another "te" at the beginning of the index, we can look at the situation as follows:

    PUT my_test_index
    GET my_test_index

The results show that:

    {
      "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"
          }
        }
      }
    }

Clearly display source is prohibited in the mappings.

If two templates for, if the order is the same, we may be caught in a state of consolidation agnostic. It must be avoided in actual use.

Query Index template interfaces

We can query a good index template has been created by the following interfaces:

GET /_template/<index-template>

such as:

GET _template/logs_template

The results show that:

    {
      "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" : { }
        }
      }
    }

What is displayed is the index template we have previously created.

You can also check on the status simultaneously by multiple template of the following ways:

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

To delete a index template

In the previous exercise, we match "*", that is, after we create a new index will not all storage source, this is obviously not what we need. We need to put this template to delete. To delete a template interface as follows:

DELETE /_template/<index-template>

So for our case, we can use the following command to delete our unwanted template:

    DELETE _template/template_1
    DELETE _template/template_2

We removed the two templates that we just created.

参考:
【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

Guess you like

Origin www.cnblogs.com/sanduzxcvbnm/p/12085103.html