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-11-18 22:29:02 +01:00
|
|
|
use Elastica\Document;
|
2013-03-27 18:44:06 +01:00
|
|
|
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;
|
2013-04-22 13:20:10 +02:00
|
|
|
use Elastica\SearchableInterface;
|
|
|
|
use Elastica\Query;
|
2011-04-20 22:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
public function __construct(SearchableInterface $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
|
2014-02-05 02:41:38 +01:00
|
|
|
* @param array $options
|
2011-04-28 00:14:21 +02:00
|
|
|
* @return array of model objects
|
2011-04-20 22:53:59 +02:00
|
|
|
**/
|
2014-02-05 02:41:38 +01:00
|
|
|
public function find($query, $limit = null, $options = array())
|
2012-02-23 23:50:46 +01:00
|
|
|
{
|
2014-02-05 02:41:38 +01:00
|
|
|
$results = $this->search($query, $limit, $options);
|
2012-02-23 23:50:46 +01:00
|
|
|
|
|
|
|
return $this->transformer->transform($results);
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:41:38 +01:00
|
|
|
public function findHybrid($query, $limit = null, $options = array())
|
2012-02-23 23:50:46 +01:00
|
|
|
{
|
2014-02-05 02:41:38 +01:00
|
|
|
$results = $this->search($query, $limit, $options);
|
2012-02-23 23:50:46 +01:00
|
|
|
|
|
|
|
return $this->transformer->hybridTransform($results);
|
|
|
|
}
|
|
|
|
|
2013-08-12 12:04:54 +02:00
|
|
|
/**
|
|
|
|
* Find documents similar to one with passed id.
|
|
|
|
*
|
|
|
|
* @param integer $id
|
|
|
|
* @param array $params
|
|
|
|
* @param array $query
|
|
|
|
* @return array of model objects
|
|
|
|
**/
|
|
|
|
public function moreLikeThis($id, $params = array(), $query = array())
|
|
|
|
{
|
|
|
|
$doc = new Document($id);
|
|
|
|
$results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults();
|
|
|
|
|
|
|
|
return $this->transformer->transform($results);
|
2012-02-23 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 12:23:44 +01:00
|
|
|
/**
|
|
|
|
* @param $query
|
|
|
|
* @param null|int $limit
|
2014-02-05 02:41:38 +01:00
|
|
|
* @param array $options
|
2013-03-02 12:23:44 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-02-05 02:41:38 +01:00
|
|
|
protected function search($query, $limit = null, $options = array())
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
2013-04-22 13:20:10 +02:00
|
|
|
$queryObject = Query::create($query);
|
2011-06-11 01:03:17 +02:00
|
|
|
if (null !== $limit) {
|
2013-04-22 13:20:10 +02:00
|
|
|
$queryObject->setSize($limit);
|
2011-06-11 01:03:17 +02:00
|
|
|
}
|
2014-02-05 02:41:38 +01:00
|
|
|
$results = $this->searchable->search($queryObject, $options)->getResults();
|
2011-04-20 22:53:59 +02:00
|
|
|
|
2012-02-23 23:50:46 +01:00
|
|
|
return $results;
|
2011-04-20 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a paginator wrapping the result of a search
|
|
|
|
*
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param string $query
|
2014-02-05 02:41:38 +01:00
|
|
|
* @param array $options
|
2011-07-05 19:42:14 +02:00
|
|
|
* @return Pagerfanta
|
2012-06-13 20:28:16 +02:00
|
|
|
*/
|
2014-02-05 02:41:38 +01:00
|
|
|
public function findPaginated($query, $options = array())
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
2013-04-22 13:20:10 +02:00
|
|
|
$queryObject = Query::create($query);
|
2014-02-05 02:41:38 +01:00
|
|
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
|
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
|
|
|
*/
|
2014-02-05 02:41:38 +01:00
|
|
|
public function createPaginatorAdapter($query, $options = array())
|
2011-04-20 22:53:59 +02:00
|
|
|
{
|
2013-04-22 13:20:10 +02:00
|
|
|
$query = Query::create($query);
|
2013-11-14 16:04:08 +01:00
|
|
|
|
2014-02-05 02:41:38 +01:00
|
|
|
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
|
2011-04-20 22:53:59 +02:00
|
|
|
}
|
|
|
|
}
|