# Multiple nested objects stats aggregation

**URL:** https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360
**Category:** Elasticsearch
**Created:** [December 12, 2017, 2:36pm UTC](https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360 "2017-12-12T14:36:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![johntadd](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@johntadd](https://discuss.elastic.co/u/johntadd)
#### Post date: [December 12, 2017, 2:36pm UTC](https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360/1 "2017-12-12T14:36:31Z")

</div>

I can retrieve docs based on ['And' effect search for multiple nested object](https://discuss.elastic.co/t/and-effect-search-for-multiple-nested-object/111148). A new problem is how can I count returned result?

Previous nested query returns 2 docs.

```
Doc 1
    * rank 1: user A
    * rank 2: user B
    * rank 3: user C 

Doc 2
    * rank 1: user A
    * rank 2: user A
    * rank 3: user D

```

Now I want to count the number of user A in rank 1. It's expected to be 2, but the aggregation search I use returns 3 (because it include rank 2 as well). The aggregation search I use is

```
{
    "query": { ...query used in the previous 'and' effect thread }
    "aggs": {
        "stats": {
            "nested": {
                "path": "level1tag.level2tag.level3tag"
            },
            "aggs": {
                 "stats": {
                     "nested": {
                         "path": "level1tag.level2tag.level3tag.level4tag"
                     },
                     "aggs": {
                         "count": {
                             "terms": {
                                 "field": "level1tag.level2tag.level3tag.level4tag.level4_attr1.keyword" 
                             }
                         }
                     }
                 }
            }
        }
    }
}

```

How can I adjust so that I can accurately calculate the result count?

Thanks

---

<div class="post-metadata">

### Author: ![johntadd](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@johntadd](https://discuss.elastic.co/u/johntadd)
#### Post date: [December 12, 2017, 3:56pm UTC](https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360/2 "2017-12-12T15:56:23Z")

</div>

After digging a bit, following aggs expression seems to do the trick but I am not sure if it's correct.

```
{
    "query": {
        "nested": {
            "path": "level1tag.level2tag.level3tag",
            "query": {
                "bool": {
                    "filter": {
                        "term": {
                            "level1tag.level2tag.level3tag.level3_attr1.keyword": "value A"
                        }
                    },
                    "must": [
                        {
                            "nested": {
                                "path": "level1tag.level2tag.level3tag.level4tag",
                                "query": {
                                    "bool": {
                                        "must": [
                                             {
                                                 "term": {
                                                     "level1tag.level2tag.level3tag.level4tag.levelt4_attr1.keyword": "value B"
                                                 }
                                             }
                                        ]
                                    }
                                }
                            } 
                        }
                    ]
                }
            }
        }
    },
    "aggs": {
        "group_by_level3tag": {
            "nested": {
                "path": "level1tag.level2tag.level3tag"
            },
            "aggs": {
                "group_by_level3_attr1": {
                    "terms": {
                        "field": "level1tag.level2tag.level3tag.level3_attr1.keyword"
                    },
                    "aggs": {
                        "filter_by_level4tag": {
                            "filter": {
                                "nested": {
                                    "path": "level1tag.level2tag.level3tag.level4tag",
                                    "query": {
                                        "bool": {
                                            "filter": {
                                                "term": {
                                                    "level1tag.level2tag.level3tag.level4tag.level4_attr1.keyword": "value B" 
                                                }  
                                            }
                                        }
                                    }
                                }
                            } 
                        }
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [December 12, 2017, 5:03pm UTC](https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360/3 "2017-12-12T17:03:19Z")

</div>

What do your json documents look like? It's unclear to me how you go from rank/user to levelXtag.

---

<div class="post-metadata">

### Author: ![johntadd](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@johntadd](https://discuss.elastic.co/u/johntadd)
#### Post date: [December 13, 2017, 2:09pm UTC](https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360/4 "2017-12-13T14:09:14Z")

</div>

Sorry my bad. Didn't notice that was too abstract. The docs returned its structure should look like one below (Apologize I do not have data at hand so based on my memory if it serves correctly)

```
level1tag : {
    ... other attrs or tags
    level2tag: {
        ... other attrs or tags 
        level3tag: {
            level3_attr1: "value A"
            level4tag: {
                level4_attr1: "value B"
                ... other attrs or tags 
            }
            ... other attrs or tags                 
        }
    }
}

```

So the returned stats

```
"aggregations": {
    "group_by_level3tag": {
        "doc_count": 6,
        "group_by_level4_attr1": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                { 
                    "key": "rank 3",
                    "doc_count": 2,
                    "filter_by_level4_attr1": {
                        "doc_count": 0
                    }
                },
                { 
                    "key": "rank 1",
                    "doc_count": 2,
                    "filter_by_level4_attr1": {
                        "doc_count": 2
                    }
                },
                { 
                    "key": "rank 2",
                    "doc_count": 2,
                    "filter_by_level4_attr1": {
                        "doc_count": 1
                    }
                }
            ]
        }
   }
}
```

---

<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 10, 2018, 2:09pm UTC](https://discuss.elastic.co/t/multiple-nested-objects-stats-aggregation/111360/5 "2018-01-10T14:09:45Z")

</div>

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