added support for setting search options

This commit is contained in:
Hung Tran 2014-02-04 19:41:38 -06:00
parent 82e9809ceb
commit d04a403f71
6 changed files with 37 additions and 25 deletions

View file

@ -9,7 +9,8 @@ interface FinderInterface
* *
* @param mixed $query Can be a string, an array or an \Elastica\Query object * @param mixed $query Can be a string, an array or an \Elastica\Query object
* @param int $limit How many results to get * @param int $limit How many results to get
* @param array $options
* @return array results * @return array results
*/ */
function find($query, $limit = null); function find($query, $limit = null, $options = array());
} }

View file

@ -12,15 +12,17 @@ interface PaginatedFinderInterface extends FinderInterface
* Searches for query results and returns them wrapped in a paginator * 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 mixed $query Can be a string, an array or an \Elastica\Query object
* @param array $options
* @return Pagerfanta paginated results * @return Pagerfanta paginated results
*/ */
function findPaginated($query); function findPaginated($query, $options = array());
/** /**
* Creates a paginator adapter for this query * Creates a paginator adapter for this query
* *
* @param mixed $query * @param mixed $query
* @param array $options
* @return PaginatorAdapterInterface * @return PaginatorAdapterInterface
*/ */
function createPaginatorAdapter($query); function createPaginatorAdapter($query, $options = array());
} }

View file

@ -29,18 +29,19 @@ class TransformedFinder implements PaginatedFinderInterface
* *
* @param string $query * @param string $query
* @param integer $limit * @param integer $limit
* @param array $options
* @return array of model objects * @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); 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); return $this->transformer->hybridTransform($results);
} }
@ -64,15 +65,16 @@ class TransformedFinder implements PaginatedFinderInterface
/** /**
* @param $query * @param $query
* @param null|int $limit * @param null|int $limit
* @param array $options
* @return array * @return array
*/ */
protected function search($query, $limit = null) protected function search($query, $limit = null, $options = array())
{ {
$queryObject = Query::create($query); $queryObject = Query::create($query);
if (null !== $limit) { if (null !== $limit) {
$queryObject->setSize($limit); $queryObject->setSize($limit);
} }
$results = $this->searchable->search($queryObject)->getResults(); $results = $this->searchable->search($queryObject, $options)->getResults();
return $results; return $results;
} }
@ -81,12 +83,13 @@ class TransformedFinder implements PaginatedFinderInterface
* Gets a paginator wrapping the result of a search * Gets a paginator wrapping the result of a search
* *
* @param string $query * @param string $query
* @param array $options
* @return Pagerfanta * @return Pagerfanta
*/ */
public function findPaginated($query) public function findPaginated($query, $options = array())
{ {
$queryObject = Query::create($query); $queryObject = Query::create($query);
$paginatorAdapter = $this->createPaginatorAdapter($queryObject); $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
} }
@ -94,10 +97,10 @@ class TransformedFinder implements PaginatedFinderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createPaginatorAdapter($query) public function createPaginatorAdapter($query, $options = array())
{ {
$query = Query::create($query); $query = Query::create($query);
return new TransformedPaginatorAdapter($this->searchable, $query, $this->transformer); return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
} }
} }

View file

@ -22,6 +22,11 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface
*/ */
private $query; private $query;
/**
* @var array search options
*/
private $options;
/** /**
* @var integer the number of hits * @var integer the number of hits
*/ */
@ -38,10 +43,11 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface
* @param SearchableInterface $searchable the object to search in * @param SearchableInterface $searchable the object to search in
* @param Query $query the query to search * @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->searchable = $searchable;
$this->query = $query; $this->query = $query;
$this->options = $options;
} }
/** /**
@ -72,7 +78,7 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface
$query->setFrom($offset); $query->setFrom($offset);
$query->setSize($itemCountPerPage); $query->setSize($itemCountPerPage);
$resultSet = $this->searchable->search($query); $resultSet = $this->searchable->search($query, $this->options);
$this->totalHits = $resultSet->getTotalHits(); $this->totalHits = $resultSet->getTotalHits();
$this->facets = $resultSet->getFacets(); $this->facets = $resultSet->getFacets();
return $resultSet; return $resultSet;

View file

@ -18,9 +18,9 @@ class TransformedPaginatorAdapter extends RawPaginatorAdapter
* @param Query $query the query to search * @param Query $query the query to search
* @param ElasticaToModelTransformerInterface $transformer the transformer for fetching the results * @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; $this->transformer = $transformer;
} }

View file

@ -19,23 +19,23 @@ class Repository
$this->finder = $finder; $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);
} }
} }