We have answer of your question!

100% solved queries, no empty question

Question: Java API aggregation with elastic search 1.x


0

I am new to aggregations with Elastic Search and have been stuck for a couple of hours with this very simple example taken from this link: https://www.elastic.co/guide/en/elasticsearch/guide/1.x/_aggregation_test_drive.html

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 resopnse 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.

Question author Marcos-maia | Source

Answer


1


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
            } ]
        }
    }
}
Answer author Emmanuel-guiton

Tickanswer.com is providing the only single recommended solution of the question Java API aggregation with elastic search 1.x under the categories i.e java , elasticsearch , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!