Sorting nested documents with inner_hits not returning desired results. ES7.x

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!

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:

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