# I don't get the same results with Java API and Kibana

**URL:** <https://discuss.elastic.co/t/i-dont-get-the-same-results-with-java-api-and-kibana/59445>\
**Category:** Elasticsearch\
**Created:** [August 31, 2016, 4:15pm UTC](https://discuss.elastic.co/t/i-dont-get-the-same-results-with-java-api-and-kibana/59445 "2016-08-31T16:15:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [August 31, 2016, 4:15pm UTC](https://discuss.elastic.co/t/i-dont-get-the-same-results-with-java-api-and-kibana/59445/1 "2016-08-31T16:15:26Z")

</div>

Hello everyone,

**Here is what I have:**  
I have an index in elasticseach storing thousands of documents. Each document includes a field called 'deviceName'.

**Here is what I would like to do with java api:**  
I want to get all devices stored in my index and their occurence (number of time a device appears in this index).

**Here is my java code:**

```
String[] indexVector = new String[1];
     indexVector[0] = "myIndex";
     String [] values = new String[2];
    values[0] = "sum_delta";
    values[1] = "count_frames";
    String [] totalPercentage = new String[1];
    totalPercentage[0] = "devices>per_percentage";
    SearchResponse response = (SearchResponse) client.prepareSearch(indexVector)
            .setSize(10000)
            .addAggregation(AggregationBuilders.terms("devices").field("deviceName")
                    .size(100)
                    .subAggregation(AggregationBuilders.sum("sum_delta").field("delta"))
                    .subAggregation(AggregationBuilders.count("count_frames").field("cnt"))
                    .subAggregation(PipelineAggregatorBuilders.bucketScript("per_percentage")
                            .setBucketsPaths(values)
                            .script(new Script("_value0 / (_value0 + _value1 + 1)"))                
                             )
                    )
            .addAggregation(PipelineAggregatorBuilders.avgBucket("avg_per_all_devices")
                    .setBucketsPaths(totalPercentage)
                    )           
            .execute()
            .actionGet();

```

**How I search in Kibana:**

 ![](https://us1.discourse-cdn.com/elastic/original/2X/7/7239c78832f70309329ace642e907fc9a4d7440a.PNG)

**Here is my results:**  
With java API I get 63 devices and with kibana 64 devices. However, the biggest problem for me is the occurence. Indeed, for a device that has a occurence lower than 30, i have the same result with Java api and Kibana. But, when the occurence is larger, kibana returns a occurence greater than the one returned by java API. Example: kibana returns 1010 and in the same time java api returns 880.

I really don't understand why there is so much difference.

**Question:**  
So, please, can you tell me what is wrong in my code and what I have to do ?

Thank you for your attention and your help.

---

<div class="post-metadata">

**Author:** ![vinayakshukre](https://avatars.discourse-cdn.com/v4/letter/v/58f4c7/32.png) [@vinayakshukre](https://discuss.elastic.co/u/vinayakshukre)\
**Post date:** [September 6, 2016, 9:06am UTC](https://discuss.elastic.co/t/i-dont-get-the-same-results-with-java-api-and-kibana/59445/2 "2016-09-06T09:06:50Z")

</div>

In the SearchResponse json in your java code, check sum\_other\_doc\_count for your aggregation query. I think it is the number of documents which Elasticsearch has ignored while calculating your aggregations or on which aggregations were not calculated. Elasticsearch aggregations are not precise. They are approximate.

I am not sure if Kibana is using some other API exposed by elasticsearch to get better results.

---

<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:22pm UTC](https://discuss.elastic.co/t/i-dont-get-the-same-results-with-java-api-and-kibana/59445/3 "2017-07-05T22:22:20Z")

</div>


