# Use of dynamic\_template in \_index\_template

**URL:** <https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453>\
**Category:** Elasticsearch\
**Created:** [January 18, 2021, 4:58pm UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453 "2021-01-18T16:58:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hzarrabi](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@hzarrabi](https://discuss.elastic.co/u/hzarrabi)\
**Post date:** [January 18, 2021, 4:58pm UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453/1 "2021-01-18T16:58:49Z")

</div>

Hi all,

```auto
Is it possible to use dynamic_template in the newly introduced _index_template ?

If yes, do you have any example of how to use it ?
If not, how should I declare some dynamic_template with the use of _index_template ?

```

Thanks

---

<div class="post-metadata">

**Author:** ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)\
**Post date:** [January 18, 2021, 6:29pm UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453/2 "2021-01-18T18:29:30Z")

</div>

Hi @hzarrabi,

yes, that is possible. You can combine the examples from the [dynamic template docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html) and [composable index templates](https://www.elastic.co/guide/en/elasticsearch/reference/7.10/index-templates.html) into an example like the below.

First create the template using `dynamic_templates` (notice plural):

```auto
PUT _index_template/my_template
{
  "index_patterns": ["my_data"],
  "template": {
    "mappings": {
      "dynamic_templates" : [{
        "unindexed_longs": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "long",
            "index": false
          }
        }
      }]
    }
  }
}

```

Index some doc with a matching type:

```auto
PUT my_data/_doc/1
{
  "along" : 17
}

```

and finally introspect the resulting mapping:

```auto
GET my_data/_mapping 

{
  "my_data" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "unindexed_longs" : {
            "match_mapping_type" : "long",
            "mapping" : {
              "index" : false,
              "type" : "long"
            }
          }
        }
      ],
      "properties" : {
        "along" : {
          "type" : "long",
          "index" : false
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![hzarrabi](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@hzarrabi](https://discuss.elastic.co/u/hzarrabi)\
**Post date:** [January 19, 2021, 5:45pm UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453/3 "2021-01-19T17:45:34Z")

</div>

Hi @HenningAndersen,  
Thank you so much.

Could you please tell me what is wrong here :

```auto
    PUT _index_template/my-index_template
    {
       "index_patterns": [
          "my-index-*"
       ],
       "composed_of": [
          "my-component-template"
       ],
       "template": {
          "aliases": {
             "my-index-alias": {}
          }
       },
       "mappings": {
         "properties": {
           "REGISTER_DATE" : {
             "type" : "date",
             "format": "yyyy-MM-dd"
           },
           "DOCUMENT_NB" : {
              "type" : "keyword"
           },
           "DOCUMENT_TYPE" : {
              "type" : "keyword"
           }
         }
       }
    }

    PUT _component_template/my-component-template
    {
      "template": {
          "settings": {
              "index": {
                  "number_of_shards": "1",
                  "number_of_replicas": "2"
              }
          },
          "mappings": {
              "properties": {
                  "projectId": {
                      "type": "keyword"
                  },
                  "YEAR" : {
                      "type" : "keyword"
                  },
                  "MONTH" : {
                      "type" : "keyword"
                  },	
                  "DAY" : {
                     "type" : "keyword"
                  }
              },
              "dynamic_templates": [
                    {
                      "long": {
                        "mapping": {
                          "type": "float"
                        },
                        "match": "*_montant"
                      }
                    },
                    {
                      "geo_point": {
                        "mapping": {
                          "type": "geo_point"
                        },
                        "match": "*_location"
                      }
                    },
                    {
                      "strings_as_dates": {
                        "match_mapping_type": "string",
                        "mapping": {
                          "type": "date"
                        },
                        "match": "*_date"
                      }
                    }
                ]
          }
      }
    }

```

I receive the folowing error message that says the mapping is unkown when I PUT **\_index\_template**

```auto
    {
      "error" : {
        "root_cause" : [
          {
            "type" : "x_content_parse_exception",
            "reason" : "[13:4] [index_template] unknown field [mappings]"
          }
        ],
        "type" : "x_content_parse_exception",
        "reason" : "[13:4] [index_template] unknown field [mappings]"
      },
      "status" : 400
    }

```

---

<div class="post-metadata">

**Author:** ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)\
**Post date:** [January 20, 2021, 9:17am UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453/4 "2021-01-20T09:17:06Z")

</div>

Hi @hzarrabi,

I believe the problem is that the `mappings` in `my_index_template` needs to go inside the `template` object.

---

<div class="post-metadata">

**Author:** ![hzarrabi](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@hzarrabi](https://discuss.elastic.co/u/hzarrabi)\
**Post date:** [January 21, 2021, 7:41pm UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453/5 "2021-01-21T19:41:59Z")

</div>

Hi @HenningAndersen,

Thank you so much !

---

<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:** [February 18, 2021, 7:42pm UTC](https://discuss.elastic.co/t/use-of-dynamic-template-in-index-template/261453/6 "2021-02-18T19:42:05Z")

</div>

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