Changing the Size of Result Sets

This entry is part 32 of 35 in the series Complete Guide to Elasticsearch

In this article I will show you how to change the number of documents returned in the result set.

I will start by showing you how to do it with query string searches. It’s as simple as adding a size parameter as I will show you in the following query.

GET /ecommerce/product/_search?q=name:pasta&size=2

Notice that I added a size parameter of two, which limits the number of returned documents to two. The default size of result sets is 10. If we take a look at the results, notice that the total property within the hits object shows 11 matches, even though I limited the results to two. This is convenient, because it allows one to display the number of hits while only showing the most relevant, for example.

Next, I will show you how to accomplish the same thing with the query DSL.

GET /ecommerce/product/_search
{
  "query": {
    "match": {
      "name": "pasta"
    }
  },
  "size": 2
}

With the query DSL, a size property is simply added in the request object. Taking a look at the results will reveal that they are identical to those of the query string search.

That’s all it takes to limit the number of documents that are returned. In the next article, I will show you how this is used when paginating through search results. So until then, happy searching!

Series Navigation<< Filtering ResultsPagination >>

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *