# Elastic Pagination

**URL:** <https://discuss.elastic.co/t/elastic-pagination/154976>\
**Category:** Elasticsearch\
**Created:** [November 1, 2018, 9:07am UTC](https://discuss.elastic.co/t/elastic-pagination/154976 "2018-11-01T09:07:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaralErdene\_Tumursuh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maralerdene_tumursuh/32/73137_2.png) [@MaralErdene\_Tumursuh](https://discuss.elastic.co/u/MaralErdene_Tumursuh)\
**Post date:** [November 1, 2018, 9:07am UTC](https://discuss.elastic.co/t/elastic-pagination/154976/1 "2018-11-01T09:07:14Z")

</div>

Hello Guys,

I'm using elastic search with java. My elastic query is returning 103 results, which means that if I'm paging through them (with 25 results per page) I should have 4 pages: 4 full pages and 1 page with a 3 row result.  
(using setFrom and setSize).

However, my last page is still returning 25 results. It's returning the very last result as expected, but also 22 results from the previous page. How do I disable this behavior and have it just return the single result as expected?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 1, 2018, 9:10am UTC](https://discuss.elastic.co/t/elastic-pagination/154976/2 "2018-11-01T09:10:23Z")

</div>

Could you share the code you use to page through the results?

---

<div class="post-metadata">

**Author:** ![MaralErdene\_Tumursuh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maralerdene_tumursuh/32/73137_2.png) [@MaralErdene\_Tumursuh](https://discuss.elastic.co/u/MaralErdene_Tumursuh)\
**Post date:** [November 1, 2018, 9:11am UTC](https://discuss.elastic.co/t/elastic-pagination/154976/3 "2018-11-01T09:11:49Z")

</div>

here is my code,

```
 searchReq = ElasticConnection.getClient().prepareSearch(conn.getCoreName());
        searchReq.setTypes(conn.getType());
        
        
        searchReq.setQuery(search(order))
                 
                 .setFrom(pageNumber)
                 .setSize(25);

  SearchResponse searchResponse = searchReq.execute().actionGet();
```

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 1, 2018, 9:28am UTC](https://discuss.elastic.co/t/elastic-pagination/154976/4 "2018-11-01T09:28:17Z")

</div>

That should get you the top 25, but how do you implement the paging?

---

<div class="post-metadata">

**Author:** ![MaralErdene\_Tumursuh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maralerdene_tumursuh/32/73137_2.png) [@MaralErdene\_Tumursuh](https://discuss.elastic.co/u/MaralErdene_Tumursuh)\
**Post date:** [November 1, 2018, 9:35am UTC](https://discuss.elastic.co/t/elastic-pagination/154976/5 "2018-11-01T09:35:19Z")

</div>

Thank you Sir,

I found my error. I implemented paging wrong. So i fixed it.

Here is my code

```
          searchReq.setQuery(search(order))
            .setFrom(pageNumber*pageCount)
            .setSize(pageCount);
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 29, 2018, 9:35am UTC](https://discuss.elastic.co/t/elastic-pagination/154976/6 "2018-11-29T09:35:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
