Use of dynamic_template in _index_template

Hi all,

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

Hi @hzarrabi,

yes, that is possible. You can combine the examples from the dynamic template docs and composable index templates into an example like the below.

First create the template using dynamic_templates (notice plural):

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:

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

and finally introspect the resulting mapping:

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
        }
      }
    }
  }
}

Hi @HenningAndersen,
Thank you so much.

Could you please tell me what is wrong here :

    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

    {
      "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
    }

Hi @hzarrabi,

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

Hi @HenningAndersen,

Thank you so much !

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