# 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:** 4\
**Page:** 1

<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 22, 2015, 10:10am UTC](https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772/1 "2015-01-22T10:10:21Z")

</div>

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
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).

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [January 23, 2015, 2:03am UTC](https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772/2 "2015-01-23T02:03:42Z")

</div>

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 ([jillesvangurp@gmail.com](mailto:jillesvangurp@gmail.com)) 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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> 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/etPan.54c1ac3b.7fdcc233.1877%40citra.local](https://groups.google.com/d/msgid/elasticsearch/etPan.54c1ac3b.7fdcc233.1877%40citra.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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).

---

<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, 12:37am UTC](https://discuss.elastic.co/t/stats-aggregation-on-list-length/21772/4 "2017-07-06T00:37:05Z")

</div>

Closed on July 6, 2017, 12:37am UTC


