AggregationBuilders throws an error JsonGenerationException

I am using elasticsearch API (elasticsearch5.2.1.jar)to do the aggregation query as follows,

StatsAggregationBuilder statAggBuild = AggregationBuilders.stats("agg").field("ipAddress");

This returns the follows exception

{ "error" : "JsonGenerationException[Can not write a field name, expecting a value]"}

Computing average on an IP address?

Getting the Count from IP address.

Also I have tried ValueCountAggregationBuilder, It is also throwing the same error

I might need to see more of your client code to recreate.

I suggest looking at the cardinality agg for more effiicient counting of uniques .

import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder;

public class TestFile 
{
	public static void main(String[] args) 
	{
		try 
		{
			StatsAggregationBuilder statAggBuild = AggregationBuilders.stats("agg").field("clientip");
			
			System.out.println(statAggBuild);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

Above is the code i am using, It throws the error as follows,

{ "error" : "JsonGenerationException[Can not write a field name, expecting a value]"}

Try this:

    TransportClient client = new PreBuiltXPackTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));               
    StatsAggregationBuilder aggs = AggregationBuilders.stats("stats").field("rating");
    SearchResponse searchResponse = client.prepareSearch("reviews").addAggregation(aggs).get();
    InternalStats stats = searchResponse.getAggregations().get("stats");
    System.out.println("Average stat =" + stats.getAvg());
    client.close();

It works as expected.

I think there is a problem in the toString method. Which throws the exception while called.

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