Zend_Paginator Adapter for SolrQuery

I am currently working in the Zend_Paginator adapter for PECL SolrQuery. I cannot figure out how to avoid duplicate request. Does anyone have a better implementation?

<?php
require_once 'Zend/Paginator/Adapter/Interface.php';
class Xxx_Paginator_Adapter_SolrQuery implements Zend_Paginator_Adapter_Interface
{
    private $query;
    private $client;
    public function __construct(SolrQuery $query, $client) {
        $this->query = $query;
        $this->client = $client instanceof SolrClient ? $client : new SolrClient($client);
    }
    public function count() {
        $this->query->setRows(0);
        return $this->execute()->numFound;
    }
    public function getItems($offset, $itemCountPerPage) {
        $this->query->setStart($offset)->setRows($itemCountPerPage);
        return $this->execute()->docs;
    }
    private function execute() {
        $response = $this->client->query($this->query)->getResponse();
        return $response['response'];
    }
}
+3
source share
2 answers

You want to do this based on SolrObject for the response, not the request. All the necessary information is there.

$solrResponse = $solrClient->query($query);
$solrObject = $solrResponse->getResponse();
$paginator = new Zend_Paginator(new Xxx_Paginator_Adapter_SolrQuery($solrObject));
+1
source

I assume that you are referencing the count function, which must execute a query to get the number of rows found?

If so, the easiest solution would be to store numFound in a class variable when executing the request. Then the count function simply retrieves the value of this count, if it exists.

0
source

All Articles