Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between mapping and template in Elasticsearch?

We Have two concepts mapping and template in ES. I read the official explanation about these two concepts. I am still confused about it.

  1. In the template, we can define mapping. In this case, why we need mapping?
  2. If I wanna update mapping, can I just modify it in template?
like image 892
Gabriel Avatar asked Oct 15 '18 19:10

Gabriel


2 Answers

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:

  1. 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.

  1. 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.

like image 182
Ryan Widmaier Avatar answered Sep 28 '22 01:09

Ryan Widmaier


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.

like image 23
Phatjam98 Avatar answered Sep 28 '22 02:09

Phatjam98