# Sorting nested documents with inner\_hits not returning desired results. ES7.x

**URL:** <https://discuss.elastic.co/t/sorting-nested-documents-with-inner-hits-not-returning-desired-results-es7-x/232762>\
**Category:** Elasticsearch\
**Created:** [May 15, 2020, 6:35am UTC](https://discuss.elastic.co/t/sorting-nested-documents-with-inner-hits-not-returning-desired-results-es7-x/232762 "2020-05-15T06:35:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hkhan](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@hkhan](https://discuss.elastic.co/u/hkhan)\
**Post date:** [May 15, 2020, 6:35am UTC](https://discuss.elastic.co/t/sorting-nested-documents-with-inner-hits-not-returning-desired-results-es7-x/232762/1 "2020-05-15T06:35:21Z")

</div>

Hi, I am trying to implement a proof of concept for using Elasticsearch in our application. I am, however, stuck at sorting the nested documents with nested field and will greatly appreciate help in finding answer to my solution.

My scenario involves a document(Person) having nested documents (Applications).  
Person has a name field(among others) and Application document has a modify\_date field(among others).

By default, I want to show 5 applications sorted by modify\_date in a descending order. ( **Basically, 5 most recently accessed applications** ).

I am using inner\_hits to sort the nested documents as per the documentation.

For this, I am using the below query,

```
GET /person/_search
{
  "query": {
    "nested": {
      "inner_hits": {
        "sort": [
          {
            "applications.modify_date": {
              "order": "desc"
            }
          }
        ],
        "size": "5"
      },
      "path": "applications",
      "query": {
        "match_all": {}
      }
    }
  }
}

```

But this isn't giving me the desired results. I am getting (default) 10 person documents each with person's inner\_hits including 5 documents sorted by modify\_date. **I am only looking for 5 most recent applications**. **So, all the nested documents should be compared and sorted and only 5 should be returned.**

I am wondering if I can query in such a way,

- Ideally, I get only 5 most recent applications, without the person document. (Maybe set \_source false for this?)
- If I do get person document in the result, 5 person documents each with only 1 application and the result is sorted by application's modify\_date field.

Also, is it possible to query just the nested documents to directly get nested documents without the parent document? Or is this just a feature of parent-child relationship.

I greatly appreciate any help in this! If any further information is needed, I will be happy to provide it. Thanks!

---

<div class="post-metadata">

**Author:** ![hkhan](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@hkhan](https://discuss.elastic.co/u/hkhan)\
**Post date:** [May 15, 2020, 2:28pm UTC](https://discuss.elastic.co/t/sorting-nested-documents-with-inner-hits-not-returning-desired-results-es7-x/232762/2 "2020-05-15T14:28:16Z")

</div>

I ran into an old post which perfectly describes the scenario I am facing. It was answered as not supported by ES as of Oct 2015. I am wondering if it's still not supported? If it isn't, I can't use Nested approach and will have to consider parent/child. I have to eventually filter nested objects and sort them by different nested fields.

This is the old post answered as not supported as of Oct 2015:

> [@Nested objects, hits, inner hits and sorting](https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565):
>
> As an example imagine Mapping OuterObject contains nested NestedObject. Data OuterObject1 contains NestedObject1, NestedObject2 OuterObject2 contains NestedObject3, NestedObject4 Desired Results (using a nested filter) 4 hits OuterObject1 and NestedObject1 OuterObject1 and NestedObject2 OuterObject2 and NestedObject3 OuterObject2 and NestedObject4 If I use inner hits then the results look like this OuterObject1, inner\_hits = NestedObject1, NestedObject2 OuterObject2, inner\_hits = Nes…

---

<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:** [June 12, 2020, 2:28pm UTC](https://discuss.elastic.co/t/sorting-nested-documents-with-inner-hits-not-returning-desired-results-es7-x/232762/3 "2020-06-12T14:28:24Z")

</div>

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