• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ekman / es-pagination / 3d30d0e2-2ab7-43eb-898f-16cee41c973d

pending completion
3d30d0e2-2ab7-43eb-898f-16cee41c973d

push

circleci

GitHub
Test PHP 8.2

19 of 103 relevant lines covered (18.45%)

0.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/CursorFactories/EsSearchAfterCursorFactory.php
1
<?php
2

3
namespace Nekman\EsPagination\CursorFactories;
4

5
use Elasticsearch\Client;
6
use Nekman\EsPagination\EsUtility;
7

8
final class EsSearchAfterCursorFactory extends BaseCursorFactory
9
{
10
    private Client $client;
11
    private int $pageSize;
12
    private array $defaultSort;
13

14
    public function __construct(
15
        Client $client,
16
        int $pageSize = EsUtility::DEFAULT_PAGE_SIZE,
×
17
        array $defaultSort = [["_doc" => "asc"]]
×
18
    ) {
19
        $this->client = $client;
×
20
        $this->pageSize = $pageSize;
×
21
        $this->defaultSort = $defaultSort;
×
22
    }
23

24
    public function responses(array $params = []): iterable
25
    {
26
        if (!isset($params["size"])) {
×
27
            $params["size"] = $this->pageSize;
×
28
        }
29

30
        // If no sorting was provided in the initial params, the last hit will have no sorting as well. Therefore,
31
        // we need to fallback to something that is similar to all entities which is an "_id". Note that ES
32
        // does not recommend using "_id" here, not much to do though.
33
        if (empty($params["body"]["sort"])) {
×
34
            $params = EsUtility::paramsSort($params, $this->defaultSort);
×
35
        }
36

37
        yield $response = $this->client->search($params);
×
38

39
        while (EsUtility::countHits($response) >= $params["size"]) {
×
40
            // The last hit of the response will contain information on how to get the next result set
41
            $lastHit = EsUtility::lastHit($response);
×
42

43
            if (!$lastHit || !isset($lastHit["sort"])) {
×
44
                break;
×
45
            }
46

47
            $params["body"]["search_after"] = $lastHit["sort"];
×
48

49
            yield $response = $this->client->search($params);
×
50
        }
51
    }
52
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc