# AWS Elasticsearch Avg aggregation Java API generates error when trying to retrieve results using a Jest client during a certain time period

**URL:** <https://discuss.elastic.co/t/aws-elasticsearch-avg-aggregation-java-api-generates-error-when-trying-to-retrieve-results-using-a-jest-client-during-a-certain-time-period/63465>\
**Category:** Elasticsearch\
**Created:** [October 20, 2016, 5:06am UTC](https://discuss.elastic.co/t/aws-elasticsearch-avg-aggregation-java-api-generates-error-when-trying-to-retrieve-results-using-a-jest-client-during-a-certain-time-period/63465 "2016-10-20T05:06:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![khurram.khan](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@khurram.khan](https://discuss.elastic.co/u/khurram.khan)\
**Post date:** [October 20, 2016, 5:06am UTC](https://discuss.elastic.co/t/aws-elasticsearch-avg-aggregation-java-api-generates-error-when-trying-to-retrieve-results-using-a-jest-client-during-a-certain-time-period/63465/1 "2016-10-20T05:06:53Z")

</div>

I am using Elasticsearch SDK 2.3 and Jest 2.0.0 to create a client. I am trying to get average aggregation (and other aggregations) implemented which will retrieve results from a certain time period.

```
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());

GregorianCalendar gC = new GregorianCalendar();
gC.set(2016, Calendar.OCTOBER, 18, 0, 0, 0);
long from = gC.getTimeInMillis();
gC.add(Calendar.MINUTE, 15);
long to = gC.getTimeInMillis();

searchSourceBuilder.postFilter(QueryBuilders.rangeQuery("timestamp").from(from).to(to));

JestClient client = getJestClient();

AvgBuilder aggregation2 = AggregationBuilders
        .avg(AvgAggregation.TYPE)
        .field("backend_processing_time");

AggregationBuilder ag = AggregationBuilders.dateRange(aggregation2.getName())
        .addRange("timestamp", from, to).
        subAggregation(aggregation2);

searchSourceBuilder.aggregation(ag);
String query = searchSourceBuilder.toString();
Search search = new Search.Builder(query)
        .addIndex(INDEX)
        .addType(TYPE)
        // .addSort(new Sort("code"))
        .setParameter(Parameters.SIZE, 5)
        // .setParameter(Parameters.SCROLL, "5m")
        .build();

SearchResult result = client.execute(search);
System.out.println("ES Response with aggregation:\n" + result.getJsonString());

```

AWS Elasticsearch is returning an error which is as follows:

> {"error":{"root\_cause":[{"type":"aggregation\_execution\_exception","reason":"could not find the appropriate value context to perform aggregation [avg]"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"elbaccesslogs\_2016\_10","node":"5ttEmYcTTsie-z23OpKY0A","reason":{"type":"aggregation\_execution\_exception","reason":"could not find the appropriate value context to perform aggregation [avg]"}}]},"status":500}

Looks like the reason is 'could not find the appropriate value context to perform aggregation [avg]' ... which I don't know really why it is occurring since I haven't hard coded anything like '[avg]'.

Can anyone suggest anything please? Or if need more info on this before responding, please let me know.

Thanks,

Khurram

---

<div class="post-metadata">

**Author:** ![khurram.khan](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@khurram.khan](https://discuss.elastic.co/u/khurram.khan)\
**Post date:** [October 20, 2016, 10:51am UTC](https://discuss.elastic.co/t/aws-elasticsearch-avg-aggregation-java-api-generates-error-when-trying-to-retrieve-results-using-a-jest-client-during-a-certain-time-period/63465/2 "2016-10-20T10:51:50Z")

</div>

I have found the solution myself so I am explaining below and closing the issue which is as follows:

Basically the code

```
searchSourceBuilder.postFilter(QueryBuilders.rangeQuery("timestamp").from(from).to(to)); 

```

doesn't work with aggregations. We need to remove 'postFilter' code and the following code:

```
AggregationBuilder ag = AggregationBuilders.dateRange(aggregation2.getName())
    .addRange("timestamp", from, to).
    subAggregation(aggregation2);

```

And change the following code too:

```
searchSourceBuilder.query(QueryBuilders.matchAllQuery());

```

to

```
searchSourceBuilder.query(
            QueryBuilders.boolQuery().
                    must(QueryBuilders.matchAllQuery()).
                    filter(QueryBuilders.rangeQuery("timestamp").from(from).to(to))
    );

```

So here is the entire code again:

```
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    GregorianCalendar gC = new GregorianCalendar();
    gC.set(2016, Calendar.OCTOBER, 18, 0, 0, 0);
    long from = gC.getTimeInMillis();
    gC.add(Calendar.MINUTE, 15);
    long to = gC.getTimeInMillis();

    searchSourceBuilder.query(
            QueryBuilders.boolQuery().
                    must(QueryBuilders.matchAllQuery()).
                    filter(QueryBuilders.rangeQuery("timestamp").from(from).to(to))
    );

    JestClient client = getJestClient(); // just a private method creating a client the way Jest client is created

    AvgBuilder avg_agg = AggregationBuilders
            .avg(AvgAggregation.TYPE)
            .field("backend_processing_time");

    searchSourceBuilder.aggregation(avg_agg);
    
    String query = searchSourceBuilder.toString();

    Search search = new Search.Builder(query)
            .addIndex(INDEX)
            .addType(TYPE)
            .setParameter(Parameters.SIZE, 10)
            .build();

    SearchResult result = client.execute(search);
    System.out.println("ES Response with aggregation:\n" + result.getJsonString());
```

---

<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:** [July 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/aws-elasticsearch-avg-aggregation-java-api-generates-error-when-trying-to-retrieve-results-using-a-jest-client-during-a-certain-time-period/63465/3 "2017-07-05T22:10:54Z")

</div>


