Elasticsearch results per page

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.

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

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) or the scroll API (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html).

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 { } ?>

Set from to 20

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

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

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.

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.

1 Like

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