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\Query; |
15
|
|
|
use Elastica\SearchableInterface; |
16
|
|
|
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; |
17
|
|
|
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter; |
18
|
|
|
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter; |
19
|
|
|
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter; |
20
|
|
|
use FOS\ElasticaBundle\Paginator\TransformedScrollPaginatorAdapter; |
21
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; |
22
|
|
|
use Pagerfanta\Pagerfanta; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Finds elastica documents and map them to persisted objects. |
26
|
|
|
*/ |
27
|
|
|
class TransformedFinder implements PaginatedFinderInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var SearchableInterface |
31
|
|
|
*/ |
32
|
|
|
protected $searchable; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ElasticaToModelTransformerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $transformer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param SearchableInterface $searchable |
41
|
|
|
* @param ElasticaToModelTransformerInterface $transformer |
42
|
8 |
|
*/ |
43
|
|
|
public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer) |
44
|
8 |
|
{ |
45
|
8 |
|
$this->searchable = $searchable; |
46
|
8 |
|
$this->transformer = $transformer; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
1 |
|
*/ |
52
|
|
|
public function find($query, $limit = null, $options = []) |
53
|
1 |
|
{ |
54
|
|
|
$results = $this->search($query, $limit, $options); |
55
|
1 |
|
|
56
|
|
|
return $this->transformer->transform($results); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $query |
61
|
|
|
* @param null|int $limit |
62
|
|
|
* @param array $options |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
1 |
|
*/ |
66
|
|
|
public function findHybrid($query, $limit = null, $options = []) |
67
|
1 |
|
{ |
68
|
|
|
$results = $this->search($query, $limit, $options); |
69
|
1 |
|
|
70
|
|
|
return $this->transformer->hybridTransform($results); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
1 |
|
*/ |
76
|
|
|
public function findPaginated($query, $options = []) |
77
|
1 |
|
{ |
78
|
|
|
$paginatorAdapter = $this->createPaginatorAdapter($query, $options); |
79
|
1 |
|
|
80
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Searches for query hybrid results and returns them wrapped in a paginator. |
85
|
|
|
* |
86
|
|
|
* @param mixed $query Can be a string, an array or an \Elastica\Query object |
87
|
|
|
* |
88
|
|
|
* @return Pagerfanta paginated hybrid results |
89
|
1 |
|
*/ |
90
|
|
|
public function findHybridPaginated($query) |
91
|
1 |
|
{ |
92
|
|
|
$paginatorAdapter = $this->createHybridPaginatorAdapter($query); |
93
|
1 |
|
|
94
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
2 |
|
*/ |
100
|
|
|
public function createPaginatorAdapter($query, $options = []) |
101
|
2 |
|
{ |
102
|
|
|
$query = Query::create($query); |
103
|
2 |
|
|
104
|
|
|
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function createScrollPaginatorAdapter($query, $options = []) |
108
|
|
|
{ |
109
|
2 |
|
$query = Query::create($query); |
110
|
|
|
|
111
|
2 |
|
return new TransformedScrollPaginatorAdapter($this->searchable, $query, $options, $this->transformer); |
112
|
|
|
} |
113
|
2 |
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function createHybridPaginatorAdapter($query) |
118
|
|
|
{ |
119
|
|
|
$query = Query::create($query); |
120
|
|
|
|
121
|
|
|
return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function createRawPaginatorAdapter($query, $options = []) |
128
|
|
|
{ |
129
|
|
|
$query = Query::create($query); |
130
|
|
|
|
131
|
|
|
return new RawPaginatorAdapter($this->searchable, $query, $options); |
132
|
|
|
} |
133
|
1 |
|
|
134
|
|
|
/** |
135
|
1 |
|
* @param $query |
136
|
1 |
|
* @param null|int $limit |
137
|
1 |
|
* @param array $options |
138
|
|
|
* |
139
|
1 |
|
* @return array |
140
|
|
|
*/ |
141
|
1 |
|
protected function search($query, $limit = null, $options = []) |
142
|
|
|
{ |
143
|
|
|
$queryObject = Query::create($query); |
144
|
|
|
if (null !== $limit) { |
145
|
|
|
$queryObject->setSize($limit); |
146
|
|
|
} |
147
|
|
|
$results = $this->searchable->search($queryObject, $options)->getResults(); |
148
|
|
|
|
149
|
|
|
return $results; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|