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