Thanks for the reply Vineeth. Here’s a concise example showing the date type problem. Despite having a mapping, the script is getting the properties as strings. How can I verify how ElasticSearch is actually storing something?
$ cat /etc/elasticsearch/scripts/setdur.groovy ctx._source.dur = (ctx._source.end.getTime() - ctx._source.start.getTime()) $ curl -i -XPUT http://localhost:9200/tesi/ -d '{\ "mappings": {\ "testt": { "properties": { "start": { "type": "date" }, "end": { "type": "date" } } }\ }\ }' HTTP/1.1 200 OK $ curl -i -XPOST http://localhost:9200/tesi/testt/1 -d '{ "start": "2014-09-01T12:00:00", "end": "2014-09-02T12:00:00" }' HTTP/1.1 201 Created rooty@sofab-es1:~$ curl -i -XPOST http://localhost:9200/tesi/testt/1/_update -d '{"script": "setdur", "lang": "groovy"}' HTTP/1.1 400 Bad Request Content-Type: application/json; charset=UTF-8 Content-Length: 399 {"error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: java.lang.String.getTime() is applicable for argument types: () values: []\nPossible solutions: getBytes(), trim(), getBytes(java.lang.String), getBytes(java.nio.charset.Charset), getAt(groovy.lang.IntRange), getAt(int)]]; ","status":400}rooty@sofab-es1:~$ Any suggestions? -Michael From: [email protected] [mailto:[email protected]] On Behalf Of vineeth mohan Sent: Wednesday, September 10, 2014 5:14 AM To: [email protected] Subject: Re: Scripting and dates Hello Michael , Please find the answers in the order of questions you have asked - 1. Referencing script from file system is explained here. It has very well worked for me , please double check on it - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html 2. I feel you haven't declared that field as date type in the schema . If you had done that , you will recieve the epoch instead of string. - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#date 3. Dates are internally stored as epoch. So it should handle that second fraction too. More on the format can be seen here - http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html 4. What exactly do you want to do with the duration ? If its range aggregation , it does have script support - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html#search-aggregations-bucket-range-aggregation Thanks Vineeth On Wed, Sep 10, 2014 at 11:32 AM, Michael Giagnocavo <[email protected]<mailto:[email protected]>> wrote: I'm trying to work with dates inside a script. I've got a few questions: 1. How do I reference a script that I have in the scripts directory? Simply POSTing to /index/type/id/_update with { "script": "scriptname" } does not seem to work. "No such property: scriptname for class: ScriptN", where N starts at 3 (I have two .groovy files in my scripts directory). 2: How can I get actual date objects from the source? ctx._source.fieldname always returns a type string, even if I just created the field with ctx._source.fieldname = new Date(). Right now I'm parsing the string output in Groovy, which seems suboptimal. 3: Are ISO8601 dates not fully supported, as far as arbitrary fractional second decimals? (Not just 3 or another fixed number?) Any suggestions on handling JSON input from multiple sources, some of which have high-precision? 4: Can I use a script to project the document into a scalar for aggregates? For instance, if I have Date fields "start" and "end", and want to calculate the average duration (start - end) in an aggregate. I see value-level scripts are allowed, and 1.4 has "scripted metric aggregation". For now am I best off just storing the duration in the document? Thank you, Michael -- You received this message because you are subscribed to the Google Groups "elasticsearch" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]<mailto:elasticsearch%[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/13d4cf783a83447a84b62206605ad312%40CO1PR07MB331.namprd07.prod.outlook.com. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "elasticsearch" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]<mailto:[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DBzthM14yz3SuzxvTz5QXOW4Gtt72rvsA1-dND5eP--A%40mail.gmail.com<https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DBzthM14yz3SuzxvTz5QXOW4Gtt72rvsA1-dND5eP--A%40mail.gmail.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "elasticsearch" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/06f511148cb64db49990663de158e9b6%40CO1PR07MB331.namprd07.prod.outlook.com. For more options, visit https://groups.google.com/d/optout.
