# How to limit the number of search results returned by the search() method?

**URL:** <https://forum.opensearch.org/t/how-to-limit-the-number-of-search-results-returned-by-the-search-method/20683>\
**Category:** OpenSearch\
**Tags:** discuss\
**Created:** [August 5, 2024, 4:42pm UTC](https://forum.opensearch.org/t/how-to-limit-the-number-of-search-results-returned-by-the-search-method/20683 "2024-08-05T16:42:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgoldenberg](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@dgoldenberg](https://forum.opensearch.org/u/dgoldenberg)\
**Post date:** [August 5, 2024, 4:42pm UTC](https://forum.opensearch.org/t/how-to-limit-the-number-of-search-results-returned-by-the-search-method/20683/1 "2024-08-05T16:42:30Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
OpenSearch\_2\_13\_R20240520-P4  
opensearch-py: 2.6.0

**Describe the issue** :  
Having trouble understanding how to properly limit the number of search results returned by the search() method.

Should I be using the from\_ and size parameters in the method call (and why is it named from\_ with an underscore)?

Or, should I be setting size alongside the “query” dictionary entry in the query dictionary?

I’ve tried a variety of such settings and I’m still getting the full results set back, up to thousands of matches.

Of note: I’m doing k-nn queries. I pass in the below to the search() method.

knn\_query = {“knn”: {“vector”: {“vector”: query\_vector, “k”: k}}}  
query = {“query”: knn\_query}

I have tried: query = {“size”: k, “query”: knn\_query} among other permutations and I either get an error or a full result set.

For now, I’m applying the limit in my code by short-circuiting after the desired limit. However, it should be more efficient if this were to happen on the OpenSearch side to potentially reduce latency.

I’d appreciate any pointers or recommendations.

**Configuration** :  
Master nodes: 3 x m6g.large.search  
Data nodes: 6 x r6g.4xlarge.search

**Relevant Logs or Screenshots** :  
None for now
