# Dynamic template for complex types

**URL:** <https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059>\
**Category:** Elasticsearch\
**Created:** [October 24, 2019, 12:17pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059 "2019-10-24T12:17:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![parosio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/parosio/32/27367_2.png) [@parosio](https://discuss.elastic.co/u/parosio)\
**Post date:** [October 24, 2019, 12:17pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/1 "2019-10-24T12:17:55Z")

</div>

Hello,

is it possible to have a complex type (like a geopoint) dinamically recognized using the dynamic\_templates section of a template/index mapping?

Sort of:

```
PUT test_loc/doc/1
{
  "position" : [-64, -19.4]
}

```

resulting in `position` field recognized as a geopoint, instead of float.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 24, 2019, 2:05pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/2 "2019-10-24T14:05:24Z")

</div>

No it's not AFAIK. You should better detect on the field name in such a case.

---

<div class="post-metadata">

**Author:** ![parosio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/parosio/32/27367_2.png) [@parosio](https://discuss.elastic.co/u/parosio)\
**Post date:** [October 25, 2019, 8:55am UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/3 "2019-10-25T08:55:27Z")

</div>

Thank you for your answer, @dadoonet.  
I tried what follows, but it doesn't seem to work as expected:

```
DELETE test-loc

PUT test-loc
{
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "geo_mapping_1": {
            "match_mapping_type": "string",
            "match": "*location",
            "mapping": {
              "type": "geopoint"
            } } }
      ] } }
}

GET test-loc

PUT test-loc/doc/1
{ "start_location" : { "lon" : 4.8995, "lat" : 52.3824 } }

GET test-loc

```

After the post of document 1 I get:

```
# GET test-loc
{
  "test-loc" : {
    "aliases" : { },
    "mappings" : {
      "_default_" : {
        "_all" : {
          "enabled" : false
        }
      },
      "doc" : {
        "_all" : {
          "enabled" : false
        },
        "dynamic_templates" : [
          {
            "geo_mapping_1" : {
              "match" : "*location",
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "geopoint"
              }
            }
          }
        ],
        "properties" : {
          "start_location" : {
            "properties" : {
              "lat" : {
                "type" : "float"
              },
              "lon" : {
                "type" : "float"
              }      
       } } } }
    },
        . . .
    }
  }
}

```

So, apparently, I'm missing something: `start_location` property isn't correctly recognized as a geopoint.

I can _statically_ create a geopoint property:

```
DELETE test-loc
PUT test-loc
{
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "geo_mapping_1": {
            "match_mapping_type": "string",
            "match": "*location",
            "mapping": {
              "type": "geopoint"
            }
          }
        }
      ],
      "properties" : {
          "dest_location": {
            "type" : "geo_point"
          }
      }
    }
  }
}

```

and the `dest_location` is correctly mapped when indexing the following document:

```
PUT test-loc/doc/2
{
  "dest_location" : { 
      "lon" : 4.8995,
      "lat" : 52.3824
  }
}

```

 ![20191025_test_loc_pattern](https://us1.discourse-cdn.com/elastic/original/3X/e/4/e4b855e5f52c3f6430b26497ba5be3ed5ed16653.png)

Is there a way to have the complex type correctly recognized and dinamically mapped to the desired property type?

---

<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:** [November 22, 2019, 9:05am UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/4 "2019-11-22T09:05:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 7, 2020, 1:45pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/5 "2020-01-07T13:45:37Z")

</div>



---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 7, 2020, 1:46pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/6 "2020-01-07T13:46:03Z")

</div>

Sorry it took so long.  
Here is how you can fix it:

```auto
DELETE test-loc
PUT test-loc
{
  "mappings": {
    "dynamic_templates": [
      {
        "geo": {
          "match_mapping_type": "object",
          "match": "*location",
          "mapping": {
            "type": "geo_point"
          }
        }
      }
    ]
  }
}

PUT test-loc/_doc/1
{
  "start_location": {
    "lon": 4.8995,
    "lat": 52.3824
  }
}

GET test-loc/_mapping

```

Tested on 7.5.

---

<div class="post-metadata">

**Author:** ![parosio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/parosio/32/27367_2.png) [@parosio](https://discuss.elastic.co/u/parosio)\
**Post date:** [January 7, 2020, 4:53pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/7 "2020-01-07T16:53:28Z")

</div>

Thank you @dadoonet!

Following the example, I successfully tested also a dynamic mapping for a geo-shape (still using 6.7):

```
PUT test-loc3
{
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {"geo": {
            "match_mapping_type": "object",
            "match": "*location",
            "mapping": {"type": "geo_point"}
        }},
        {"shape": {
            "match_mapping_type": "object",
            "match": "*route",
            "mapping": {"type": "geo_shape"}
        }}
      ]
    }
  }
}

PUT test-loc3/_doc/r2
{
  "a_route": {
    "type": "linestring",
    "coordinates": [[5, 50], [0, 0]]
  }
}

```

And the route is correctly visualized in a kibana map (after creating the kibana index pattern).

---

<div class="post-metadata">

**Author:** ![parosio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/parosio/32/27367_2.png) [@parosio](https://discuss.elastic.co/u/parosio)\
**Post date:** [September 11, 2020, 2:58pm UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/8 "2020-09-11T14:58:27Z")

</div>

Sorry to reopen this issue,  
but I'm trying to define a template to map a geoip.

As static property could be defined as:

```auto
    "some_geoip" : {
        "dynamic" : true,
        "properties" : {
          "ip" : {
            "type" : "ip"
          },
          "location" : {
            "type" : "geo_point"
          },
          "latitude" : {
            "type" : "half_float"
          },
          "longitude" : {
            "type" : "half_float"
          }
        }
    },

```

but (for example) if I have documents with an arbitrary number of IPs, I'd need to declare it dynamic.

Is it possible to have it in the `dynamic_templates` part of index / template mapping?

```auto
    "dynamic_templates": [
        . . .,
        {
            "geo_ip": {
                "match_mapping_type": "object",
                "match": "*geoip",
                "mapping": {"type": "< what goes here? >"}
            }
        },
        . . .
    ]

```

I suppose I should have to a way to define a new **Field datatype name** , and then use that name in my template/index definition.

Is it possible, or is there any workaround to the same effect?

---

<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:** [November 4, 2022, 7:30am UTC](https://discuss.elastic.co/t/dynamic-template-for-complex-types/205059/9 "2022-11-04T07:30:48Z")

</div>


