0

I need to build an aggregation using Java API:

GET /sales/t/_search
{
  "aggs": {
    "group_by_month": {
      "terms": {
        "script": "def opDate = new DateTime(doc['date'].date); opDate.getMonthOfYear()",
        "order": {
          "_term": "asc"
        }
      }
    }
  }
}

Up to now, I've been able to create an terms aggregation, however, I'm not able to provide the script:

AggregationBuilders.terms(this.getName()).field(this.getName()).script(??????????)

Which's the way to provide an inline script using Java API?

1 Answer 1

2

Simply like this (no need for field() call by the way):

AggregationBuilders.terms(this.getName())
    .script(new Script("def opDate = new DateTime(doc['date'].date); opDate.getMonthOfYear()"))

Note, though, that your script can be much simpler, like this:

doc['date'].date.getMonthOfYear()

The reason is because doc['date'].date is already a DateTime instance

Sign up to request clarification or add additional context in comments.

2 Comments

Solved! Thanks. Nevertheless, this aggregation returns me an 'LongTerms' bucketing aggregation result with each month as a double. Is there any way to set the terms aggregation bucket are longs instead of doubles?
Sorry, it returns me an DoubleTerms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.