# Can you filter on top ranked inner nested hit in same query

**URL:** <https://discuss.elastic.co/t/can-you-filter-on-top-ranked-inner-nested-hit-in-same-query/314085>\
**Category:** Elasticsearch\
**Created:** [September 9, 2022, 7:35pm UTC](https://discuss.elastic.co/t/can-you-filter-on-top-ranked-inner-nested-hit-in-same-query/314085 "2022-09-09T19:35:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimauwah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chimauwah/32/110694_2.png) [@chimauwah](https://discuss.elastic.co/u/chimauwah)\
**Post date:** [September 9, 2022, 7:35pm UTC](https://discuss.elastic.co/t/can-you-filter-on-top-ranked-inner-nested-hit-in-same-query/314085/1 "2022-09-09T19:35:13Z")

</div>

Cheers all!

Let's say I have a mapping with a nested object like this:

```auto
{
  "APP_APPLICATION_ID": {
    "type": "long"
  },
  "APP_FNAME": {
    "type": "text",
    "fields": {
      "raw": {
        "type": "keyword"
      }
    }
  },
  "PROGRAM_ASSIGNMENT_DATA": {
    "type": "nested",
    "properties": {
      "PA_SEQ_NBR": {
        "type": "long"
      },
      "PA_DOC_ID": {
        "type": "long"
      },
      "PA_DOC_RELEASE_DT": {
        "type": "date"
      }
    }
  }
}

```

What I want to do is first sort the inner hits of every top-level document, then apply a filter to ONLY the top ranked (or index 0-however you want to look at it) inner hit document.

For example, what does the query need to look like to return all top-level documents whose most "recent" document, has a PA\_DOC\_ID = 19, where most recent is defined by sorting PA\_DOC\_RELASE\_DT desc then PA\_SEQ\_NBR asc?

A sample document might look like this:

```auto
{
  "APP_APPLICATION_ID": 100,
  "APP_FNAME": "Clark",
  "PROGRAM_ASSIGNMENT_DATA": [
    {
      "PA_SEQ_NBR": 1,
      "PA_DOC_ID": 19,
      "PA_DOC_RELEASE_DT" : "2022-04-30T00:00:00Z",
    },
    {
      "PA_SEQ_NBR": 2,
      "PA_DOC_ID": 19,
      "PA_DOC_RELEASE_DT" : "2022-04-30T00:00:00Z",
    },
    {
      "PA_SEQ_NBR": 3,
      "PA_DOC_ID": 19,
      "PA_DOC_RELEASE_DT" : "2022-04-30T00:00:00Z",
    },
    {
      "PA_SEQ_NBR": 4,
      "PA_DOC_ID": 9,
      "PA_DOC_RELEASE_DT" : "2022-05-22T00:00:00Z",
    },
    {
      "PA_SEQ_NBR": 5,
      "PA_DOC_ID": 13,
      "PA_DOC_RELEASE_DT" : "2022-05-22T00:00:00Z",
    }
  ]
}

```

It's highest ranked inner hit would be PA\_SEQ\_NBR:4. Then applying the filter, this inner hit document does NOT match PA\_DOC\_ID=19, so this document would NOT return.

I would like to do this all in 1 query.

Is it possible to do this all in the same query? If not, which parts can and cannot be done?

Any guidance would be much appreciated. Thanks!

---

<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:** [October 7, 2022, 7:35pm UTC](https://discuss.elastic.co/t/can-you-filter-on-top-ranked-inner-nested-hit-in-same-query/314085/2 "2022-10-07T19:35:15Z")

</div>

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