Changing the Size of Result Sets

Published on November 12, 2016 by

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!

Filtering Results
Pagination
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.