Accessing 'getWeekOfWeekyear()' when aggregating

Hi guys,

Out of the box, Elastic allows us to perform an aggregation as following:

"inline": "doc['LAST_MODIFIED_DATE'].date.getWeekOfWeekyear()"

Which is great! However, our system here supports time zone. That also is normally covered by using the ZonedDateDime, ie:

ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('UTC'))

So far, so good.

The problem I'm facing is, after getting the zoned datetime I cannot invoke the 'getWeekOfWeekyear()' method as it can only be accessed MutableDateTime (and other classes) but not by a ZonedDateTime object.

I've tried to create an instance a MutableDateTime obj, but it's not recognized. I've tried the following workarounds as well:

Instant instant = ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('UTC')).toInstant(); 
Date date = Date.from(instant);
date.getWeekOfWeekyear()


def dateCopy = doc['LAST_MODIFIED_DATE'].date;
Instant instant = ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('UTC')).toInstant(); 
dateCopy = new MutableDateTime(instant);
dateCopy.getWeekOfWeekyear()

It seems so close, but I couldn't sort that out yet.
Any guru could help me w/ that?

Cheers!

I guess that was a tough one... This is the working script I've came up with:

GET /unittesttg1_tg1_fq1/_search
{
  "size": 0,
  "aggs": {
    "groupby": {
      "terms": {
        "script": {
          "inline": "ZonedDateTime ztz = ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('UTC')); ztz.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);"
        },
        "size": 60
      }
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.