Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search results very complex to browse #31

Closed
pierrexp9 opened this issue Mar 8, 2022 · 1 comment · Fixed by #36
Closed

Search results very complex to browse #31

pierrexp9 opened this issue Mar 8, 2022 · 1 comment · Fixed by #36
Labels
documentation Improvements or additions to documentation

Comments

@pierrexp9
Copy link
Contributor

Hello MacJFA,
(using version 2.1.0)

I wanted to share with you that I find it very hard for a newcomer to simply browse the results of a search, this is the current method I use.

I do not really get why I have to get $results[0] to get to the actual results. (I could also do a $results->current() I suppose, but the problem stays the same).

 /** @var PaginatedResponse $results */
$results = iterator_to_array($results);
$results = $results[0];
/** @var SearchResponseItem $result */
foreach ($results as $result) {
    var_dump($result->getFields());
}

Also, thanks a million for your hard work on this library !

@MacFJA MacFJA added the documentation Improvements or additions to documentation label Mar 12, 2022
@MacFJA
Copy link
Owner

MacFJA commented Mar 12, 2022

The way the PaginatedResponse is built/thought of is to hold the list of individual response.

PaginatedResponse is a page, and a page contains a list of items. And those item are SearchResponseItem or AggregateResponseItem

As your search (or aggregate) can have multiple page, you can request the next page:

// If your RediSearch search have 15 results in total, but you set the pagination to 10 per page:
/** @var PaginatedResponse $results */

/** @var array<SearchResponseItem> $items */
$items = $results->current();
// $items is a list of 10 SearchResponseItem

$results->next();
/** @var array<SearchResponseItem> $items */
$items = $results->current();
// $items is a list of 5 SearchResponseItem

The sister project macfja/redisearch-integration have a special PHP Iterator (ResponseItemIterator) that allow to loop over every results (across all needed pages) like this:

/** @var \MacFJA\RediSearch\Redis\Client $client */
/** @var \MacFJA\RediSearch\Redis\Command\Search $search */

$results = $client->execute($search);
$allItems = new \MacFJA\RediSearch\Integration\Iterator\ResponseItemIterator($results, $client);
/** @var \MacFJA\RediSearch\Redis\Response\SearchResponseItem $item */
foreach ($allItems as $item) {
    // $item is a "document" result for RediSearch
    echo $item->getField('name');
}

I will write a documentation on how the PaginatedResponse object work

MacFJA added a commit that referenced this issue Apr 5, 2022
Rework paginated response (close #31)
Throw exception is client is not defined in Paginated response (close #30)
Increase the code coverage
MacFJA added a commit that referenced this issue Apr 5, 2022
Rework paginated response (close #31)
Throw exception is client is not defined in Paginated response (close #30)
Increase the code coverage
MacFJA added a commit that referenced this issue Apr 5, 2022
Rework paginated response (close #31)
Throw exception is client is not defined in Paginated response (close #30)
Increase the code coverage
MacFJA added a commit that referenced this issue Jun 6, 2022
Rework paginated response (close #31)
Throw exception is client is not defined in Paginated response (close #30)
Increase the code coverage
@MacFJA MacFJA closed this as completed in #36 Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants