# Index Template getting overwritten on when indexing first document

**URL:** <https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580>\
**Category:** Elasticsearch\
**Created:** [June 29, 2015, 10:04pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580 "2015-06-29T22:04:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donnie\_McNeal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnie_mcneal/32/3508_2.png) [@Donnie\_McNeal](https://discuss.elastic.co/u/Donnie_McNeal)\
**Post date:** [June 29, 2015, 10:04pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/1 "2015-06-29T22:04:47Z")

</div>

I'm seeing a strange issue with my index templates in elasticsearch where Elasticsearch is overwriting the index templates I have defined with it's default mappings. I have a index template for all of my rpt\_\* indicies. When i do a GET /\_template I see.

{  
"reporting\_template\_msghist":{  
"order":0,  
"template":"rpt\_\*",  
"settings":{

```
  },
  "mappings":{  
     "msgHistory":{  
        "properties":{  
           "date":{  
              "format":"dateOptionalTime",
              "type":"date"
           },
           "campaignId":{  
              "index":"not_analyzed",
              "type":"string"
           },
           "venueId":{  
              "index":"not_analyzed",
              "type":"string"
           },
           "state":{  
              "index":"not_analyzed",
              "type":"string"
           }
        }
     }
  },
  "aliases":{  

  }

```

}  
}

I then create a new document (along with a new index) in the index rpt\_201506 with body:

```
{
  "campaignId": "abc-123-zxyz",
  "date": "2015-01-23T19:28:07.000+0000",
  "venueId": "venue_id",
  "state": "read"
}

```

When I investigate the index mappings after the index and first document is created they are now:

```
"msgHistory":{  
  "properties":{  
               "date":{  
                  "format":"dateOptionalTime",
                  "type":"date"
               },
               "campaignId":{  
                  "type":"string"
               },
               "venueId":{  
                  "type":"string"
               },
               "state":{  
                  "type":"string"
               }
  }
}

```

Hopefully, I'm just doing something stupid, but I didn't expect Elasticsearch to try and dynamically determine the mapping since I already had the index template mapping defined.

---

<div class="post-metadata">

**Author:** ![nellicus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nellicus/32/51566_2.png) [@nellicus](https://discuss.elastic.co/u/nellicus)\
**Post date:** [June 29, 2015, 10:20pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/2 "2015-06-29T22:20:10Z")

</div>

How are you inserting these?

Template is honoured in my repro

```
    PUT _template/reporting_template_msghist
{
  "order": 0,
  "template": "rpt_*",
  "settings": {},
  "mappings": {
    "msgHistory": {
      "properties": {
        "date": {
          "format": "dateOptionalTime",
          "type": "date"
        },
        "campaignId": {
          "index": "not_analyzed",
          "type": "string"
        },
        "venueId": {
          "index": "not_analyzed",
          "type": "string"
        },
        "state": {
          "index": "not_analyzed",
          "type": "string"
        }
      }
    }
  },
  "aliases": {}
}

PUT rpt_test/msgHistory/1
{
  "campaignId": "abc-123-zxyz",
  "date": "2015-01-23T19:28:07.000+0000",
  "venueId": "venue_id",
  "state": "read"
}

GET rpt_test/_mapping

```

returns

```
{
   "rpt_test": {
      "mappings": {
         "msgHistory": {
            "properties": {
               "campaignId": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "date": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "state": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "venueId": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 29, 2015, 10:24pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/3 "2015-06-29T22:24:21Z")

</div>

Are you setting the `msgHistory` type for your document when you index?

---

<div class="post-metadata">

**Author:** ![Donnie\_McNeal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnie_mcneal/32/3508_2.png) [@Donnie\_McNeal](https://discuss.elastic.co/u/Donnie_McNeal)\
**Post date:** [June 29, 2015, 10:27pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/4 "2015-06-29T22:27:44Z")

</div>

@nellicus I'm using the java client to create the index template and document. I'm currently retrying using just the REST API directly.

---

<div class="post-metadata">

**Author:** ![Donnie\_McNeal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnie_mcneal/32/3508_2.png) [@Donnie\_McNeal](https://discuss.elastic.co/u/Donnie_McNeal)\
**Post date:** [June 29, 2015, 10:28pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/5 "2015-06-29T22:28:27Z")

</div>

@warkolm I'm pretty sure I am setting the type, but I will double check.

---

<div class="post-metadata">

**Author:** ![Donnie\_McNeal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnie_mcneal/32/3508_2.png) [@Donnie\_McNeal](https://discuss.elastic.co/u/Donnie_McNeal)\
**Post date:** [June 29, 2015, 11:17pm UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/6 "2015-06-29T23:17:50Z")

</div>

Thanks for the help. I am a little further along. It looks like there is something wrong the way I'm adding the index template through the java API. I have copied a sample of the code I am using:

```
XContentBuilder json = jsonBuilder()
                .startObject()
                .startObject("properties")
                    .startObject("campaignId")
                        .field("type", "string")
                        .field("index", "not_analyzed")
                    .endObject()
                    .startObject("date")
                        .field("type", "date")
                        .field("format", "dateOptionalTime")
                    .endObject()
                    .startObject("state")
                        .field("type", "string")
                        .field("index", "not_analyzed")
                    .endObject()
                    .startObject("venueId")
                        .field("type", "string")
                        .field("index", "not_analyzed")
                    .endObject()
                .endObject()
            .endObject();

client.admin().indices().preparePutTemplate("reporting_template_msghist")
                    .setTemplate("rpt_*")
                    .addMapping("msgHistory", json)
                    .execute()
                    .actionGet();
```

---

<div class="post-metadata">

**Author:** ![Donnie\_McNeal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/donnie_mcneal/32/3508_2.png) [@Donnie\_McNeal](https://discuss.elastic.co/u/Donnie_McNeal)\
**Post date:** [June 30, 2015, 3:08am UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/7 "2015-06-30T03:08:26Z")

</div>

I have tracked down my problem to an issue in the Java API (i think) - [https://github.com/elastic/elasticsearch/issues/10436](https://github.com/elastic/elasticsearch/issues/10436)

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:04am UTC](https://discuss.elastic.co/t/index-template-getting-overwritten-on-when-indexing-first-document/24580/8 "2017-07-06T00:04:39Z")

</div>


