Unfortunately not (at least not yet).
A query in ElasticSearch simply determines which documents match the query and how much they match.
To understand which attached documents are useful, consider the following example:
{ "title": "My post", "body": "Text in my body...", "followers": [ { "name": "Joe", "status": "active" }, { "name": "Mary", "status": "pending" }, ] }
The above JSON, once indexed in ES, is functionally equivalent to the following. Notice how the followers field was flattened:
{ "title": "My post", "body": "Text in my body...", "followers.name": ["Joe","Mary"], "followers.status": ["active","pending"] }
Search: followers with status == active and name == Mary will match this document ... incorrectly.
Nested fields allow us to circumvent this limitation. If it is declared that the followers field is of type nested instead of type object , then its contents are created as a separate (invisible) subdocument inside. This means that we can use a sub-query or a sub-filter to query these sub-documents as separate documents.
However, the conclusion from the sentences of nested queries / filters only says whether the main document matches and how well it matches. He does not even tell us which of the attached documents match. To understand this, we need to write code in our application to check each of the attached documents according to our search criteria.
There are several open issues that require the addition of these features, but solving this problem is not easy.
The only way to achieve what you want is to index your documents as separate documents, and query and sort them independently. It may be useful to establish a relationship between parent and child between the main document and these separate subdocs. (see parent type mapping , section Parent and child index api docs and top-children and has-child .
In addition, the ES user sent a list from the list of the new has_parent filter on which they are working in the plug . However, this is not yet available in the main ES repositories.