elasticsearchelasticsearch-queryelasticsearch-nested

ElasticSearch - Limit size of nested collection on Query Result


If I have blog-post with thousands (or hundred-thousands) of nested comments and I want to retrieve just the top 10 blog-posts. I will just use size to control how many blog-posts I want to retrieve, but I am not sure how to limit the size of how many nested comments I want.

e.g. This will return top 10 blog-posts with unlimited comments

GET myblog/_search
{
   "size": 10,
   "query": {
      "match_all": {}
   }
}

I try inner_hits but it doesn't work for me. When I used, I have to do a query in the nested-comments, I also disabled the source (to avoid retrieving post with all comments), and the inner_hits result will give me each comment with each post (redundant) even though in some cases it is the same parent-post. I also thought about parent-child approach, but this mean creates multiple request/queries.

Do you know how to limit the size of a nested collection in a query?

What I am looking for is to create a query that I can do something like get top 10 blog-posts with top 5 comments.


Solution

  • Can you try this query:

    {
        "_source": false,
        "fields":["your_fields"],
       "size": 10,
       "query": {
          "match_all": {}
       },
       "inner_hits" : {
            "comments" : {
                "path" : { 
                    "comments" : { 
                        "size":5,
                        "query" : {
                            "match_all": {}
                        }
                    }
                }
            }
        }
    }