4

I am new with elasticsearch.

I try to make real app. I am using elasticsearch-php
https://github.com/elastic/elasticsearch-php

I don't know to make pagination.

my code :

<?php
require_once 'app/init.php';

if(isset ($_GET['q'])) {

  $q = $_GET['q'];

  $query = $es->search([
    'index' => 'user',
    'type' => 'profile',
    'body' => [
      'query' => [
        'bool' => [
          'should' => [
            'match' => ['bio' => $q]
            ]
        ]
    ]
]
  ]);
  if ($query['hits']['total'] >=1) {
    $results= $query['hits']['hits'];
  }

}

 ?>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Search | ES</title>
  </head>
  <body>
    <form class="" action="index.php" method="get" autocomplete="off">
      <label for="">
          Search
          <input type="text" name="q">
      </label>
      <input type="submit" value="search">
    </form>
    <?php
      if (isset($results)) {
        foreach ($results as $r) {
      ?>
          <div class="result">
            <?php echo $r['_source']['bio']; ?>
          </div>
     <?php
        }
      }
     ?>
  </body>
</html>

and how to make pagination like this :

View image Pagination like github

0

1 Answer 1

6

You need two things here, total number of pages and way to navigate from one page to other. When you query elassticsearch with simple Query->filter->bool

It returns hits in the response JSON which is the total number of results. Divide them with the number of results you want to show on the App, you will get pages.

Use from and size property in your PHP-elasticsearch request object and change keep changing from on navigating between the pages. Example query(considering page 3 and page size 10)

{
    "from": 20,
    "size": 10,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "FIELD": {
                            "value": "VALUE"
                        }
                    }
                }
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.