Mappings._default_.dynamic_templates doesn't show up in resulting mapping

Hi everyone,

Using ES 2.3 I create an index with default dynamic templates, just like in the examples.

PUT /myindex
{
    "mappings": { 
        "_default_": {
            "dynamic_templates": [
                {
                    "no_date_detection": {
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "string",
                            "date_detection": false
                       }
                    }
                }
            ]
        }
    }
}

Then I create a new type and object by

PUT /myindex/gardeners/1
{
    "name": "Oscar"
}

When I GET the _mapping afterwards it shows that the default mapping is handed down to the type "gardeners" but "date_detection": false does not show up in the mapping of the new field "name".

{
   "myindex": {
      "mappings": {
         "_default_": {
            "dynamic_templates": [
               {
                  "no_date_detection": {
                     "mapping": {
                        "type": "string",
                        "date_detection": false
                     },
                     "match_mapping_type": "string"
                  }
               }
            ]
         },
         "gardeners": {
            "dynamic_templates": [
               {
                  "no_date_detection": {
                     "mapping": {
                        "type": "string",
                        "date_detection": false
                     },
                     "match_mapping_type": "string"
                  }
               }
            ],
            "properties": {
               "name": {
                  "type": "string"
               }
            }
         }
      }
   }
}

Shouldn't the properties for the field "name" be

        "properties": {
           "name": {
              "type": "string",
              "date_detection": false
           }
        }

?

Is this expected behaviour? And if, how can I verify the mapping got applied to the field?

Thanks in advance and cheers /Carsten

Edit: Fixed preformatted

yes, this is expected behavior. Once the String type is determined for the name field, there is no need for date_detection on that level. The date_detection is only relevant when ES encounters a new String field (see here).

If you want to test that the templates get properly applied to the field, try indexing a new document with a different field where you put a date as string value.

Understood. But when, after deleting the index and creating it again, I put in

PUT /myindex/gardeners/1
{
    "name": "2009-11-15T14:12:12"
}

GET _mapping shows

        "properties": {
           "name": {
              "type": "date",
              "format": "strict_date_optional_time||epoch_millis"
           }
        }

So, the dynamic template didn't get applied. I can't find the error in that minimalistic code I more or less copied from the documentation. I think I remember that, when I first tried ES, it actually did work.

ok, I think that dynamic_templates don't allow you to specify the date_detection.
You have to specify the date_detection directly on the mapping (not at the level of a specific type), i.e. do

PUT /myindex
{
    "mappings": {
        "gardeners": {
            "date_detection": false
        }
    }
}

before indexing the first document.

If you want this to get automatically added to new indices, you can use index templates.

Thanks Yannick, that is the case.
Now that I know what I'm looking for I also find that in the documentation https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html
Well, I'll be off checking and fixing like 20 models :wink:

Looks like

PUT /myindex
{
    "mappings": { 
        "_default_": {
          "date_detection": false
        }
    }
}

achieves what I need.

Just one thing I wanted to add here for sake of completeness: In ES v5.0.0, we are throwing an exception that date_detection is an invalid parameter in that place.

Hey everyone,

On the same subject - do you know if it is possible to mix date_detection true and false within the same mapping?

i.e. something similar to this:

PUT /myindex
{
	    "mappings": { 
	        "_default_": {
	        	"dynamic": true,
	            "date_detection": false,
	            "dynamic_templates": [
	                {
	                    "date_detection_off": {
	                    	"match": "*_nodate",
	                        "match_mapping_type": "string",
	                        "mapping": {
	                            "type": "string",
	                       }
	                    }
	                },
	                {
	                    "date_detection_on": {
	                    	"match": "*_yesdate",
	                        "match_mapping_type": "string",
	                        "mapping": {
	                            "type": "dateOptionalTime",
	                            "date_detection": true
	                       }
	                    }
	                }
	            ]
	        }
	    }
	}

Thanks
Lior