added support for setting search options
This commit is contained in:
parent
82e9809ceb
commit
d04a403f71
|
@ -9,7 +9,8 @@ interface FinderInterface
|
|||
*
|
||||
* @param mixed $query Can be a string, an array or an \Elastica\Query object
|
||||
* @param int $limit How many results to get
|
||||
* @param array $options
|
||||
* @return array results
|
||||
*/
|
||||
function find($query, $limit = null);
|
||||
function find($query, $limit = null, $options = array());
|
||||
}
|
||||
|
|
|
@ -12,15 +12,17 @@ interface PaginatedFinderInterface extends FinderInterface
|
|||
* Searches for query results and returns them wrapped in a paginator
|
||||
*
|
||||
* @param mixed $query Can be a string, an array or an \Elastica\Query object
|
||||
* @param array $options
|
||||
* @return Pagerfanta paginated results
|
||||
*/
|
||||
function findPaginated($query);
|
||||
function findPaginated($query, $options = array());
|
||||
|
||||
/**
|
||||
* Creates a paginator adapter for this query
|
||||
*
|
||||
* @param mixed $query
|
||||
* @param array $options
|
||||
* @return PaginatorAdapterInterface
|
||||
*/
|
||||
function createPaginatorAdapter($query);
|
||||
function createPaginatorAdapter($query, $options = array());
|
||||
}
|
||||
|
|
|
@ -29,18 +29,19 @@ class TransformedFinder implements PaginatedFinderInterface
|
|||
*
|
||||
* @param string $query
|
||||
* @param integer $limit
|
||||
* @param array $options
|
||||
* @return array of model objects
|
||||
**/
|
||||
public function find($query, $limit = null)
|
||||
public function find($query, $limit = null, $options = array())
|
||||
{
|
||||
$results = $this->search($query, $limit);
|
||||
$results = $this->search($query, $limit, $options);
|
||||
|
||||
return $this->transformer->transform($results);
|
||||
}
|
||||
|
||||
public function findHybrid($query, $limit = null)
|
||||
public function findHybrid($query, $limit = null, $options = array())
|
||||
{
|
||||
$results = $this->search($query, $limit);
|
||||
$results = $this->search($query, $limit, $options);
|
||||
|
||||
return $this->transformer->hybridTransform($results);
|
||||
}
|
||||
|
@ -64,15 +65,16 @@ class TransformedFinder implements PaginatedFinderInterface
|
|||
/**
|
||||
* @param $query
|
||||
* @param null|int $limit
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
protected function search($query, $limit = null)
|
||||
protected function search($query, $limit = null, $options = array())
|
||||
{
|
||||
$queryObject = Query::create($query);
|
||||
if (null !== $limit) {
|
||||
$queryObject->setSize($limit);
|
||||
}
|
||||
$results = $this->searchable->search($queryObject)->getResults();
|
||||
$results = $this->searchable->search($queryObject, $options)->getResults();
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
@ -81,12 +83,13 @@ class TransformedFinder implements PaginatedFinderInterface
|
|||
* Gets a paginator wrapping the result of a search
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $options
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findPaginated($query)
|
||||
public function findPaginated($query, $options = array())
|
||||
{
|
||||
$queryObject = Query::create($query);
|
||||
$paginatorAdapter = $this->createPaginatorAdapter($queryObject);
|
||||
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
|
||||
|
||||
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
|
||||
}
|
||||
|
@ -94,10 +97,10 @@ class TransformedFinder implements PaginatedFinderInterface
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createPaginatorAdapter($query)
|
||||
public function createPaginatorAdapter($query, $options = array())
|
||||
{
|
||||
$query = Query::create($query);
|
||||
|
||||
return new TransformedPaginatorAdapter($this->searchable, $query, $this->transformer);
|
||||
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,11 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface
|
|||
*/
|
||||
private $query;
|
||||
|
||||
/**
|
||||
* @var array search options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @var integer the number of hits
|
||||
*/
|
||||
|
@ -38,10 +43,11 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface
|
|||
* @param SearchableInterface $searchable the object to search in
|
||||
* @param Query $query the query to search
|
||||
*/
|
||||
public function __construct(SearchableInterface $searchable, Query $query)
|
||||
public function __construct(SearchableInterface $searchable, Query $query, array $options = array())
|
||||
{
|
||||
$this->searchable = $searchable;
|
||||
$this->query = $query;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,7 +78,7 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface
|
|||
$query->setFrom($offset);
|
||||
$query->setSize($itemCountPerPage);
|
||||
|
||||
$resultSet = $this->searchable->search($query);
|
||||
$resultSet = $this->searchable->search($query, $this->options);
|
||||
$this->totalHits = $resultSet->getTotalHits();
|
||||
$this->facets = $resultSet->getFacets();
|
||||
return $resultSet;
|
||||
|
|
|
@ -18,9 +18,9 @@ class TransformedPaginatorAdapter extends RawPaginatorAdapter
|
|||
* @param Query $query the query to search
|
||||
* @param ElasticaToModelTransformerInterface $transformer the transformer for fetching the results
|
||||
*/
|
||||
public function __construct(SearchableInterface $searchable, Query $query, ElasticaToModelTransformerInterface $transformer)
|
||||
public function __construct(SearchableInterface $searchable, Query $query, array $options = array(), ElasticaToModelTransformerInterface $transformer)
|
||||
{
|
||||
parent::__construct($searchable, $query);
|
||||
parent::__construct($searchable, $query, $options);
|
||||
|
||||
$this->transformer = $transformer;
|
||||
}
|
||||
|
|
|
@ -19,23 +19,23 @@ class Repository
|
|||
$this->finder = $finder;
|
||||
}
|
||||
|
||||
public function find($query, $limit=null)
|
||||
public function find($query, $limit = null, $options = array())
|
||||
{
|
||||
return $this->finder->find($query, $limit);
|
||||
return $this->finder->find($query, $limit, $options);
|
||||
}
|
||||
|
||||
public function findHybrid($query, $limit=null)
|
||||
public function findHybrid($query, $limit = null, $options = array())
|
||||
{
|
||||
return $this->finder->findHybrid($query, $limit);
|
||||
return $this->finder->findHybrid($query, $limit, $options);
|
||||
}
|
||||
|
||||
public function findPaginated($query)
|
||||
public function findPaginated($query, $options = array())
|
||||
{
|
||||
return $this->finder->findPaginated($query);
|
||||
return $this->finder->findPaginated($query, $options);
|
||||
}
|
||||
|
||||
public function createPaginatorAdapter($query)
|
||||
public function createPaginatorAdapter($query, $options = array())
|
||||
{
|
||||
return $this->finder->createPaginatorAdapter($query);
|
||||
return $this->finder->createPaginatorAdapter($query, $options);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue