Elasticsearch template not working

I first write data directly to the specified index, and then create an index template,
After the index template is created, I continue to write data into the specified index. At this time, I write the new properties of the object, and I feel that the index template does not take effect for it.

{
  "order": 0,
  "index_patterns": [
    "jiankunking-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "6",
      "number_of_replicas": "1",
      "refresh_interval": "200ms"
    }
  },
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "type": "keyword"
          },
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "id": {
        "type": "keyword"
      },
      "attrs": {
        "type": "object"
      },
      "creator": {
        "type": "keyword"
      },
      "updater": {
        "type": "keyword"
      },
      "createdAt": {
        "format": "epoch_second",
        "type": "date"
      },
      "updatedAt": {
        "format": "epoch_second",
        "type": "date"
      }
    }
  }
}

mapping results

"attrDesc" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }

The index template is applied when the index is created, so I would recommend creating the template before creating the index.

Is it true that the index template does not take effect for new fields?

As per the docs

An index template is a way to tell Elasticsearch how to configure an index when it is created. For data streams, the index template configures the stream’s backing indices as they are created. Templates are configured prior to index creation. When an index is created - either manually or through indexing a document - the template settings are used as a basis for creating the index.

I do not think a newly added or changed template gets applied after creation, but could be wrong.

The index template is only applied on the creation of the index, if you create the index before creating the index template, it will not work.

If you create an index template, then create an index that matches the index pattern, then the template will be applied.

If you change a template after an index was created, these changes will only apply to new indices that matches the index pattern.

If I create a template first and then add data, why does the dynamic template take effect at this time? After all, the fields in the data added at the beginning are not necessarily complete?

Check the mappings of the index after it has been created and you will see the index template has been applied. including the dynamic section. This determines how future fields are mapped.

You are right, I looked at the mapping of the index and found that when the index was created, the mapping in the template was directly inherited to the mapping of its own index.

like this

{
	"mappings": {
		"dynamic_templates": [
			{
				"strings_as_keywords": {
					"unmatch": "log",
					"match_mapping_type": "string",
					"mapping": {
						"type": "keyword"
					}
				}
			}
		]
	}
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.