# Accessing 'getWeekOfWeekyear()' when aggregating

**URL:** <https://discuss.elastic.co/t/accessing-getweekofweekyear-when-aggregating/91844>\
**Category:** Elasticsearch\
**Created:** [July 5, 2017, 6:30am UTC](https://discuss.elastic.co/t/accessing-getweekofweekyear-when-aggregating/91844 "2017-07-05T06:30:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paulo\_Silva](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Paulo\_Silva](https://discuss.elastic.co/u/Paulo_Silva)\
**Post date:** [July 5, 2017, 6:30am UTC](https://discuss.elastic.co/t/accessing-getweekofweekyear-when-aggregating/91844/1 "2017-07-05T06:30:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Paulo\_Silva](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Paulo\_Silva](https://discuss.elastic.co/u/Paulo_Silva)\
**Post date:** [July 6, 2017, 2:41am UTC](https://discuss.elastic.co/t/accessing-getweekofweekyear-when-aggregating/91844/2 "2017-07-06T02:41:03Z")

</div>

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
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 3, 2017, 2:41am UTC](https://discuss.elastic.co/t/accessing-getweekofweekyear-when-aggregating/91844/3 "2017-08-03T02:41:15Z")

</div>

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