# Loop over inner\_hits within post\_filter via script

**URL:** <https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044>\
**Category:** Elasticsearch\
**Created:** [August 26, 2022, 7:13pm UTC](https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044 "2022-08-26T19:13:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sumit\_Jha1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sumit_jha1/32/100878_2.png) [@Sumit\_Jha1](https://discuss.elastic.co/u/Sumit_Jha1)\
**Post date:** [August 26, 2022, 7:13pm UTC](https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044/1 "2022-08-26T19:13:25Z")

</div>

This is how the response looks like:

```auto
{
  "hits" : {
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "7201e7f0-882f-4184-bdda-71302b8c28d3",
        "_source" : {...},
        "inner_hits" : {
          "inner_hits_1" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "hits" : [
                {
                  "_index" : "myindex",
                  "_type" : "_doc",
                  "_id" : "7201e7f0-882f-4184-bdda-71302b8c28d3",
                  "_nested" : {
                    "field" : "transcript",
                    "offset" : 1
                  },
                  "_source" : {
                    "phrase" : "good afternoon"
                    "conv_id" : 1
                  },
                  "highlight" : {
                    "transcript.phrase" : [
                      "<HIGHLIGHT>good<HIGHLIGHT/> afternoon"
                    ]
                  }
                }
              ]
            }
          },
          "inner_hits_2" : {...},
          "inner_hits_3" : {...}
        }
      }
    ]
  }
}

```

How do I loop over all the `inner_hits` within `post_filter` via script ?  
Something like this:

```auto
GET myindex/_search
{
  "query": {...},
  "post_filter": {
    "script": {
      "script": {
        "lang": "painless",
        "source": """

          // loop over inner_hits here and filter documents
        
        """
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Sumit\_Jha1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sumit_jha1/32/100878_2.png) [@Sumit\_Jha1](https://discuss.elastic.co/u/Sumit_Jha1)\
**Post date:** [August 27, 2022, 1:05pm UTC](https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044/2 "2022-08-27T13:05:25Z")

</div>

@dadoonet @Christian_Dahlqvist

Kindly suggest if this is possible.

Regards,

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 27, 2022, 1:14pm UTC](https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044/3 "2022-08-27T13:14:54Z")

</div>

Please note that this forum is manned exclusively by volunteers, out of which some may work for Elastic. There are therefore no SLAs offered and not even any guarantee that you will receive a response (although this is not very common). As it is a community forum it is by many considered rude to ping people not already involved in the thread.

Most people, including myself, tend to respond to topics where we have knowledge or experience and let others respond to topics that match their profile and interests. What makes you believe I have any input on this topic? Have you ever seen me respond to topics around inner hits and advanced script scoring/filtering?

---

<div class="post-metadata">

**Author:** ![Sumit\_Jha1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sumit_jha1/32/100878_2.png) [@Sumit\_Jha1](https://discuss.elastic.co/u/Sumit_Jha1)\
**Post date:** [August 27, 2022, 1:27pm UTC](https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044/4 "2022-08-27T13:27:38Z")

</div>

Apologies for that Chris. Will be mindful of this going forward. 🙂

---

<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:** [September 24, 2022, 1:28pm UTC](https://discuss.elastic.co/t/loop-over-inner-hits-within-post-filter-via-script/313044/5 "2022-09-24T13:28:29Z")

</div>

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