Pagination Elastic Search


(Yash Kapoor) #1

How do we use pagination in elastic search ?

I have been using the size field with from field with size field set to 1000 and from field to 0.
{ "from : 0, "sort": .... , "_source": ... , "size": 1000 }

Even after this query i am getting more than 1000 hits. How can we solve this?


(john rabindranath) #2

hi @Yash_Kapoor,
you can use like this , how many total hits in your Es ?
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"should":
}
},
"from": 0,
"size": 1000
}


(Yash Kapoor) #3

Hello @johnrabi

I tried the given query but the hits are still more than 1000. (3621 hits)

Any solution to this problem?


(David Pilato) #4

From 1000, size 1000 to get the next 1000.


(Yash Kapoor) #5

Hello @dadoonet

I want the first 1000 results only therefore the from field in set to 0.
But still the number of hits are more. How to improve this?


(David Pilato) #6

That's not about pagination then.
You want to export all results?

In that case, if the number of hits is reasonable ( < 10000 ), you can set size to 10000.
Otherwise you need to use the scroll API.


(Pranav) #7

Hi @Yash_Kapoor
If you are talking about the value of "total" under hits, It is the count of document matched your search string. But the number of documents retrieved will be 1000 only.

Try testing it with lower size such as 2 or 3. This would retrieve only 2 or 3 but the value of "total" under hits would be the same.


(Yash Kapoor) #8

hello Pranav initially i was cionfusing total results with total hits. The query is working fine. Thankyou