Return matched nested object instead of root parent document


(Shekhar Chauhan) #1

I want nested query to return only matched nested object for nested field instead of root parent document, I s it possible ?


(Martijn Van Groningen) #2

This can be achieved via aggregations. You can use the nested with the
top_hits aggregation to only return the matched nested inner objects.
Something like this:

{
"size": 0,
"aggs": {
"my-query": {
"filter": {
"query": {
...
}
},
"aggs": {
"to-nested": {
"nested": {
"path": "level1"
},
"aggs": {
"nested-docs": {
"top_hits": {
"size": 10
}
}
}
}
}
}
}
}


#3

You need to add filter in your aggregation which will be equal to your main query. Here is the post with similar use case and solution to it.

Let me know if this works for you.


(system) #4