Open source Elasticsearch does not use template in index creation

I am using an open source build of Elasticsearch, version 7.8.0.
I executed

curl -ivvk -H "Content-Type: application/json" -H "Accept: application/json" -u username:password -X PUT "https://localhost:9200/_template/test_template" -d '
{
  "index_patterns": ["test-*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "index.refresh_interval": "5s"
    },
    "mappings": {
      "properties": {
        "status": {
    	  "type": "keyword"
        },
	"message": {
		  "type": "text"
	}
      }
    }
  }
}'

HTTP/1.1 200 OK

{"acknowledged":true}

curl -ivvk -H "Accept: application/json" -u username:password -X GET "https://localhost:9200/_cat/templates"

HTTP/1.1 200 OK

[{"name":"test_template","index_patterns":"[test-*]","order":"0","version":null,"composed_of":""}]

I then created the index, test-1, with a deliberate attempt to violate the intended mapping of status to keyword, by assigning an integer value to it:

curl -ivvk -H "Content-Type: application/json" -H "Accept: application/json" -u username:password -X POST "https://localhost:9200/test-1/_create/1" -d '
{
  "status": 7,
  "message": "This is a test."
}'

HTTP/1.1 201 Created

{"_index":"test-1","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

When I inspected the mapping that was applied to test-1, I saw:

curl -ivvk -u username:password -H "Accept: application/json" "https://localhost:9200/test-1/_mapping?pretty"

HTTP/1.1 200

{
  "test-1" : {
    "mappings" : {
      "properties" : {
        "message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "status" : {
          "type" : "long"
        }
      }
    }
  }
}

I realized the template was ignored since status had type, long, instead of keyword. Can someone tell me what I should be doing?

What is the output of _cat/templates? Do you have any other templates that match the same pattern?

The output is exactly what I originally wrote, [{"name":"test_template","index_patterns":"[test-*]","order":"0","version":null,"composed_of":""}]. I used a pristine Elasticsearch that had no templates.

It appears that the “template” name/value in my test_template caused the problem. Removing it (moving its inner data to its level) allowed it to work.

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