Elasticsearch Index templates allow you to define templates that will automatically be applied on index creation time. The templates can include both settings and mappings..
What are we doing?
We want to create a template on how we would a target index to look like. It should consist of 1 primary shard and 2 replica shards and we want to update the mapping that we can make use of text and keyword string fields.
So then whenever we create an index which matches our template, the template will be applied on index creation.
String Fields
We will make use of the following string fields in our mappings which will be included in our templates:
Text:
A field to index full-text values, such as the body of an email or the description of a product. These fields are analyzed, that is they are passed through an analyzer to convert the string into a list of individual terms before being indexed. The analysis process allows Elasticsearch to search for individual words within each full text field
Keyword":
A field to index structured content such as email addresses, hostnames, status codes, zip codes or tags.
They are typically used for filtering (Find me all blog posts where status is published), for sorting, and for aggregations. Keyword fields are only searchable by their exact value
Note about templates:
Couple of things to keep in mind:
12
1. Templates gets referenced on index creation and does not affect existing indexes
2. When you update a template, you need to specify the exact template, the payload overwrites the whole template
View your current templates in your cluster:
1234
$ curl -XGET http://localhost:9200/_cat/templates?v
name index_patterns order version
.monitoring-kibana [.monitoring-kibana-6-*] 0 6020099
filebeat-6.3.1 [filebeat-6.3.1-*] 1
Create the template foobar_docs which will match any indexes matching foo-* and bar-* which will inherit index settings of 1 primary shards and 2 replica shards and also apply a mapping template shown below:
$ curl -XGET http://localhost:9200/_cat/indices/test-2018.07.20?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open test-2018.07.20 -5XOfl0GTEGeHycTwL51vQ 5 1 0 0 2kb 1.1kb
We can also inspect the template like shown below:
View our current index, as you can see the index is unaffected of the template change as only new indexes will retrieve the update of the template:
123
$ curl -XGET http://localhost:9200/_cat/indices/foo-2018.07.20?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open foo-2018.07.20 ol1pGugrQCKd0xES4R6oFg 1 2 2 0 20.4kb 10.2kb
Create a new index to verify that the template’s config is pulled into the new index:
View the elasticsearch indexes to verify the behavior:
1234
$ curl -XGET http://localhost:9200/_cat/indices/foo-2018.07.*?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open foo-2018.07.20 ol1pGugrQCKd0xES4R6oFg 1 2 2 0 20.4kb 10.2kb
green open foo-2018.07.20-new g6Ii8jtKRFa1zDVB2IsDBQ 2 1 0 0 920b 460b