1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Finder; |
13
|
|
|
|
14
|
|
|
use Elastica\Document; |
15
|
|
|
use Elastica\Query; |
16
|
|
|
use Elastica\SearchableInterface; |
17
|
|
|
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; |
18
|
|
|
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter; |
19
|
|
|
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter; |
20
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; |
21
|
|
|
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; |
|
|
|
|
22
|
|
|
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter; |
23
|
|
|
use Pagerfanta\Pagerfanta; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Finds elastica documents and map them to persisted objects. |
27
|
|
|
*/ |
28
|
|
|
class TransformedFinder implements PaginatedFinderInterface |
29
|
|
|
{ |
30
|
|
|
protected $searchable; |
31
|
|
|
protected $transformer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param SearchableInterface $searchable |
35
|
8 |
|
* @param ElasticaToModelTransformerInterface $transformer |
36
|
|
|
*/ |
37
|
8 |
|
public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer) |
38
|
8 |
|
{ |
39
|
8 |
|
$this->searchable = $searchable; |
40
|
|
|
$this->transformer = $transformer; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
1 |
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function find($query, $limit = null, $options = array()) |
47
|
|
|
{ |
48
|
1 |
|
$results = $this->search($query, $limit, $options); |
49
|
|
|
|
50
|
|
|
return $this->transformer->transform($results); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function findHybrid($query, $limit = null, $options = array()) |
54
|
|
|
{ |
55
|
1 |
|
$results = $this->search($query, $limit, $options); |
56
|
|
|
|
57
|
|
|
return $this->transformer->hybridTransform($results); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Find documents similar to one with passed id. |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @param array $params |
65
|
|
|
* @param array $query |
66
|
|
|
* |
67
|
1 |
|
* @return array of model objects |
68
|
|
|
**/ |
69
|
1 |
|
public function moreLikeThis($id, $params = array(), $query = array()) |
70
|
1 |
|
{ |
71
|
|
|
$doc = new Document($id); |
72
|
1 |
|
$results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults(); |
73
|
|
|
|
74
|
|
|
return $this->transformer->transform($results); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $query |
79
|
|
|
* @param null|int $limit |
80
|
|
|
* @param array $options |
81
|
|
|
* |
82
|
1 |
|
* @return array |
83
|
|
|
*/ |
84
|
1 |
|
protected function search($query, $limit = null, $options = array()) |
85
|
1 |
|
{ |
86
|
1 |
|
$queryObject = Query::create($query); |
87
|
|
|
if (null !== $limit) { |
88
|
1 |
|
$queryObject->setSize($limit); |
89
|
|
|
} |
90
|
1 |
|
$results = $this->searchable->search($queryObject, $options)->getResults(); |
91
|
|
|
|
92
|
|
|
return $results; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
1 |
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function findPaginated($query, $options = array()) |
99
|
1 |
|
{ |
100
|
|
|
$queryObject = Query::create($query); |
101
|
1 |
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options); |
102
|
|
|
|
103
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
2 |
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
2 |
|
public function createPaginatorAdapter($query, $options = array()) |
110
|
|
|
{ |
111
|
2 |
|
$query = Query::create($query); |
112
|
|
|
|
113
|
|
|
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
1 |
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function createHybridPaginatorAdapter($query) |
120
|
|
|
{ |
121
|
1 |
|
$query = Query::create($query); |
122
|
|
|
|
123
|
|
|
return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function createRawPaginatorAdapter($query) |
130
|
|
|
{ |
131
|
|
|
$query = Query::create($query); |
132
|
|
|
|
133
|
|
|
return new RawPaginatorAdapter($this->searchable, $query); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|