# Elasticsearch results per page

**URL:** <https://discuss.elastic.co/t/elasticsearch-results-per-page/98155>\
**Category:** Elasticsearch\
**Created:** [August 24, 2017, 5:19am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155 "2017-08-24T05:19:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akhil\_Prashar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhil_prashar/32/20313_2.png) [@Akhil\_Prashar](https://discuss.elastic.co/u/Akhil_Prashar)\
**Post date:** [August 24, 2017, 5:19am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/1 "2017-08-24T05:19:47Z")

</div>

Hi guys, I want to display 20 results in a single page and then I want a next page option for next 20 results and so on. I shall be really thankful to you if you can help me with this.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 24, 2017, 9:00am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/2 "2017-08-24T09:00:24Z")

</div>

You can add `from` and `size` parameters to your search request which will determine the hits that will be returned. To get hits 0-20 set `from` equal to `0` and `size` to `20`. To get the next page of results, set `from` to 20, and keep `size` at 20.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html)

Be careful of deep pagination and don't let your users go past the first 100 or so pages of search results. If you want to do that, take a look at `search_after` ([https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-search-after.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-search-after.html)) or the `scroll` API ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html)).

---

<div class="post-metadata">

**Author:** ![Akhil\_Prashar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhil_prashar/32/20313_2.png) [@Akhil\_Prashar](https://discuss.elastic.co/u/Akhil_Prashar)\
**Post date:** [August 24, 2017, 9:48am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/3 "2017-08-24T09:48:11Z")

</div>

Hi Abdon, I am getting first 20 results. How should I jump to next page?

My code is below:

\<?php use Elasticsearch\ClientBuilder; require('/home/akhil/Documents/june/elastic/vendor/autoload.php'); $hosts = ["http://localhost:9200"]; $clientBuilder = ClientBuilder::create(); // Instantiate a new ClientBuilder $clientBuilder-\>setHosts($hosts); // Set the hosts $client = $clientBuilder-\>build(); // Build the client object $params = array(); if(isset($\_GET['search'])) { //echo "

search is set

"; if(isset($\_GET['q'])) { $keyword=$\_GET['q']; if(isset($\_GET['from'])) { $from = $\_GET['from']; } if(isset($\_GET['to'])) { $to = $\_GET['to']; } $from\_year= date('Y', strtotime($from)); $from\_month= date('m', strtotime($from)); $from\_date = date('Y-m-d', strtotime($from)); $to\_year = date('Y', strtotime($to)); $to\_month= date('m', strtotime($to)); $to\_date = date('Y-m-d', strtotime($to)); $indexes = array(); if($from\_year == $to\_year) { for($i=(int)$from\_month; $i\<=$to\_month;$i++) { if($i\<=9) { array\_push($indexes,$from\_year.'0'.$i); }else{ array\_push($indexes,$from\_year.$i); } } }else{ for($j=$from\_year;$j\<=$to\_year;$j++) { if($j==$to\_year) { for($i=1; $i\<=$to\_month;$i++) { if($i\<=9) { array\_push($indexes,$j.'0'.$i); }else{ array\_push($indexes,$j.$i); } } }elseif($j==$from\_year){ for($i=(int)$from\_month; $i\<=12; $i++) { if($i\<=9) { array\_push($indexes,$j.'0'.$i); }else{ array\_push($indexes,$j.$i); } } }else{ for($i=1; $i\<=12;$i++) { if($i\<=9) { array\_push($indexes,$j.'0'.$i); }else{ array\_push($indexes,$j.$i); } } } } } $search\_indexes=implode(",",$indexes); $params['index'] = $search\_indexes; if($\_GET['datasource'] != 'all') { $datasource = $\_GET['datasource']; $params['type'] = $datasource; if(isset($\_GET['datasourcefields'])) { $datasourcefields = $\_GET['datasourcefields']; $params['body']['query']['bool']['should']['match'][$datasourcefields] = $keyword; }else{ #$params['body']['query']['match']['\_all'] = $keyword; $params['body']['query']['bool']['should']['match']['\_all'] = $keyword; } }else{ $params['body']['query']['bool']['should']['match']['\_all'] = $keyword; } $from\_time=$from." 00:00:00"; $to\_time =$to." 23:59:59"; $params['body']['query']['bool']['must']['range']['created\_at']=array('gte'=\>$from\_time,'lte'=\>$to\_time); $params['from'] = 0; $params['size'] = 20; $params['body']['sort'] = ['\_score']; $results = $client-\>search($params); if(isset($results['hits'])) { $hits = $results['hits']; $rows = $hits['hits']; $result\_page=""; foreach($rows as $row) { if($row['\_type']=='facebook') { $str="
"; $str.= "

### Message:".$row['\_source']['message']."

User ID:".$row['\_source']['fb\_user\_id']."

"; $str.= "
Date:".$row['\_source']['created\_at']." Media:facebook 

"; $str.= "

##### Sentiment Score:".$row['\_source']['sentiment\_score']."

Sentiment Category:".$row['\_source']['sentiment\_category']."

"; $result\_page .=$str; } if($row['\_type']=='youtube') { $str="
"; $str.= "

### Title:".$row['\_source']['title']."

Description:".$row['\_source']['description']."

"; $str.= "
Date:".$row['\_source']['created\_at']." Media:youtube 

"; $result\_page .=$str; } if($row['\_type']=='twitter') { $str="
"; $str.= "

### Tweet:".$row['\_source']['message']."

Source Name:".$row['\_source']['name']."

Screen Name:".$row['\_source']['screen\_name']."

City:".$row['\_source']['city']."

State:".$row['\_source']['state']."

"; $str.= "
Date:".$row['\_source']['created\_at']." Media:twitter 

"; $str.= "

##### Sentiment Score:".$row['\_source']['sentiment\_score']."

Sentiment Category:".$row['\_source']['sentiment\_category']."

"; $result\_page .=$str; } //echo $str = $row-\>\_source-\>title; } print $result\_page; } } }else { } ?\>

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 24, 2017, 9:52am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/4 "2017-08-24T09:52:48Z")

</div>

Set `from` to `20`

---

<div class="post-metadata">

**Author:** ![Akhil\_Prashar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhil_prashar/32/20313_2.png) [@Akhil\_Prashar](https://discuss.elastic.co/u/Akhil_Prashar)\
**Post date:** [August 24, 2017, 9:54am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/5 "2017-08-24T09:54:46Z")

</div>

Where should I write that? just below that?  
like this :  
$params['from'] = 0;   
$params['size'] = 20;  
$params['from'] = 20;  
$params['size'] = 20;

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 24, 2017, 9:55am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/6 "2017-08-24T09:55:42Z")

</div>

I'm not familiar with your code, but that looks OK.

---

<div class="post-metadata">

**Author:** ![Akhil\_Prashar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhil_prashar/32/20313_2.png) [@Akhil\_Prashar](https://discuss.elastic.co/u/Akhil_Prashar)\
**Post date:** [August 24, 2017, 9:55am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/7 "2017-08-24T09:55:54Z")

</div>

This doesn't seems to work, there is no option of navigating to another page. I am new to elasticsearch I am sorry if am doing any silly mistakes.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 24, 2017, 9:58am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/8 "2017-08-24T09:58:14Z")

</div>

This more of a challenge with your PHP code than with Elasticsearch. You need to create a link "Next page" that cause the next 20 hits to be returned from Elasticsearch. To get those next hits from Elasticsearch, just keep incrementing `from` by 20.

---

<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:** [September 21, 2017, 9:58am UTC](https://discuss.elastic.co/t/elasticsearch-results-per-page/98155/9 "2017-09-21T09:58:32Z")

</div>

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