# Wrong doc\_count on Aggregations

**URL:** <https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315>\
**Category:** Elasticsearch\
**Created:** [August 5, 2016, 8:31am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315 "2016-08-05T08:31:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joora](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@joora](https://discuss.elastic.co/u/joora)\
**Post date:** [August 5, 2016, 8:31am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/1 "2016-08-05T08:31:26Z")

</div>

Request:

```
{
  "query": {
    "bool": {
      "should": [
        {
          "match_all": {}
        }
      ],
      "must": [
        {
          "nested": {
            "path": "invitations",
            "query": {
              "terms": {
                "invitations.locationName": [
                  "rue de Lemaire"
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "invitations.locationName": {
      "nested": {
        "path": "invitations"
      },
      "aggs": {
        "invitations.locationName_nested": {
          "terms": {
            "field": "invitations.locationName",
            "size": 0
          }
        }
      }
    }
  },
  "from": 0,
  "size": 10
}

```

Response:

```
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 108,
...
},
"aggregations": {
"invitations.locationName": {
"doc_count": 369,
"invitations.locationName_nested": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "rue de Lemaire",
"doc_count": 123
},
...

```

=\> The filter doesn't correspond to the aggregation,  
hits = 108 vs agg doc\_count = 123 with the key "rue de Lemaire".  
Same issue with the other keys of this aggregation.  
Why? How to solve it?  
Thanks!

I don't have this issue with other aggs that are not nested.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 5, 2016, 8:37am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/2 "2016-08-05T08:37:20Z")

</div>

> [@joora](#):
>
> "\_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 108,...},"aggregations": {"invitations.locationName": {"doc\_count": 369,"invitations.locationName\_nested": {"doc\_count\_error\_upper\_bound": 0,"sum\_other\_doc\_count": 0,"buckets": [{"key": "rue de Lemaire","doc\_count": 123},

Because 369 and 123 are numbers of nested documents and 108 is the number of root-level docs?

---

<div class="post-metadata">

**Author:** ![joora](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@joora](https://discuss.elastic.co/u/joora)\
**Post date:** [August 5, 2016, 8:52am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/3 "2016-08-05T08:52:17Z")

</div>

Thank you,  
if you're right, what should I add on the query to have the root\_doc count insted of the nested one?

---

<div class="post-metadata">

**Author:** ![joora](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@joora](https://discuss.elastic.co/u/joora)\
**Post date:** [August 5, 2016, 9:20am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/4 "2016-08-05T09:20:00Z")

</div>

I got it!  
Source: [http://stackoverflow.com/questions/27553916/how-to-return-the-count-of-unique-documents-by-using-elasticsearch-aggregation](http://stackoverflow.com/questions/27553916/how-to-return-the-count-of-unique-documents-by-using-elasticsearch-aggregation)

Original:

```
"aggs": {
    "invitations.locationName_nested": {
        "terms": {
            "field": "invitations.locationName",
            "size": 0
        }
    }
}

```

Modified:

```
"aggs": {
    "invitations.locationName_nested": {
        "terms": {
            "field": "invitations.locationName",
            "size": 0
        },
        "aggs": {
            "top_reverse_nested": {
                "reverse_nested": {}
            }
        }
    }
}

```

And in the response I have:

```
"buckets": [
{
    "key": "rue de Lemaire",
    "doc_count": 123,
    "top_reverse_nested": {
        "doc_count": 108 <---- HERE 
    }
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 5, 2016, 9:20am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/5 "2016-08-05T09:20:57Z")

</div>

When you use the nested agg you are accounting for numbers of nested docs based on properties of those nested docs.  
If you want your accounting to be on root-level docs then you can use the `copy_to` expression in your mapping to copy nested terms to a root-level one and then do your aggs on that

---

<div class="post-metadata">

**Author:** ![joora](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@joora](https://discuss.elastic.co/u/joora)\
**Post date:** [August 5, 2016, 9:29am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/6 "2016-08-05T09:29:56Z")

</div>

Ok so there are 2 solutions:

- using the reverse\_nested agg in the query (may have performance issue with large data?)
- using "copy\_to" in the indexation part and aggregate directly on the root level (may introduce some limitations for complexe aggregation queries?).

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 5, 2016, 9:35am UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/7 "2016-08-05T09:35:28Z")

</div>

Good point re reverse\_nested!

---

<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 5, 2017, 10:29pm UTC](https://discuss.elastic.co/t/wrong-doc-count-on-aggregations/57315/8 "2017-07-05T22:29:51Z")

</div>


