# Avg aggregation on string values

**URL:** <https://discuss.elastic.co/t/avg-aggregation-on-string-values/17875>\
**Category:** Elasticsearch\
**Created:** [June 3, 2014, 8:14am UTC](https://discuss.elastic.co/t/avg-aggregation-on-string-values/17875 "2014-06-03T08:14:27Z")\
**Posts on this page:** 3\
**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:** [June 3, 2014, 8:14am UTC](https://discuss.elastic.co/t/avg-aggregation-on-string-values/17875/1 "2014-06-03T08:14:27Z")

</div>

Hi,

I've below kind of documents in elasticsearch which is coming from 3rd  
party api and there are values upon which I want to do aggregation.

{  
"\_index": "virtualmachines",  
"\_type": "nodes",  
"\_id": "103",  
"\_score": 1,  
"\_source": {  
"NODE\_ID": "12335",  
"CLOUD\_TYPE": "AWS-EC2",  
"NODE\_GROUP\_NAME": "MYSQL",  
"NODE\_CPU": "4GHZ",  
"NODE\_HOSTNAME": "[cloud.aricent.com](http://cloud.aricent.com)",  
"NODE\_NAME": "aws-node1",  
"NODE\_PRIVATE\_IP\_ADDRESS": "10.123.124.126",  
"NODE\_PUBLIC\_IP\_ADDRESS": "125.31.108.73",  
"NODE\_INSTANCE\_ID": "aws111",  
"NODE\_STATUS": "INACTIVE",  
"NODE\_CATEGORY\_ID": "2",  
"NODE\_CREATE\_TIME": "2014-05-22 14:40:35",  
"CPU\_SPEED": "500",  
"MEMORY": 512,  
"CPU\_USED": "0.02%"  
}

Here is my code from where I do aggregation

SearchResponse response = client.prepareSearch("virtualmachines")  
.setTypes("nodes").setQuery(QueryBuilders.matchAllQuery())  
//.addAggregation(AggregationBuilders.avg("mem\_average").field("CPU\_SPEED"))  
.addAggregation(AggregationBuilders.avg("mem\_average").script("doc['CPU\_USED'].value"))  
.execute().actionGet();  
_error :_  
Exception in thread "main"  
org.elasticsearch.transport.TransportSerializationException: Failed to  
deserialize exception response from stream  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:169)

the mapping for CPU\_SPEED is  
"CPU\_USED": {  
"type": "string"  
}

If I change the mapping to type long then the same is working. Is there any  
way to get the string value and do the aggregation.

Thanks,  
Subhadip

--  
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/bff62cd0-65af-4090-90d3-2d14aeb3363a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bff62cd0-65af-4090-90d3-2d14aeb3363a%40googlegroups.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:** [June 3, 2014, 2:56pm UTC](https://discuss.elastic.co/t/avg-aggregation-on-string-values/17875/2 "2014-06-03T14:56:19Z")

</div>

Hi,

please suggest...

--  
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/cef8d6e2-95f7-4040-9683-df188f1717cb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cef8d6e2-95f7-4040-9683-df188f1717cb%40googlegroups.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:24am UTC](https://discuss.elastic.co/t/avg-aggregation-on-string-values/17875/3 "2017-07-06T01:24:55Z")

</div>


