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!