# Hourly aggregation for timestamp and Java API irrespective of date

**URL:** <https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575>\
**Category:** Elasticsearch\
**Created:** [November 29, 2017, 11:18am UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575 "2017-11-29T11:18:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpmiitian](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jpmiitian](https://discuss.elastic.co/u/jpmiitian)\
**Post date:** [November 29, 2017, 11:18am UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/1 "2017-11-29T11:18:36Z")

</div>

Hi

Though we can get hourly buckets using script as described in below link.  
But, Can I get the same result using Elasticsearch Java Rest HL client.  
Is it possible to define the script for AggregationBuilder.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html)

```
POST /sales/_search?size=0
{
    "aggs": {
        "dayOfWeek": {
            "terms": {
                "script": {
                    "lang": "painless",
                    "source": "doc['date'].value.dayOfWeek"
                }
            }
        }
    }
}

```

Please provide your suggestions.

Thanks  
Jay

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [November 29, 2017, 12:16pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/2 "2017-11-29T12:16:55Z")

</div>

Yes, the `TermsAggregationBuilder` has a `.script(Script)` method you can pass your script to.

---

<div class="post-metadata">

**Author:** ![jpmiitian](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jpmiitian](https://discuss.elastic.co/u/jpmiitian)\
**Post date:** [November 30, 2017, 7:21am UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/3 "2017-11-30T07:21:37Z")

</div>

Thanks for responding Colin. 🙂

I was using the `.script(Script)` method of `TermsAggregationBuilder`.  
But there is an error coming while searching the elasticsearch.

Please refer below code block.

```
public Map findByHour(String pixelId, Long startTime, Long endTime) {
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.matchQuery("pid", pixelId));

    QueryBuilder queryBuilder = boolQueryBuilder.filter(QueryBuilders.rangeQuery("eventTime").gte(startTime).lt(endTime));

    // date histogram

```

// DateHistogramAggregationBuilder dateHistoAggBuilder =  
// AggregationBuilders.dateHistogram("date\_histogram").field("eventTime")  
// .dateHistogramInterval(DateHistogramInterval.DAY)  
// .format("dd-MM-yyyy");

```
    // aggs for day of the week
    // Script dayOfWeekScript = new Script("doc['eventTime'].date.dayOfWeek");

    // inline = new Script("doc['eventTime'].date.dayOfWeek");

    Script inline = new Script(ScriptType.INLINE, "painless", "doc['eventTime'].date.dayOfWeek", new HashMap<>());

    TermsAggregationBuilder termsAggBuilderForDOW = AggregationBuilders.terms("by_day_of_week").script(inline);
    termsAggBuilderForDOW.size(7); // 7 size

    List<AggregationBuilder> aggregationBuilderList = new ArrayList<>();

```

// aggregationBuilderList.add(dateHistoAggBuilder);  
aggregationBuilderList.add(termsAggBuilderForDOW);

```
    SearchRequest searchRequest = new SearchRequest("esIndex"); // index name
    searchRequest.types("indexType");// type name
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryBuilder);

    aggregationBuilderList.stream().forEach(termsAggregationBuilder -> {
        searchSourceBuilder.aggregation(termsAggregationBuilder);
    });

    searchSourceBuilder.fetchSource(false);//don't fetch docs.
    searchRequest.source(searchSourceBuilder);//set search source builder

    // make search request for client
    try {
        // getting error here
        SearchResponse searchResponse = esServiceIndex.searchViaRestHLClient(searchRequest);
        Double totalHits = (double) searchResponse.getHits().getTotalHits();
        if (totalHits == 0L) {
            return null;
        }
        Map responseMap = new HashMap();

        Aggregations aggregations = searchResponse.getAggregations().get("date_histogram");

        Terms weekdayTerms = searchResponse.getAggregations().get("by_day_of_week");

        // weekday map
        weekdayTerms.getBuckets().stream().forEach(weekdayBucket -> {
            responseMap.put(weekdayBucket.getKeyAsString(), weekdayBucket.getDocCount());
        });

        return responseMap;

    } catch (Exception e) {
        log.error("Error in searching.", e);
    }
    return null;
}

```

**//Error logs.**

```
error":{"root_cause":[{"type":"illegal_argument_exception","reason":"[script] unknown field [source], parser not found"}],"type":"illegal_argument_exception","reason":"[script] unknown field [source], parser not found"},"status":400}
	at org.elasticsearch.client.RestClient$1.completed(RestClient.java:354) ~[rest-5.5.3.jar:5.6.2]
	at org.elasticsearch.client.RestClient$1.completed(RestClient.java:343) ~[rest-5.5.3.jar:5.6.2]
	at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) ~[httpcore-4.4.5.jar:4.4.5]
	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177) ~[httpasyncclient-4.1.2.jar:4.1.2]
	at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436) ~[httpcore-nio-4.4.5.jar:4.4.5]
	at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326) ~[httpcore-nio-4.4.5.jar:4.4.5]
	at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265) ~[httpcore-nio-4.4.5.jar:4.4.5]

```

 ![05 PM](https://us1.discourse-cdn.com/elastic/original/3X/e/c/ec14943c832810783bf830e08448d4d2eb96362e.png) ![22 PM](https://us1.discourse-cdn.com/elastic/original/3X/d/2/d211865f05070ac8b3b00629e5b3c29187d27a8c.png)

Regards  
Jay

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 30, 2017, 2:45pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/4 "2017-11-30T14:45:29Z")

</div>

@jpmiitian trying to reproduce, just to make sure I'm looking at the right thing: which HL client version are you using exactly and which version is the elasticsearch server that you are querying?

---

<div class="post-metadata">

**Author:** ![jpmiitian](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jpmiitian](https://discuss.elastic.co/u/jpmiitian)\
**Post date:** [November 30, 2017, 3:08pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/5 "2017-11-30T15:08:45Z")

</div>

Hi @cbuescher  
HL version is 5.6.2 and ES is 5

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 30, 2017, 3:45pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/6 "2017-11-30T15:45:59Z")

</div>

Which ES version exactly?

The problem is probably that the 5.6.2 client renders the Script class as the following Json:

```auto
{
	"source": "doc['eventTime'].date.dayOfWeek",
	"lang": "painless"
}

```

But older versions of Elasticsearch (I think everything older than 5.5) can only parse:

```auto

{
	"inline": "doc['eventTime'].date.dayOfWeek",
	"lang": "painless"
}

```

The renaming of `inline` to `script` was done in [https://github.com/elastic/elasticsearch/pull/25127](https://github.com/elastic/elasticsearch/pull/25127) and both parameter names are supported in 5.6 and the 6.x.

Your backend should be on at least 5.6. so this can work. In general, the HL client is supposed to be [forward compatible](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-compatibility.html), while there may be bw comp issues even for non breaking changes made to ES core.

---

<div class="post-metadata">

**Author:** ![jpmiitian](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jpmiitian](https://discuss.elastic.co/u/jpmiitian)\
**Post date:** [November 30, 2017, 4:34pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/7 "2017-11-30T16:34:49Z")

</div>

Thanks @cbuescher for informing regarding the compatibility.

The ES version is 5.5,  
I noted that below was working on kibana console.  
`{ "inline": "doc['eventTime'].date.dayOfWeek", "lang": "painless" }`

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 30, 2017, 4:51pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/8 "2017-11-30T16:51:04Z")

</div>

Yes, it works as long as you specify the query manually and use "inline" instead of "source". The 5.6 client uses "source" when rendering the REST request though, since this is the preferred variant of the parameter name starting with 5.6.

---

<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:** [December 28, 2017, 4:51pm UTC](https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/9 "2017-12-28T16:51:19Z")

</div>

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