Elasticsearch: transform date with groovy script -
i have following (simplified) mapping:
{ "event_date": { "_source": { "enabled": true }, "_all": { "enabled": true }, "dynamic": "strict", "properties": { "start_date_time": { "type": "date", "format": "dateoptionaltime" }, "start_date_day": { "type": "date", "format": "dateoptionaltime", "index": "not_analyzed" } } } }
the indexed objects this:
{ "start_date_time": "2017-05-08t18:23:45+0200" }
the property start_date_day
should contain same date, time set 00:00:00. in example above start_date_day
must "2017-05-08t00:00:00+0200".
i think,it possible achieve transform mapping , groovy script, developed groovy code did not work in elasticsearch-context , not familiar groovy language.
maybe has idea on how solve this?
yes doable, testing/running might need turn on script.groovy.sandbox.enabled: true
in ../conf/elasticsearch.yml
first.
put datetest/ { "mappings": { "event_date": { "_source": { "enabled": true }, "_all": { "enabled": true }, "dynamic": "strict", "transform" : { "script" : "ctx._source['start_date_day'] = new date().parse(\"yyyy-mm-dd\", ctx._source['start_date_time']).format(\"yyyy-mm-dd\");", "lang": "groovy" }, "properties": { "start_date_time": { "type": "date", "format": "dateoptionaltime" }, "start_date_day": { "type": "date", "format": "dateoptionaltime", "index": "not_analyzed", "store": "yes" } } } } }
sample data:
put /datetest/event_date/1 { "start_date_time": "2017-05-08t18:23:45+0200" }
sample output:
get /datetest/event_date/_search { "query": { "match_all": {} }, "fields": ["start_date_time","start_date_day"] } { "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "datetest", "_type": "event_date", "_id": "1", "_score": 1, "fields": { "start_date_day": [ "2017-05-08t00:00:00.000z" ], "start_date_time": [ "2017-05-08t18:23:45+0200" ] } } ] } }
Comments
Post a Comment