# Applying post\_filter to limit inner\_hits in nested document

**URL:** <https://discuss.elastic.co/t/applying-post-filter-to-limit-inner-hits-in-nested-document/207925>\
**Category:** Elasticsearch\
**Created:** [November 14, 2019, 3:46pm UTC](https://discuss.elastic.co/t/applying-post-filter-to-limit-inner-hits-in-nested-document/207925 "2019-11-14T15:46:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emil/32/49885_2.png) [@Emil](https://discuss.elastic.co/u/Emil)\
**Post date:** [November 14, 2019, 3:46pm UTC](https://discuss.elastic.co/t/applying-post-filter-to-limit-inner-hits-in-nested-document/207925/1 "2019-11-14T15:46:29Z")

</div>

Hi, I'm struggling how to apply a post\_filter to some nested documents.

What I'd like to do is have some aggregation on all my nested documents, but have only certain nested documents returned (the general idea of a post\_filter). I'm succesfull in applying the post\_filter to filter out the root (parent)-documents, but not in filtering the inner hits on this document.

As an example, I've created two documents that each contain 2 people (with a name and age), and what I'd like to have returned is a) a histogram of their ages b) the people who are under 25.

Like this code does: [https://gist.github.com/EmilBode/550d3ad44220d1ce57a20dda428b15b5](https://gist.github.com/EmilBode/550d3ad44220d1ce57a20dda428b15b5)

The code as it is now succesfully filters the right document, but not only gives me Dave (age 20) in the inner\_hits, but also Carlos (age 50), I think because he is in the document with Dave.

What I would like to see is the same as I would get with the range-query included in the nested query, but with the aggregation over all the documents.

Is this possible, or should I just work around it by using 2 seperate queries?

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [November 16, 2019, 10:53pm UTC](https://discuss.elastic.co/t/applying-post-filter-to-limit-inner-hits-in-nested-document/207925/2 "2019-11-16T22:53:28Z")

</div>

Hello,

This was a bit of a headscratcher. I started to wonder if this was possible, but then because of [this issue](https://github.com/elastic/elasticsearch/issues/26649) with a related [pull request](https://github.com/elastic/elasticsearch/pull/27118), I knew that it must be.

Then I stopped fixating on the path, and the various parameters available in the `inner_hits` object of the query, and it became clear.

Just put an `inner_hits` inside your `post_filter`.

```auto
  "post_filter": {
    "nested": {
      "path": "people",
      "inner_hits": {},
      "query": {
        "range": {
          "people.age": {
            "lte": 25
          }
        }
      }
    }
  }

```

and in your response you'll have

```auto
        "inner_hits" : {
          "people" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "temp",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_nested" : {
                    "field" : "people",
                    "offset" : 1
                  },
                  "_score" : 1.0,
                  "_source" : {
                    "name" : "Dave",
                    "age" : 20
                  }
                }
              ]
            }
          }
        }
      }

```

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emil/32/49885_2.png) [@Emil](https://discuss.elastic.co/u/Emil)\
**Post date:** [November 18, 2019, 1:09pm UTC](https://discuss.elastic.co/t/applying-post-filter-to-limit-inner-hits-in-nested-document/207925/3 "2019-11-18T13:09:46Z")

</div>

Thanks, this was exactly what I was looking for!  
As you wrote it down it seems perfectly logical to do it this way, yet I was scratching my head as well.

---

<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:** [December 16, 2019, 1:09pm UTC](https://discuss.elastic.co/t/applying-post-filter-to-limit-inner-hits-in-nested-document/207925/4 "2019-12-16T13:09:47Z")

</div>

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