We Have two concepts mapping and template in ES. I read the official explanation about these two concepts. I am still confused about it.
Templates
and mappings
are two different things. And to make it a little more confusing, the phrase template is used in two different but related features.
Mapping
The mapping of an index is the schema definition of all of the data store into that one index. Think of it as what is really there.
Index Templates
Index Templates
are used to make it easier to define mappings
for new indices. If you are going to define a lot of indices that share some common mappings or settings, you can define an Index Template
with those shared settings and have it apply them for you automatically. In your template you tell ElasticSearch what indices it can apply those templates to.
Index Templates Reference
dynamic_templates in a Mapping
For completeness, there is another place templating shows up in ElasticSearch. When you declare a mapping for a new index you can also define a dynamic_templates
section. This allows you to have ElasticSearch define field mappings based upon a new fields type or name. For example you could create an entry that says any field whose name ends with "_date" should be treated as a date field. These let you define your fields by a convention instead of needing to explicitly specify their field mappings for every one.
Dynamic Templates Reference
It's worth noting that for both Index Templates
and the dynamic_templates
section, the values are only applied when the index/field is first created. In your mapping
definitions, if you explicitly specify something different than the template it will use your value instead.
So, back to your questions:
- In the template, we can define mapping. In this case, why we need mapping?
Index Templates
are useful for applying a mapping to multiple indices as they are created. If you aren't creating multiple identical indices, you will probably just define your mapping manually.
The dynamic_templates
in an index definition is useful for defining field types by naming convention. Any changes to this will only affect new fields added to the index.
- If I wanna update mapping, can I just modify it in template?
No, once an index is created, updates to the Index Templates
will not affect that index. They are only applied to new indices. Also, updated to dynamic_templates
will only affect new fields in an index.
The Mappings are what a current index has. These can be set and updated on an index.
Templates are used to create mappings for new indexes as they are created. From what I know of them, they both come from the same JSON payload just different ES endpoints.
So If you apply mappings but not templates, tomorrows index most likely will have yesterdays mappings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With