I want nested query to return only matched nested object for nested field instead of root parent document, I s it possible ?
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
}
}
}
}
}
}
}
}
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.