# Group by terms and get count of nested array property?

**URL:** https://discuss.elastic.co/t/group-by-terms-and-get-count-of-nested-array-property/211823
**Category:** Elasticsearch
**Created:** [December 13, 2019, 5:11pm UTC](https://discuss.elastic.co/t/group-by-terms-and-get-count-of-nested-array-property/211823 "2019-12-13T17:11:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tzn01](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@tzn01](https://discuss.elastic.co/u/tzn01)
#### Post date: [December 13, 2019, 5:11pm UTC](https://discuss.elastic.co/t/group-by-terms-and-get-count-of-nested-array-property/211823/1 "2019-12-13T17:11:50Z")

</div>

I would like to get the count from a document series where an array item matches some value.

I have documents like these:

```auto
{
    "Name": "jason",
    "Todos": [{
        "State": "COMPLETED"
        "Timer": 10
        },{
        "State": "PENDING"
        "Timer": 5
    }]
}

{
    "Name": "jason",
    "Todos": [{
        "State": "COMPLETED"
        "Timer": 5
        },{
        "State": "PENDING"
        "Timer": 2
    }]
}

{
    "Name": "martin",
    "Todos": [{
        "State": "COMPLETED"
        "Timer": 15
        },{
        "State": "PENDING"
        "Timer": 10
    }]
}

```

I would like to count how many documents I have where they have any Todos with COMPLETED State. And group by Name.

So from the above I would need to get: jason: 2 martin: 1

Usually I do this with a term aggregation for the Name, and an other sub aggregation for other items:

```auto
"aggs": {
    "statistics": {
        "terms": {
            "field": "Name"
        },
        "aggs": {
            "test": {
                "filter": {
                    "bool": {
                        "must": [{
                                "match_phrase": {
                                    "SomeProperty.keyword": {
                                        "query": "THEVALUE"
                                    }
                                }
                            }
                        ]
                    }
                },

```

But not sure how to do this here as I have items in an array.

---

<div class="post-metadata">

### Author: ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)
#### Post date: [December 14, 2019, 7:55am UTC](https://discuss.elastic.co/t/group-by-terms-and-get-count-of-nested-array-property/211823/2 "2019-12-14T07:55:25Z")

</div>

What type of field is `Todo`, an `object` type or `nested`?

---

<div class="post-metadata">

### Author: ![tzn01](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@tzn01](https://discuss.elastic.co/u/tzn01)
#### Post date: [December 14, 2019, 10:37am UTC](https://discuss.elastic.co/t/group-by-terms-and-get-count-of-nested-array-property/211823/3 "2019-12-14T10:37:02Z")

</div>

State has the following mapping:

```
"State": {
	"type": "text",
	"fields": {
		"keyword": {
			"type": "keyword",
			"ignore_above": 256
		}
	}
},

```

But I realized I made a mistake and put a filter into the query (not in the example) which resulted 0 result, and actually the above mentioned query works when I fix my time range filer. But according to what I have found online it should not work, because I should use nested type fields. I have a feeling it works only because I also group the aggregation by Name using the terms aggregation.

---

<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: [January 11, 2020, 10:37am UTC](https://discuss.elastic.co/t/group-by-terms-and-get-count-of-nested-array-property/211823/4 "2020-01-11T10:37:06Z")

</div>

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