How can I apply dynamic date formats to multiple types in Elasticsearch?
I would create a custom dynamic template. Something like this:
PUT /datetest/_mapping/_default_{ "date_detection" : true, "dynamic_templates" : [ { "dates" : { "match" : ".*date.*", "mapping" : { "type" : "date", "format" : 'yyyy-MM-dd HH:mm:ss' } } } ]}
Just tried it. It seems to work. I hope this helps :)
All I can think of, would be adding dynamic template:
PUT /datetest{ "mappings": { "_default_": { "date_detection": false, "dynamic_templates": [ { "dates": { "match": ".*Date|date", "match_pattern": "regex", "mapping": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||dateOptionalTime" } } } ] } }}
All three statements will run just fine:
PUT /datetest/doc/1{ "date" : "2015-01-01 12:00:00" }PUT /datetest/otherdoc/1{ "otherdate" : "2015-01-01 12:00:00" }PUT /datetest/otherdoc/2{ "date" : "2015-01-01 12:00:00" }
Dynamic template will try to match your field name on based pattern. They need to end with date or Date.
I know you need just yyyy-MM-dd HH:mm:ss
format, but I've also added default one, so that Elasticsearch can pick one from multiple ones.