2011-04-15 01:23:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOQ\ElasticaBundle\Paginator;
|
|
|
|
|
2012-05-22 15:23:51 +02:00
|
|
|
use Elastica_Searchable;
|
|
|
|
use Elastica_Query;
|
|
|
|
use FOQ\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
|
|
|
use FOQ\ElasticaBundle\Paginator\RawPartialResults;
|
|
|
|
|
2011-04-15 01:23:10 +02:00
|
|
|
/**
|
|
|
|
* Allows pagination of Elastica_Query. Does not map results
|
|
|
|
*/
|
2012-05-22 15:23:51 +02:00
|
|
|
class RawPaginatorAdapter implements PaginatorAdapterInterface
|
2011-04-15 01:23:10 +02:00
|
|
|
{
|
|
|
|
/**
|
2012-05-22 15:23:51 +02:00
|
|
|
* @var Elastica_SearchableInterface the object to search in
|
|
|
|
*/
|
|
|
|
private $searchable = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Elastica_Query the query to search
|
|
|
|
*/
|
|
|
|
private $query = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see PaginatorAdapterInterface::__construct
|
|
|
|
*
|
|
|
|
* @param Elastica_SearchableInterface the object to search in
|
|
|
|
* @param Elastica_Query the query to search
|
|
|
|
*/
|
|
|
|
public function __construct(Elastica_Searchable $searchable, Elastica_Query $query)
|
|
|
|
{
|
|
|
|
$this->searchable = $searchable;
|
|
|
|
$this->query = $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the paginated results.
|
|
|
|
*
|
|
|
|
* @return Elastica_ResultSet
|
2011-04-15 01:23:10 +02:00
|
|
|
*/
|
2012-05-22 15:23:51 +02:00
|
|
|
protected function getElasticaResults($offset, $itemCountPerPage)
|
2011-04-15 01:23:10 +02:00
|
|
|
{
|
2012-05-22 15:23:51 +02:00
|
|
|
$query = clone $this->query;
|
|
|
|
$query->setFrom($offset);
|
|
|
|
$query->setLimit($itemCountPerPage);
|
|
|
|
|
|
|
|
return $this->searchable->search($query);
|
|
|
|
}
|
2011-04-15 01:23:10 +02:00
|
|
|
|
2012-05-22 15:23:51 +02:00
|
|
|
/**
|
|
|
|
* Returns the paginated results.
|
|
|
|
*
|
|
|
|
* @return FOQ\ElasticaBundle\Paginator\PartialResultInterface
|
|
|
|
*/
|
|
|
|
public function getResults($offset, $itemCountPerPage)
|
|
|
|
{
|
|
|
|
return new RawPartialResults($this->getElasticaResults($offset,$itemCountPerPage));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of results.
|
|
|
|
*
|
|
|
|
* @return integer The number of results.
|
|
|
|
*/
|
|
|
|
public function getTotalHits()
|
|
|
|
{
|
|
|
|
return $this->searchable->count($this->query);
|
2011-04-15 01:23:10 +02:00
|
|
|
}
|
|
|
|
}
|