简体   繁体   English

用于SolrQuery的Zend_Paginator适配器

[英]Zend_Paginator adapter for SolrQuery

I am currently working in a Zend_Paginator's adapter for the PECL SolrQuery. 我目前正在使用Zend_Paginator的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. 您可能希望基于SolrObject进行响应,而不是查询。 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? 我假设您指的是count函数必须执行查询以接收找到的行数?

If so, the simplest solution would be to store the numFound in a class variable when executing the query. 如果是这样,最简单的解决方案是在执行查询时将numFound存储在类变量中。 Then, the count function simply retrieves the value of that count if it exists. 然后,count函数只检索该计数的值(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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