Search code examples
elasticsearchfiddlerelasticsearch-7elasticsearch-template

ElasticSearch: Weird problem in inserting documents to ElasticSearch indices when a template is present


I am trying to do some testing in ElasticSearch. I am able to populate everything as required but whenever I try to put our project's default template and then insert, the data is not ingested in the index (http calls are successful though).

Upon inspection, I realised that I can't insert even a simple document even after using elasticSearch's default template. For example inserting the template from ES's documentation:

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "host_name": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date",
        "format": "EEE MMM dd HH:mm:ss Z yyyy"
      }
    }
  }
}

And then inserting a doc in index = "bark" by

PUT http://localhost:9200/bark/_doc/11232 HTTP/1.1
User-Agent: Fiddler
Host: localhost:9200
Content-Length: 21
Content-Type: application/json

{"host_name": "generic_name"}

adds a doc in the index but without the data about the host_name. Simply changing the index name to something for which this template does not apply (like index = dark) will add a doc with the data about host_name. Showing index data for replication:

(when index=bark)

{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"bark","_type":"_doc","_id":"11232","_score":1.0}]}}

(when index=dark)

{"took":6,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"dark","_type":"_doc","_id":"11232","_score":1.0,"_source":{"host_name":"generic_name"}}]}}

Notice the _source":{"host_name":"generic_name"} field is absent in the former?

What could be done for this? If someone has faced this issue or knows a fix, please help.


Solution

  • You need to remove this from your mapping

    "_source": {
      "enabled": false
    },
    

    The effect of this setting is that the source document is not stored in the _source field. That's probably not what you want.