Changing the Size of Result Sets
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!
Here is what you will learn:
- The architecture of Elasticsearch
- Mappings and analyzers
- Many kinds of search queries (simple and advanced alike)
- Aggregations, stemming, auto-completion, pagination, filters, fuzzy searches, etc.
- ... and much more!