# How to get only aggregation values from elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674>\
**Category:** Elasticsearch\
**Created:** [May 22, 2014, 11:46am UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674 "2014-05-22T11:46:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [May 22, 2014, 11:46am UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/1 "2014-05-22T11:46:58Z")

</div>

Hi,

I want to get the average value of MEMORY field from my ES document. Below  
is the query I'm using for that. Here I'm getting the aggregation along  
with the hits Json also. Is there any way we can get the aggreation result  
only. Please suggest.

POST /virtualmachines/\_search  
{  
"query" : {  
"filtered" : {  
"query" : { "match" : {  
"CLOUD\_TYPE" : "CLOUDSTACK"  
}},  
"filter" : {  
"range" : { "NODE\_CREATE\_TIME" : { "from" : "2014-05-22  
14:11:35", "to" : "2014-05-22 14:33:35" }}  
}  
}  
},  
"aggs" : {  
"memory\_avg" : { "avg" : { "field" : "MEMORY" } }  
}  
}

_response_ :  
{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 3,  
"successful": 3,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "virtualmachines",  
"\_type": "nodes",  
"\_id": "102",  
"\_score": 1,  
"\_source": {  
"NODE\_ID": "12235",  
"CLOUD\_TYPE": "CLOUDSTACK",  
"NODE\_GROUP\_NAME": "JBOSS",  
"NODE\_CPU": "4GHZ",  
"NODE\_HOSTNAME": "[cloud.aricent.com](http://cloud.aricent.com)",  
"NODE\_NAME": "cloudstack-node1",  
"NODE\_PRIVATE\_IP\_ADDRESS": "10.123.124.125",  
"NODE\_PUBLIC\_IP\_ADDRESS": "125.31.108.72",  
"NODE\_INSTANCE\_ID": "cloudstack112",  
"NODE\_STATUS": "ACTIVE",  
"NODE\_CATEGORY\_ID": "13",  
"NODE\_CREATE\_TIME": "2014-05-22 14:23:04",  
"CPU\_SPEED": "500",  
"MEMORY": 512,  
"CPU\_USED": "0.03%"  
}  
}  
]  
},  
"aggregations": {  
"memory\_avg": {  
"value": 512  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a8538769-7ba5-4ad5-b190-5f0b8d25a26b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a8538769-7ba5-4ad5-b190-5f0b8d25a26b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![emeschitc](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@emeschitc](https://discuss.elastic.co/u/emeschitc)\
**Post date:** [May 22, 2014, 1:02pm UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/2 "2014-05-22T13:02:47Z")

</div>

On Thu, May 22, 2014 at 1:47 PM, bagui [via Elasticsearch Users] \<  
[ml-node+s115913n4056274h86@n3.nabble.com](mailto:ml-node+s115913n4056274h86@n3.nabble.com)\> wrote:

Hi,

> I want to get the average value of MEMORY field from my ES document. Below  
> is the query I'm using for that. Here I'm getting the aggregation along  
> with the hits Json also. Is there any way we can get the aggreation result  
> only. Please suggest.
> 
> POST /virtualmachines/\_search  
> {  
> "query" : {  
> "filtered" : {  
> "query" : { "match" : {  
> "CLOUD\_TYPE" : "CLOUDSTACK"  
> }},  
> "filter" : {  
> "range" : { "NODE\_CREATE\_TIME" : { "from" : "2014-05-22  
> 14:11:35", "to" : "2014-05-22 14:33:35" }}  
> }  
> }  
> },  
> "aggs" : {  
> "memory\_avg" : { "avg" : { "field" : "MEMORY" } }  
> }  
> }

This is how I do it:  
Simply add a post\_filter with a term query for a non-existent term or a  
value which is not possible (e.g. -1 for a positive integer field).  
I am sure there are cleaner ways but this works for me

em

---

<div class="post-metadata">

**Author:** ![emeschitc](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@emeschitc](https://discuss.elastic.co/u/emeschitc)\
**Post date:** [May 22, 2014, 1:10pm UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/3 "2014-05-22T13:10:56Z")

</div>

Hi  
On Thu, May 22, 2014 at 1:47 PM, bagui [via Elasticsearch Users] \<  
[ml-node+s115913n4056274h86@n3.nabble.com](mailto:ml-node+s115913n4056274h86@n3.nabble.com)\> wrote:

Hi,

> I want to get the average value of MEMORY field from my ES document. Below  
> is the query I'm using for that. Here I'm getting the aggregation along  
> with the hits Json also. Is there any way we can get the aggreation result  
> only. Please suggest.
> 
> POST /virtualmachines/\_search  
> {  
> "query" : {  
> "filtered" : {  
> "query" : { "match" : {  
> "CLOUD\_TYPE" : "CLOUDSTACK"  
> }},  
> "filter" : {  
> "range" : { "NODE\_CREATE\_TIME" : { "from" : "2014-05-22  
> 14:11:35", "to" : "2014-05-22 14:33:35" }}  
> }  
> }  
> },  
> "aggs" : {  
> "memory\_avg" : { "avg" : { "field" : "MEMORY" } }  
> }  
> }

This is how I do it:  
Simply add a post\_filter with a term query for a non-existent term or a  
value which is not possible (e.g. -1 for a positive integer field).  
I am sure there are cleaner ways but this works for me

em ​

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 22, 2014, 5:56pm UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/4 "2014-05-22T17:56:48Z")

</div>

You can set the size to 0.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

You will still get back the search metadata though.

--  
Ivan

On Thu, May 22, 2014 at 4:46 AM, Subhadip Bagui [i.bagui@gmail.com](mailto:i.bagui@gmail.com) wrote:

> Hi,
> 
> I want to get the average value of MEMORY field from my ES document. Below  
> is the query I'm using for that. Here I'm getting the aggregation along  
> with the hits Json also. Is there any way we can get the aggreation result  
> only. Please suggest.
> 
> POST /virtualmachines/\_search  
> {  
> "query" : {  
> "filtered" : {  
> "query" : { "match" : {  
> "CLOUD\_TYPE" : "CLOUDSTACK"  
> }},  
> "filter" : {  
> "range" : { "NODE\_CREATE\_TIME" : { "from" : "2014-05-22  
> 14:11:35", "to" : "2014-05-22 14:33:35" }}  
> }  
> }  
> },  
> "aggs" : {  
> "memory\_avg" : { "avg" : { "field" : "MEMORY" } }  
> }  
> }
> 
> _response_ :  
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 3,  
> "successful": 3,  
> "failed": 0  
> },  
> "hits": {  
> "total": 1,  
> "max\_score": 1,  
> "hits": [  
> {  
> "\_index": "virtualmachines",  
> "\_type": "nodes",  
> "\_id": "102",  
> "\_score": 1,  
> "\_source": {  
> "NODE\_ID": "12235",  
> "CLOUD\_TYPE": "CLOUDSTACK",  
> "NODE\_GROUP\_NAME": "JBOSS",  
> "NODE\_CPU": "4GHZ",  
> "NODE\_HOSTNAME": "[cloud.aricent.com](http://cloud.aricent.com)",  
> "NODE\_NAME": "cloudstack-node1",  
> "NODE\_PRIVATE\_IP\_ADDRESS": "10.123.124.125",  
> "NODE\_PUBLIC\_IP\_ADDRESS": "125.31.108.72",  
> "NODE\_INSTANCE\_ID": "cloudstack112",  
> "NODE\_STATUS": "ACTIVE",  
> "NODE\_CATEGORY\_ID": "13",  
> "NODE\_CREATE\_TIME": "2014-05-22 14:23:04",  
> "CPU\_SPEED": "500",  
> "MEMORY": 512,  
> "CPU\_USED": "0.03%"  
> }  
> }  
> ]  
> },  
> "aggregations": {  
> "memory\_avg": {  
> "value": 512  
> }  
> }  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/a8538769-7ba5-4ad5-b190-5f0b8d25a26b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a8538769-7ba5-4ad5-b190-5f0b8d25a26b%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/a8538769-7ba5-4ad5-b190-5f0b8d25a26b%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a8538769-7ba5-4ad5-b190-5f0b8d25a26b%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCfKh\_qbSiEJtj\_%3DS%3DZ5Q43Lw3V4egRVEdsvz46FvvqPw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCfKh_qbSiEJtj_%3DS%3DZ5Q43Lw3V4egRVEdsvz46FvvqPw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [May 23, 2014, 6:00am UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/5 "2014-05-23T06:00:50Z")

</div>

Thanks Ivan for your reply.  
I tried with _size : 0_ but still the metadata is coming. Is there any way  
to suppress the metadata.  
Can I use java api to do so. Earlier I used the method like below to get  
the source response only. Can we do that for aggregation also?

public String searchESIndex(String index, String type) {  
Client client = ESClientFactory.getInstance();  
SearchRequestBuilder srequest = client.prepareSearch(index)  
.setTypes(type).setSearchType(SearchType.QUERY\_AND\_FETCH)  
.setQuery(QueryBuilders.matchQuery("status", "SUSPEND"))  
.setSize(1);  
SearchResponse sresp = srequest.execute().actionGet();  
List\<Map\<String, Object\>\> listOfSource = new ArrayList\<Map\<String,  
Object\>\>();  
for (SearchHit hits : sresp.getHits()) {  
listOfSource.add(hits.getSource());  
}  
Gson gson = new Gson();  
String jsonSourceList = gson.toJson(listOfSource);  
return jsonSourceList;  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/47f686b0-e844-410b-955e-6dfb88221569%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/47f686b0-e844-410b-955e-6dfb88221569%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 23, 2014, 6:09am UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/6 "2014-05-23T06:09:12Z")

</div>

As mentioned before, you will still get the metadata. With the Java API,  
you can get the aggregations back directly from the SearchResponse object,  
which is a list of Aggregation objects:

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/action/search/SearchResponse.java#L103-L105](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/action/search/SearchResponse.java#L103-L105)

--  
Ivan

On Thu, May 22, 2014 at 11:00 PM, Subhadip Bagui [i.bagui@gmail.com](mailto:i.bagui@gmail.com) wrote:

> Thanks Ivan for your reply.  
> I tried with _size : 0_ but still the metadata is coming. Is there any  
> way to suppress the metadata.  
> Can I use java api to do so. Earlier I used the method like below to get  
> the source response only. Can we do that for aggregation also?
> 
> public String searchESIndex(String index, String type) {  
> Client client = ESClientFactory.getInstance();  
> SearchRequestBuilder srequest = client.prepareSearch(index)  
> .setTypes(type).setSearchType(SearchType.QUERY\_AND\_FETCH)  
> .setQuery(QueryBuilders.matchQuery("status", "SUSPEND"))  
> .setSize(1);  
> SearchResponse sresp = srequest.execute().actionGet();  
> List\<Map\<String, Object\>\> listOfSource = new ArrayList\<Map\<String,  
> Object\>\>();  
> for (SearchHit hits : sresp.getHits()) {  
> listOfSource.add(hits.getSource());  
> }  
> Gson gson = new Gson();  
> String jsonSourceList = gson.toJson(listOfSource);  
> return jsonSourceList;  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/47f686b0-e844-410b-955e-6dfb88221569%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/47f686b0-e844-410b-955e-6dfb88221569%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/47f686b0-e844-410b-955e-6dfb88221569%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/47f686b0-e844-410b-955e-6dfb88221569%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA-Q\_s51SuWOpcqPc-ALu\_bWVy6ehsMcH59KoGYH9Hyhw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA-Q_s51SuWOpcqPc-ALu_bWVy6ehsMcH59KoGYH9Hyhw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [May 23, 2014, 7:26am UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/7 "2014-05-23T07:26:50Z")

</div>

Thanks a lot Ivan. Will try with Java API as u suggested.

One more doubt. There is a field in my document like "CPU\_USED": "0.03%"  
Can we do aggregation avg on this field ? I tried but got exception like  
below as this was a string field. Can u please suggest.

"reason": "QueryPhaseExecutionException[[virtualmachines][2]:  
query[filtered(CLOUD\_TYPE:cloudstack)-\>cache(NODE\_CREATE\_TIME:[1400767895000  
TO 1400769215999])],from[0],size[0]: Query Failed [Failed to execute main  
query]]; nested: AggregationExecutionException[Unsupported script value  
[0.03]]; "  
_Query :_  
POST /virtualmachines/\_search  
{  
"query" : {  
"filtered" : {  
"query" : { "match" : {  
"CLOUD\_TYPE" : "CLOUDSTACK"  
}},  
"filter" : {  
"range" : { "NODE\_CREATE\_TIME" : { "from" : "2014-05-22  
14:11:35", "to" : "2014-05-22 14:33:35" }}  
}  
}  
},  
"aggs" : {  
"avg\_grade" : { "avg" : { "script" : "doc['CPU\_USED'].value" } }  
},  
"size": 0  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/bd12c67b-9369-4231-90ec-a818a6e9be9a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bd12c67b-9369-4231-90ec-a818a6e9be9a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 23, 2014, 3:43pm UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/8 "2014-05-23T15:43:34Z")

</div>

I have yet to use aggregations (still on facets), so I am making  
assumptions here. Your field is a string ("0.03%"), so the average cannot  
be computed. Since you pass in a script, you can try doing some string  
manipulation, and perhaps converting to a numeric field if it is required.

Something like: doc['CPU\_USED'].value.replace("%", "") (not tested)

--  
Ivan

On Fri, May 23, 2014 at 12:26 AM, Subhadip Bagui [i.bagui@gmail.com](mailto:i.bagui@gmail.com) wrote:

> Thanks a lot Ivan. Will try with Java API as u suggested.
> 
> One more doubt. There is a field in my document like "CPU\_USED": "0.03%"  
> Can we do aggregation avg on this field ? I tried but got exception like  
> below as this was a string field. Can u please suggest.
> 
> "reason": "QueryPhaseExecutionException[[virtualmachines][2]:  
> query[filtered(CLOUD\_TYPE:cloudstack)-\>cache(NODE\_CREATE\_TIME:[1400767895000  
> TO 1400769215999])],from[0],size[0]: Query Failed [Failed to execute main  
> query]]; nested: AggregationExecutionException[Unsupported script value  
> [0.03]]; "  
> _Query :_  
> POST /virtualmachines/\_search  
> {  
> "query" : {  
> "filtered" : {  
> "query" : { "match" : {  
> "CLOUD\_TYPE" : "CLOUDSTACK"  
> }},  
> "filter" : {  
> "range" : { "NODE\_CREATE\_TIME" : { "from" : "2014-05-22  
> 14:11:35", "to" : "2014-05-22 14:33:35" }}  
> }  
> }  
> },  
> "aggs" : {  
> "avg\_grade" : { "avg" : { "script" : "doc['CPU\_USED'].value" } }  
> },  
> "size": 0  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/bd12c67b-9369-4231-90ec-a818a6e9be9a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bd12c67b-9369-4231-90ec-a818a6e9be9a%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/bd12c67b-9369-4231-90ec-a818a6e9be9a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/bd12c67b-9369-4231-90ec-a818a6e9be9a%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAM%2BxOoXFRRs59WdLHYfa0P0RjOnuXZp1a1VPpa\_qgjQQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAM%2BxOoXFRRs59WdLHYfa0P0RjOnuXZp1a1VPpa_qgjQQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:27am UTC](https://discuss.elastic.co/t/how-to-get-only-aggregation-values-from-elasticsearch/17674/9 "2017-07-06T01:27:22Z")

</div>


