0

I am new to aggregations with Elasticsearch and am stuck with a very simple example taken from this link.

Basically I am trying to do the Java API version of this very simple working aggregation:

http://localhost:9200/cars/transactions/_search?search_type=count
{
 "aggs" : {
  "colors" : {
    "terms" : {
    "field" : "color"
   }
  }
 }
}

and this is the Java version I am trying to build but it returns empty buckets:

SearchResponse sr = client
            .prepareSearch("cars")
            .setTypes("transactions")
            .setSearchType(SearchType.COUNT)
            .addAggregation(AggregationBuilders.terms("colors").field("color"))
            .execute()
            .actionGet();

while using the Elastic DSL I get a proper response with buckets grouped by colors, but with the Java version I get an empty bucket.

Update

It turns out the code is correct, the issue I had is related to using it in a test case; when used against a running cluster it works.

2 Answers 2

0

I suspect your issue is not with your Java version of the request, which is just fine. I tried it on some test data I have and got the expected result. The cluster is running Elasticsearch 1.7.5.

The Java code snippet I used :

        final SearchResponse sr = client
            .prepareSearch("index")
            .setTypes("docType")
            .setSearchType(SearchType.COUNT)
            .addAggregation(AggregationBuilders.terms("aggName").field("fieldName"))
            .execute()
            .actionGet();

        System.out.println(sr);

The result I got :

{
    "took" : 130,
    "timed_out" : false,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "hits" : {
        "total" : 1927227,
        "max_score" : 0.0,
        "hits" : [ ]
    },
    "aggregations" : {
        "aggName" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {
                "key" : "key1",
                "doc_count" : 757843
            }, {
                "key" : "key2",
                "doc_count" : 620033
            }, {
                "key" : "key3",
                "doc_count" : 549351
            } ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

OMG... huge facepalm here... indeed, just tested and the code is working, the issue I had is because I had this inside a testcase and for some reason some queries would proper work against the embedded elastic while this one was failing... not sure what causes it but yes, tested against a running cluster it works.
0

You are doing SearchType as Count which is not correct for aggregations. Can you please remove searchtype and construct the query as below

SearchResponse sr = client
        .prepareSearch("cars")
        .setTypes("transactions")
        .addAggregation(AggregationBuilders.terms("colors").field("color"))
        .execute()
        .actionGet();

1 Comment

Hi, thank you for your suggestion, however I've tried this before(and double checked now) but still same results. Count is used here to avoid the fetch fase as described in this link: elastic.co/guide/en/elasticsearch/guide/1.x/…

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.