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\Paginator; |
13
|
|
|
|
14
|
|
|
use Elastica\Query; |
15
|
|
|
use Elastica\ResultSet; |
16
|
|
|
use Elastica\SearchableInterface; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Allows pagination of Elastica\Query. Does not map results. |
21
|
|
|
*/ |
22
|
|
|
class RawPaginatorAdapter implements PaginatorAdapterInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var SearchableInterface the object to search in |
26
|
|
|
*/ |
27
|
|
|
private $searchable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Query the query to search |
31
|
|
|
*/ |
32
|
|
|
private $query; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array search options |
36
|
|
|
*/ |
37
|
|
|
private $options; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int the number of hits |
41
|
|
|
*/ |
42
|
|
|
private $totalHits; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array for the aggregations |
46
|
|
|
*/ |
47
|
|
|
private $aggregations; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array for the suggesters |
51
|
|
|
*/ |
52
|
|
|
private $suggests; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var float |
56
|
|
|
*/ |
57
|
|
|
private $maxScore; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @see PaginatorAdapterInterface::__construct |
61
|
|
|
* |
62
|
|
|
* @param SearchableInterface $searchable the object to search in |
63
|
|
|
* @param Query $query the query to search |
64
|
|
|
* @param array $options |
65
|
|
|
*/ |
66
|
9 |
|
public function __construct(SearchableInterface $searchable, Query $query, array $options = []) |
67
|
|
|
{ |
68
|
9 |
|
$this->searchable = $searchable; |
69
|
9 |
|
$this->query = $query; |
70
|
9 |
|
$this->options = $options; |
71
|
9 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getResults($offset, $itemCountPerPage) |
77
|
|
|
{ |
78
|
|
|
return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the number of results. |
83
|
|
|
* |
84
|
|
|
* If genuineTotal is provided as true, total hits is returned from the |
85
|
|
|
* hits.total value from the search results instead of just returning |
86
|
|
|
* the requested size. |
87
|
|
|
* |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
|
public function getTotalHits($genuineTotal = false) |
91
|
|
|
{ |
92
|
2 |
|
if (!isset($this->totalHits)) { |
93
|
2 |
|
$this->totalHits = $this->searchable->count($this->query); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return $this->query->hasParam('size') && !$genuineTotal |
97
|
1 |
|
? min($this->totalHits, (int) $this->query->getParam('size')) |
98
|
2 |
|
: $this->totalHits; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function getAggregations() |
105
|
|
|
{ |
106
|
1 |
|
if (!isset($this->aggregations)) { |
107
|
1 |
|
$this->aggregations = $this->searchable->search($this->query)->getAggregations(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this->aggregations; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
1 |
|
public function getSuggests() |
117
|
|
|
{ |
118
|
1 |
|
if (!isset($this->suggests)) { |
119
|
1 |
|
$this->suggests = $this->searchable->search($this->query)->getSuggests(); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return $this->suggests; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return float |
127
|
|
|
*/ |
128
|
1 |
|
public function getMaxScore() |
129
|
|
|
{ |
130
|
1 |
|
if (!isset($this->maxScore)) { |
131
|
1 |
|
$this->maxScore = $this->searchable->search($this->query)->getMaxScore(); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return $this->maxScore; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns the Query. |
139
|
|
|
* |
140
|
|
|
* @return Query the search query |
141
|
|
|
*/ |
142
|
1 |
|
public function getQuery() |
143
|
|
|
{ |
144
|
1 |
|
return $this->query; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the paginated results. |
149
|
|
|
* |
150
|
|
|
* @param int $offset |
151
|
|
|
* @param int $itemCountPerPage |
152
|
|
|
* |
153
|
|
|
* @throws \InvalidArgumentException |
154
|
|
|
* |
155
|
|
|
* @return ResultSet |
156
|
|
|
*/ |
157
|
|
|
protected function getElasticaResults($offset, $itemCountPerPage) |
158
|
|
|
{ |
159
|
|
|
$offset = (int) $offset; |
160
|
|
|
$itemCountPerPage = (int) $itemCountPerPage; |
161
|
|
|
$size = $this->query->hasParam('size') |
162
|
|
|
? (int) $this->query->getParam('size') |
163
|
|
|
: null; |
164
|
|
|
|
165
|
|
|
if (null !== $size && $size < $offset + $itemCountPerPage) { |
166
|
|
|
$itemCountPerPage = $size - $offset; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($itemCountPerPage < 1) { |
170
|
|
|
throw new InvalidArgumentException('$itemCountPerPage must be greater than zero'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$query = clone $this->query; |
174
|
|
|
$query->setFrom($offset); |
175
|
|
|
$query->setSize($itemCountPerPage); |
176
|
|
|
|
177
|
|
|
$resultSet = $this->searchable->search($query, $this->options); |
178
|
|
|
$this->totalHits = $resultSet->getTotalHits(); |
179
|
|
|
$this->aggregations = $resultSet->getAggregations(); |
180
|
|
|
$this->suggests = $resultSet->getSuggests(); |
181
|
|
|
$this->maxScore = $resultSet->getMaxScore(); |
182
|
|
|
|
183
|
|
|
return $resultSet; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|