Hello Michael , This should work for you -
cat scr { "script": "sdf = new java.text.SimpleDateFormat('yyyy-MM-dd\\'T\\'HH:mm:ss');startDate = sdf.parse(ctx._source.start);endDate = sdf.parse(ctx._source.end); ctx._source.diff = endDate.getTime() - startDate.getTime();" } curl -XPOST http://localhost:9200/test/logs/1/_update -d @scr {"_index":"test","_type":"logs","_id":"1","_version":10 I was in the pretext that one can access the doc object for updation , but seems we can only access _source here. In that case , you need to parse the string to date object and then do the stuffs. The above works for me perfectly. Thanks Vineeth On Thu, Sep 11, 2014 at 12:12 AM, Michael Giagnocavo <m...@giagnocavo.net> wrote: > BTW I found the problem with referring to a script by name. If the > script has an error, then it fails on compile, written to error log. It’s > then not considered a script. ES might want to change that behavior, so if > you use “script” : “brokenscript” you get an error indicating what’s > actually wrong. Of course if you know about this behavior I guess it’s not > a big deal. > > > > *From:* elasticsearch@googlegroups.com [mailto: > elasticsearch@googlegroups.com] *On Behalf Of *vineeth mohan > *Sent:* Wednesday, September 10, 2014 5:14 AM > *To:* elasticsearch@googlegroups.com > *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 <m...@giagnocavo.net> > 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 elasticsearch+unsubscr...@googlegroups.com. > 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 elasticsearch+unsubscr...@googlegroups.com. > 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 elasticsearch+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elasticsearch/ef8054ee424145a489ee496117ae0dd4%40CO1PR07MB331.namprd07.prod.outlook.com > <https://groups.google.com/d/msgid/elasticsearch/ef8054ee424145a489ee496117ae0dd4%40CO1PR07MB331.namprd07.prod.outlook.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 elasticsearch+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nk9KVGSHZHHx8OUyvGaq6MknOim23%2BBaNnZfw3TEuRDg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.