Aggregating
The Basics
Section titled “The Basics”Make an index and add a few documents to it:
use Ehann\RediSearch\Index;
$bookIndex = new Index($redis);
$bookIndex->add([ 'title' => 'How to be awesome', 'price' => 9.99]);
$bookIndex->add([ 'title' => 'Aggregating is awesome', 'price' => 19.99]);Now group by title and get the average price:
$results = $bookIndex->makeAggregateBuilder() ->groupBy('title') ->avg('price');Cursor-Based Pagination
Section titled “Cursor-Based Pagination”For large result sets, use withCursor() to retrieve results in batches instead of all at once.
The first call returns an initial batch; subsequent batches are read with cursorRead().
Always call cursorDelete() when done to free the server-side cursor.
$builder = $bookIndex->makeAggregateBuilder();
// Request the first batch of up to 50 results.$result = $builder ->groupBy('author') ->count() ->withCursor(50) ->search();
// $result contains the first batch and a cursor ID.$cursorId = $result->getCursorId();
// Read subsequent batches until the cursor is exhausted (cursorId becomes 0).while ($cursorId !== 0) { $next = $builder->cursorRead($cursorId, 50); $cursorId = $next->getCursorId(); // process $next->getDocuments() ...}
// If you need to abandon iteration early, delete the cursor explicitly.$builder->cursorDelete($cursorId);