# How to retrieve sum from elasticSearch using java

**URL:** <https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158>\
**Category:** Elasticsearch\
**Created:** [January 30, 2020, 10:47am UTC](https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158 "2020-01-30T10:47:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toshif](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Toshif](https://discuss.elastic.co/u/Toshif)\
**Post date:** [January 30, 2020, 10:47am UTC](https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158/1 "2020-01-30T10:47:27Z")

</div>

Hello,  
I am having 5 fileds (viz. A,B,C & D) in my index.  
I want to retrieve sum of field B for my query using java.  
Can any1 explain me to do so ?  
I've gone through Elastic Docs about aggregation, but did not understand how to use it in java code

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 30, 2020, 11:49am UTC](https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158/2 "2020-01-30T11:49:56Z")

</div>

Are you able to do it from the Kibana dev console? Without Java I meant but just by using the API?

If so, could you share the query?  
If not, start by writing it without Java just using the API. Then translating to Java is pretty much obvious.

And we can help from this script.

---

<div class="post-metadata">

**Author:** ![Toshif](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Toshif](https://discuss.elastic.co/u/Toshif)\
**Post date:** [January 30, 2020, 11:56am UTC](https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158/3 "2020-01-30T11:56:48Z")

</div>

I want to write in java using elastic API, but I'm unable to understand how to use those Aggregation (SUM) in java

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 30, 2020, 12:11pm UTC](https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158/4 "2020-01-30T12:11:35Z")

</div>

Here is the documentation:

- [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html#java-rest-high-search-request-building-aggs](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html#java-rest-high-search-request-building-aggs)
- [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-aggregation-builders.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-aggregation-builders.html)

---

<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:** [February 27, 2020, 12:11pm UTC](https://discuss.elastic.co/t/how-to-retrieve-sum-from-elasticsearch-using-java/217158/5 "2020-02-27T12:11:43Z")

</div>

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