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


(Khurram Khan) #1

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


(Khurram Khan) #2

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());

(system) #3