2011-04-20 22:53:59 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Finder;
|
2011-04-20 22:53:59 +02:00
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Finder\FinderInterface;
|
|
|
|
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
|
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
|
|
|
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
|
|
|
|
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
|
2011-07-05 19:42:14 +02:00
|
|
|
use Pagerfanta\Pagerfanta;
|
2011-04-20 22:53:59 +02:00
|
|
|
use Elastica_Searchable;
|
|
|
|
use Elastica_Query;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds elastica documents and map them to persisted objects
|
|
|
|
*/
|
2012-07-10 21:38:08 +02:00
|
|
|
class TransformedFinder implements PaginatedFinderInterface
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
|
|
|
protected $searchable;
|
2011-04-27 09:59:04 +02:00
|
|
|
protected $transformer;
|
2011-04-20 22:53:59 +02:00
|
|
|
|
2011-04-27 09:59:04 +02:00
|
|
|
public function __construct(Elastica_Searchable $searchable, ElasticaToModelTransformerInterface $transformer)
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
2011-04-27 09:59:04 +02:00
|
|
|
$this->searchable = $searchable;
|
|
|
|
$this->transformer = $transformer;
|
2011-04-20 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-28 00:14:21 +02:00
|
|
|
* Search for a query string
|
2011-04-20 22:53:59 +02:00
|
|
|
*
|
2011-06-11 01:03:17 +02:00
|
|
|
* @param string $query
|
|
|
|
* @param integer $limit
|
2011-04-28 00:14:21 +02:00
|
|
|
* @return array of model objects
|
2011-04-20 22:53:59 +02:00
|
|
|
**/
|
2011-06-10 23:44:24 +02:00
|
|
|
public function find($query, $limit = null)
|
2012-02-23 23:50:46 +01:00
|
|
|
{
|
|
|
|
$results = $this->search($query, $limit);
|
|
|
|
|
|
|
|
return $this->transformer->transform($results);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findHybrid($query, $limit = null)
|
|
|
|
{
|
|
|
|
$results = $this->search($query, $limit);
|
|
|
|
|
|
|
|
return $this->transformer->hybridTransform($results);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function search($query, $limit = null)
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
2011-06-11 01:03:17 +02:00
|
|
|
$queryObject = Elastica_Query::create($query);
|
|
|
|
if (null !== $limit) {
|
|
|
|
$queryObject->setLimit($limit);
|
|
|
|
}
|
2011-04-20 22:53:59 +02:00
|
|
|
$results = $this->searchable->search($queryObject)->getResults();
|
|
|
|
|
2012-02-23 23:50:46 +01:00
|
|
|
return $results;
|
2011-04-20 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
2012-02-23 23:50:46 +01:00
|
|
|
|
2011-04-20 22:53:59 +02:00
|
|
|
/**
|
|
|
|
* Gets a paginator wrapping the result of a search
|
|
|
|
*
|
2011-07-05 19:42:14 +02:00
|
|
|
* @return Pagerfanta
|
2012-06-13 20:28:16 +02:00
|
|
|
*/
|
2011-04-20 22:53:59 +02:00
|
|
|
public function findPaginated($query)
|
|
|
|
{
|
2011-06-11 01:03:17 +02:00
|
|
|
$queryObject = Elastica_Query::create($query);
|
|
|
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject);
|
2011-04-20 22:53:59 +02:00
|
|
|
|
2012-05-22 15:23:51 +02:00
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
|
2011-04-20 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-27 20:06:00 +02:00
|
|
|
* {@inheritdoc}
|
2011-04-20 22:53:59 +02:00
|
|
|
*/
|
2012-07-27 20:06:00 +02:00
|
|
|
public function createPaginatorAdapter($query)
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
2012-07-27 20:06:00 +02:00
|
|
|
$query = Elastica_Query::create($query);
|
2011-06-11 01:03:17 +02:00
|
|
|
return new TransformedPaginatorAdapter($this->searchable, $query, $this->transformer);
|
2011-04-20 22:53:59 +02:00
|
|
|
}
|
|
|
|
}
|