user6376936
user6376936

Reputation:

How to add pagination for elastic search

I am using elastic search as searching tool in my application...

I am having 9 records in my db.

My page size is 5.

So first time it will give me 5 records which is working fine by my code...

Now second time when I scroll it should give me 4 records and then scrolling should give me 0 records.

{

   "from" : 0, "size" : size,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

I am using above code for query.

using above code the data is not coming proper as first time it is giving me 5 records, 2nd time also it is giving me 5 records then it will reduce count as 4,3,2.

Kindly help me to allow pagination in my code.

Upvotes: 2

Views: 2197

Answers (2)

Kaushik
Kaushik

Reputation: 2090

You'll have to pass

1st time
"from" : 0,
"size" : 5


2nd time 
"from" : 5,
"size" : 5

Your query will be looking like

{
  "query": {
    ... Your elasticsearch query
  },
  "_source": [
    .... Columns To retrieve
  ],
  "sort": [
    .... Sorting information
  ],
  "from": 0,
  "size": 5
}

This is for elasticsearch 2.3

Upvotes: 0

Conffusion
Conffusion

Reputation: 4465

What do you specify the second time as from value? Can you post what you request in the second query call.

tip: It should be from: 5 and not from: 1. It is not a page counter but a record counter.

Upvotes: 3

Related Questions