Maybe late, I use nested sorting to restrict an element to my nested relation, here is an example:
"sort": { "ouverture.periodesOuvertures.dateDebut": { "order": "asc", "mode": "min", "nested_filter": { "range": { "ouverture.periodesOuvertures.dateFin": { "gte": "2017-08-29", "format": "yyyy-MM-dd" } } }, "nested_path": "ouverture.periodesOuvertures" } },
Since 5.5 ES (I think), you can use a filter for a subquery. Here is an example of the nested query filter used:
{ "nested": { "path": "ouverture.periodesOuvertures", "query": { "bool": { "must": [ { "range": { "ouverture.periodesOuvertures.dateFin": { "gte": "2017-08-29", "format": "yyyy-MM-dd" } } }, { "range": { "ouverture.periodesOuvertures.dateFin": { "lte": "2017-09-30", "format": "yyyy-MM-dd" } } } ], "filter": [ { "range": { "ouverture.periodesOuvertures.dateFin": { "gte": "2017-08-29", "format": "yyyy-MM-dd" } } }, { "range": { "ouverture.periodesOuvertures.dateFin": { "lte": "2017-09-30", "format": "yyyy-MM-dd" } } } ] } } } }
Hope this helps;)
Plus, if you are not in the latest version (5.5), inner_hits can slow down your query. Enabling inner hits drastically slows down query results
source share