Limit the number of results returned by Elastic Search

68,404

Solution 1

How about using From/Size in order to return the requirement number of results:

GET /_search
{
    "from" : 0, "size" : 1000,
    "query" : {
        //your query
    }
}

Solution 2

You can just specify the size as an parameter.

GET /_search?size=1000
{
    "query" : {
        //your query
    }
}

Solution 3

I know this question aged a little since it was asked, but i stumbled over this and i am surprised no one could give the correct answer.

Elasticsearch indices have an index module called max_result_window. You can find it in the documentation under dynamic index settings.

index.max_result_window
The maximum value of from + size for searches to this index. Defaults to 10000. Search requests take heap memory and time proportional to from + size and this limits that memory. See Scroll or Search After for a more efficient alternative to raising this.

So basically instead of limiting from or size (or a combination of those), you set max_result_window to 1000 and ES will only return a maximum of 1000 hits per request.

If you are using an index definition in a separate JSON file to create your index, you can set this value there under yourindexname.settings.index.max_result_window.

I hope this helps the folks still looking for a solution to this problem!

Share:
68,404
RohitWagh
Author by

RohitWagh

Updated on July 09, 2022

Comments

  • RohitWagh
    RohitWagh almost 2 years

    I am having an issue where i want to reduce the number of results from Elastic search to 1,000 no matter how many matching results are there matching, but this should not affect the ranking and scoring.

    I was trying terminate_after, but that seems to just tell the elastic search to just get the top N results without considering the scores. Correct me if am wrong.

    Any help on this?

    EDIT:

    I am already using pagination. So, using Size in From/Size will only affect the size of current page. But i want to limit the size of total results to 1,000 and then pagination on that.