63

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.

5 Answers 5

56

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

GET /_search
{
    "from" : 0, "size" : 1000,
    "query" : {
        //your query
    }
}
1
  • Please check the EDIT in the question.
    – RohitWagh
    Commented Jan 23, 2017 at 11:11
26

You can just specify the size as an parameter.

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

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!

3
  • 2
    Importantly, all your queries will stop counting results after it they hit max_result_window (even if you're only returning the top 10 results), therefore lowering can be an effective way to reduce response times, particularly for queries with a lot of matches.
    – ShibbyUK
    Commented Jul 31, 2020 at 11:52
  • 1
    I dont think you're 100% right, max_result_window is not for that purpose. Per his question, he doesn't want to do internal pagination which can be done using from and size based query. But, what s/he want is, to have external pagination, which can be done using scroll based queries, with a page size of 100/1000 requests. even then it will be having limitation of 10k, if total result spans across that; so if we want more data, we might want to alter that setting, to increase the limit value. In short, that setting max_result_window should not be used for pagination purpose. Commented Jul 20, 2022 at 11:53
  • @HariKishore I didn't say they should use max_results_window for pagination. They want to limit the results independently from any pagination approaches. That's what this is for.
    – bkis
    Commented Oct 24, 2023 at 6:48
0

Custom Routing - Best Performance Reference : https://bigdataboutique.com/blog/multi-tenancy-with-elasticsearch-and-opensearch-c1047b#:~:text=Multi%2Dtenancy%20in%20this%20sense,costs%20lower%20by%20sharing%20resources.

POST /shared_index_for_all_tenant/_doc/10001?routing=tenant_1
{
  "tenant_id": "tenant_1",  <--- This will help in filtering and aggergration 
  "fields": "value1"
}
-3

did you try with

terminate_after

The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early. If set, the response will have a boolean field terminated_early to indicate whether the query execution has actually terminated_early. Defaults to no terminate_after.

2
  • 2
    yes, as i have mentioned in the question, i have already tried terminate_after.
    – RohitWagh
    Commented Feb 12, 2018 at 10:57
  • @RohitWagh i also tryed this but its messed up the sorting. My solution was to sort the result base on creation date and limit last 60 days.
    – kozla13
    Commented Feb 23, 2018 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.