2011-04-15 01:23:10 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Paginator;
|
2011-04-15 01:23:10 +02:00
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
use Elastica\SearchableInterface;
|
|
|
|
use Elastica\Query;
|
|
|
|
use Elastica\ResultSet;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
|
|
|
use FOS\ElasticaBundle\Paginator\RawPartialResults;
|
2013-03-27 19:58:34 +01:00
|
|
|
use FOS\ElasticaBundle\Paginator\PartialResultsInterface;
|
2012-05-22 15:23:51 +02:00
|
|
|
|
2011-04-15 01:23:10 +02:00
|
|
|
/**
|
2013-04-22 13:20:10 +02:00
|
|
|
* Allows pagination of Elastica\Query. Does not map results
|
2011-04-15 01:23:10 +02:00
|
|
|
*/
|
2012-05-22 15:23:51 +02:00
|
|
|
class RawPaginatorAdapter implements PaginatorAdapterInterface
|
2011-04-15 01:23:10 +02:00
|
|
|
{
|
|
|
|
/**
|
2013-04-22 13:20:10 +02:00
|
|
|
* @var SearchableInterface the object to search in
|
2012-05-22 15:23:51 +02:00
|
|
|
*/
|
|
|
|
private $searchable = null;
|
|
|
|
|
|
|
|
/**
|
2013-04-22 13:20:10 +02:00
|
|
|
* @var Query the query to search
|
2012-05-22 15:23:51 +02:00
|
|
|
*/
|
|
|
|
private $query = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see PaginatorAdapterInterface::__construct
|
|
|
|
*
|
2013-04-22 13:20:10 +02:00
|
|
|
* @param SearchableInterface $searchable the object to search in
|
|
|
|
* @param Query $query the query to search
|
2012-05-22 15:23:51 +02:00
|
|
|
*/
|
2013-04-22 13:20:10 +02:00
|
|
|
public function __construct(SearchableInterface $searchable, Query $query)
|
2012-05-22 15:23:51 +02:00
|
|
|
{
|
|
|
|
$this->searchable = $searchable;
|
|
|
|
$this->query = $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the paginated results.
|
|
|
|
*
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param $offset
|
|
|
|
* @param $itemCountPerPage
|
2013-04-22 13:20:10 +02:00
|
|
|
* @return 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.
|
|
|
|
*
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $itemCountPerPage
|
|
|
|
* @return PartialResultsInterface
|
2012-05-22 15:23:51 +02:00
|
|
|
*/
|
|
|
|
public function getResults($offset, $itemCountPerPage)
|
|
|
|
{
|
2012-06-13 20:28:16 +02:00
|
|
|
return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
|
2012-05-22 15:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of results.
|
|
|
|
*
|
|
|
|
* @return integer The number of results.
|
|
|
|
*/
|
|
|
|
public function getTotalHits()
|
|
|
|
{
|
2012-08-24 11:23:10 +02:00
|
|
|
return $this->searchable->search($this->query)->getTotalHits();
|
2011-04-15 01:23:10 +02:00
|
|
|
}
|
|
|
|
}
|