|
| 1 | +# Using result |
| 2 | + |
| 3 | +As you can set an offset and limit on `FT.SEARCH` and `FT.AGGREGATE`, they are paginated commands. |
| 4 | +The result of those commands is a page, that can contain one or more document. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +So in this library, the result of `\MacFJA\RediSearch\Redis\Command\Search` and `\MacFJA\RediSearch\Redis\Command\Aggregate` is also a paginated object: |
| 9 | +either a `\MacFJA\RediSearch\Redis\Response\PaginatedResponse` (for Search and Aggregate) or a `\MacFJA\RediSearch\Redis\Response\CursorResponse` (for Aggregate) |
| 10 | + |
| 11 | +The 2 classes are representing a page. |
| 12 | + |
| 13 | +Both `\MacFJA\RediSearch\Redis\Response\PaginatedResponse` and `\MacFJA\RediSearch\Redis\Response\CursorResponse` can be read inside a `foreach` loop or with any iterator function. |
| 14 | +(As they implement the [`\Iterator` interface](https://www.php.net/manual/en/class.iterator.php)) |
| 15 | +What you are iterating over, are the pages (not the documents inside the page). |
| 16 | + |
| 17 | +```php |
| 18 | +// If your RediSearch search have 15 results in total, but you set the pagination to 10 per page: |
| 19 | +/** @var PaginatedResponse $results */ |
| 20 | + |
| 21 | +/** @var array<SearchResponseItem> $items */ |
| 22 | +$items = $results->current(); |
| 23 | +// $items is a list of 10 SearchResponseItem |
| 24 | + |
| 25 | +$results->next(); // To be able to class `->next()`, you need to call `->setClient()` first ! |
| 26 | + |
| 27 | +/** @var array<SearchResponseItem> $items */ |
| 28 | +$items = $results->current(); |
| 29 | +// $items is a list of 5 SearchResponseItem |
| 30 | +``` |
| 31 | + |
| 32 | +## Get all items on all pages |
| 33 | + |
| 34 | +```php |
| 35 | +/** @var \MacFJA\RediSearch\Redis\Client $client */ |
| 36 | +/** @var \MacFJA\RediSearch\Redis\Command\Search $search */ |
| 37 | + |
| 38 | +/** @var \MacFJA\RediSearch\Redis\Response\PaginatedResponse $results */ |
| 39 | +$results = $client->execute($search); |
| 40 | +$results->setClient($client); |
| 41 | + |
| 42 | +$items = []; |
| 43 | +foreach ($results as $pageIndex => $pageContent) { |
| 44 | + $items = array_merge($items, $pageContent); |
| 45 | +} |
| 46 | +/** @var \MacFJA\RediSearch\Redis\Response\SearchResponseItem $item */ |
| 47 | +foreach ($items as $item) { |
| 48 | + doSomething($item); |
| 49 | +} |
| 50 | +``` |
| 51 | +Be careful, this will load all items in the memory. |
| 52 | +This will also call Redis multiple time if there are several pages. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +If you need to avoid loading all items in the memory, you can use [`ResponseItemIterator`](https://github.com/MacFJA/php-redisearch-integration/blob/main/src/Iterator/ResponseItemIterator.php) from the sister project [macfja/redisearch-integration](https://github.com/MacFJA/php-redisearch-integration). |
| 57 | +This class it a custom iterator that will load in memory only one page at the time. |
| 58 | + |
| 59 | +```php |
| 60 | +/** @var \MacFJA\RediSearch\Redis\Client $client */ |
| 61 | +/** @var \MacFJA\RediSearch\Redis\Command\Search $search */ |
| 62 | + |
| 63 | +$results = $client->execute($search); |
| 64 | +$allItems = new \MacFJA\RediSearch\Integration\Iterator\ResponseItemIterator($results, $client); |
| 65 | +/** @var \MacFJA\RediSearch\Redis\Response\SearchResponseItem $item */ |
| 66 | +foreach ($allItems as $item) { |
| 67 | + // Only one page exist in the memory |
| 68 | + doSomething($item); |
| 69 | +} |
| 70 | +``` |
0 commit comments