Dynamic template for complex types

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.

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

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

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

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

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

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.

1 Like

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).

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

As static property could be defined as:

    "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?

    "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?