# Stats aggregation on list length

**URL:** <https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772>\
**Category:** Elasticsearch\
**Created:** [January 22, 2015, 10:10am UTC](https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772 "2015-01-22T10:10:21Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [January 23, 2015, 10:11am UTC](https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772/3 "2015-01-23T10:11:55Z")

</div>

Thanks! Now it works.

Best,

Jilles

On Friday, January 23, 2015 at 3:04:55 AM UTC+1, Masaru Hasegawa wrote:

> Hi,
> 
> Objects are flattened in index level. Nothing is indexed as “member”  
> that’s why you get the exception.  
> Using doc[‘members.name'] instead of doc[‘members’] in script should  
> work.
> 
> Masaru
> 
> On January 22, 2015 at 19:10:25, Jilles van Gurp ([jilles...@gmail.com](mailto:jilles...@gmail.com)  
> \<javascript:\>) wrote:
> 
> > I'm trying to do a stats aggregation on the list length using a script  
> > but  
> > I'm getting errors. For this data,
> > 
> > PUT test\_groups/group/1  
> > {  
> > "name":"1",  
> > "members":[  
> > {  
> > "name":"m1"  
> > }  
> > ]  
> > }
> > 
> > PUT test\_groups/group/2  
> > {  
> > "name":"2",  
> > "members":[  
> > {  
> > "name":"m1"  
> > },  
> > {  
> > "name":"m2"  
> > }  
> > ]  
> > }
> > 
> > and this query:
> > 
> > GET test\_groups/group/\_search  
> > {  
> > "aggs": {  
> > "group\_members": {  
> > "filter": {  
> > "exists": {  
> > "field": "members"  
> > }  
> > },  
> > "aggs": {  
> > "length": {  
> > "stats": {  
> > "script": "doc['members'].values.length"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > I get an error stating that the members field does not exist in type  
> > group:
> > 
> > {  
> > "took": 4,  
> > "timed\_out": false,  
> > "\_shards": {  
> > "total": 5,  
> > "successful": 3,  
> > "failed": 2,  
> > "failures": [  
> > {  
> > "index": "test\_groups",  
> > "shard": 2,  
> > "status": 500,  
> > "reason": "QueryPhaseExecutionException[[test\_groups][2]:  
> > query[ConstantScore(cache(\_type:group))],from[0],size[10]: Query Failed  
> > [Failed to execute main query]]; nested:  
> > GroovyScriptExecutionException[ElasticsearchIllegalArgumentException[No
> 
> > field found for [members] in mapping with types [group]]]; "  
> > },  
> > {  
> > "index": "test\_groups",  
> > "shard": 3,  
> > "status": 500,  
> > "reason": "QueryPhaseExecutionException[[test\_groups][3]:  
> > query[ConstantScore(cache(\_type:group))],from[0],size[10]: Query Failed  
> > [Failed to execute main query]]; nested:  
> > GroovyScriptExecutionException[ElasticsearchIllegalArgumentException[No
> 
> > field found for [members] in mapping with types [group]]]; "  
> > }  
> > ]  
> > },  
> > "hits": {  
> > "total": 0,  
> > "max\_score": null,  
> > "hits":   
> > },  
> > "aggregations": {  
> > "group\_members": {  
> > "doc\_count": 0,  
> > "length": {  
> > "count": 0,  
> > "min": null,  
> > "max": null,  
> > "avg": null,  
> > "sum": null  
> > }  
> > }  
> > }  
> > }
> > 
> > Is there a way to do this?
> > 
> > Best regards,
> > 
> > Jilles
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/fcbce46c-6556-4e4e-b74a-2a4cbea915c6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fcbce46c-6556-4e4e-b74a-2a4cbea915c6%40googlegroups.com).
> 
> > 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/b3f877a9-633e-4fe8-b4ea-3869e833782f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b3f877a9-633e-4fe8-b4ea-3869e833782f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

_[View the full topic](https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772)._
