# How to use reverse\_nested in this aggs query?

**URL:** https://discuss.elastic.co/t/how-to-use-reverse-nested-in-this-aggs-query/64796
**Category:** Elasticsearch
**Created:** [November 3, 2016, 4:03am UTC](https://discuss.elastic.co/t/how-to-use-reverse-nested-in-this-aggs-query/64796 "2016-11-03T04:03:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![PAK90](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@PAK90](https://discuss.elastic.co/u/PAK90)
#### Post date: [November 3, 2016, 4:03am UTC](https://discuss.elastic.co/t/how-to-use-reverse-nested-in-this-aggs-query/64796/1 "2016-11-03T04:03:48Z")

</div>

I've successfully implemented nesting and inner\_hits, which is great. However, I'm using [Searchkit](https://github.com/searchkit/searchkit) and the filters have built-in aggregators which are now showing aggregations over inner\_hits, not their parent docs. From googling, I think `reverse_nested` should fix this; I just can't figure out where to place it. This is what I've attempted:

```
"editions.rarity.raw163": {
  "filter": {
    ...
  },
  "aggs": {
    "inner": {
      "nested": {
        "path": "editions",
        "reverse_nested": {}
      },
      "aggs": {
        "editions.rarity.raw": {
          "terms": {
            "field": "editions.rarity.raw",
            "size": 6
          }
        },
        "editions.rarity.raw_count": {
          "cardinality": {
            "field": "editions.rarity.raw"
          }
        }
      }
    }
  }
}

```

All of this is generated by Searchkit's code, I can access it before it gets sent to the ES server and modify it, which is how I've managed to insert my test `reverse_nested` code. However right now it gives me a `Bad Request` 400 error, so I am clearly not putting this in the right place. Where should it go? The docs say it has to be in a nested query so that's where I put it.

---

<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: [November 3, 2016, 10:09am UTC](https://discuss.elastic.co/t/how-to-use-reverse-nested-in-this-aggs-query/64796/2 "2016-11-03T10:09:15Z")

</div>

It would look like this:

```auto
"editions.rarity.raw163": {
  "filter": {
    ...
  },
  "aggs": {
    "inner": {
      "nested": {
        "path": "editions"
      },
      "aggs": {
        "editions.rarity.raw": {
          "terms": {
            "field": "editions.rarity.raw",
            "size": 6
          },
          "aggs": {
            "outer": {
              "reverse_nested": {}
            }
          }
        },
        "editions.rarity.raw_count": {
          "cardinality": {
            "field": "editions.rarity.raw"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![PAK90](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@PAK90](https://discuss.elastic.co/u/PAK90)
#### Post date: [November 4, 2016, 3:32am UTC](https://discuss.elastic.co/t/how-to-use-reverse-nested-in-this-aggs-query/64796/3 "2016-11-04T03:32:08Z")

</div>

edit: It did actually work! However, it's returning the parent doc count like this:

```
"editions.rarity.raw13": {
  "doc_count": 3,
  "inner": {
    "doc_count": 19,
    "editions.rarity.raw": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Rare",
          "doc_count": 14,
          "outer": {
            "doc_count": 2
          }
        },
        {
          "key": "Common",
          "doc_count": 5,
          "outer": {
            "doc_count": 1
          }
        }
      ]
    },
    "editions.rarity.raw_count": {
      "value": 2
    }
  }
}

```

Unfortunately the UI is still using the `doc_count` property directly under the `bucket`, rather than the `outer` one (and there's not a lot I can do about that, given it's deep in the UI code somewhere). Is there a way to overwrite the top-level `doc_count` with the `doc_count` currently trapped in the `outer` object?

---

<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:06pm UTC](https://discuss.elastic.co/t/how-to-use-reverse-nested-in-this-aggs-query/64796/4 "2017-07-05T22:06:41Z")

</div>


