简体   繁体   中英

Zend_Paginator adapter for SolrQuery

I am currently working in a Zend_Paginator's adapter for the PECL SolrQuery. I can't figure a way to avoid the duplicate query. 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'];
    }
}

You would want to do it based off the SolrObject for the response, rather than the query. All of the information you need is in there.

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

I assume you're referring to the count function having to execute the query to receive the number of rows found?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM