From ea644fb4bccfde9ef1f5c1a31d1b1a7be0e35766 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 6 Jun 2016 00:13:56 +0200 Subject: [PATCH] Support pagination with result grouping When using [result grouping](https://cwiki.apache.org/confluence/display/solr/Result+Grouping), we don't get a simple number of documents matched. Instead, related documents are collapsed into groups and the groups are what we (probably) want to paginate. This is a bit whacky as it is technically possible to specify different grouping criteria. In that case, we'd get multiple lists of groups back. This change only looks at the first grouping applied as I don't see how we could make sense of the multi-group case. --- .../Subscriber/Paginate/SolariumQuerySubscriber.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Knp/Component/Pager/Event/Subscriber/Paginate/SolariumQuerySubscriber.php b/src/Knp/Component/Pager/Event/Subscriber/Paginate/SolariumQuerySubscriber.php index 5d4f7320..3c13f385 100644 --- a/src/Knp/Component/Pager/Event/Subscriber/Paginate/SolariumQuerySubscriber.php +++ b/src/Knp/Component/Pager/Event/Subscriber/Paginate/SolariumQuerySubscriber.php @@ -3,6 +3,7 @@ namespace Knp\Component\Pager\Event\Subscriber\Paginate; use Knp\Component\Pager\Event\ItemsEvent; +use Solarium\QueryType\Select\Query\Query; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -19,11 +20,23 @@ public function items(ItemsEvent $event) list($client, $query) = $values; if ($client instanceof \Solarium\Client && $query instanceof \Solarium\QueryType\Select\Query\Query) { + + if ($grouping = $query->getComponent(Query::COMPONENT_GROUPING, false)) { + $grouping->setNumberOfGroups(true); + } + $query->setStart($event->getOffset())->setRows($event->getLimit()); $solrResult = $client->select($query); $event->items = $solrResult->getIterator(); $event->count = $solrResult->getNumFound(); + + if ($event->count === null && $grouping) { + $groups = $solrResult->getComponent('grouping')->getGroups(); + $firstGroup = reset($groups); + $event->count = $firstGroup->getNumberOfGroups(); + } + $event->setCustomPaginationParameter('result', $solrResult); $event->stopPropagation(); }