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 aggregations |
37
|
|
|
*/ |
38
|
|
|
private $aggregations; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @see PaginatorAdapterInterface::__construct |
42
|
|
|
* |
43
|
|
|
* @param SearchableInterface $searchable the object to search in |
44
|
|
|
* @param Query $query the query to search |
45
|
|
|
* @param array $options |
46
|
|
|
*/ |
47
|
|
|
public function __construct(SearchableInterface $searchable, Query $query, array $options = array()) |
48
|
|
|
{ |
49
|
|
|
$this->searchable = $searchable; |
50
|
|
|
$this->query = $query; |
51
|
|
|
$this->options = $options; |
52
|
3 |
|
} |
53
|
|
|
|
54
|
3 |
|
/** |
55
|
3 |
|
* Returns the paginated results. |
56
|
3 |
|
* |
57
|
3 |
|
* @param integer $offset |
58
|
|
|
* @param integer $itemCountPerPage |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
* |
62
|
|
|
* @return ResultSet |
63
|
|
|
*/ |
64
|
|
|
protected function getElasticaResults($offset, $itemCountPerPage) |
65
|
|
|
{ |
66
|
|
|
$offset = (integer) $offset; |
67
|
|
|
$itemCountPerPage = (integer) $itemCountPerPage; |
68
|
|
|
$size = $this->query->hasParam('size') |
69
|
|
|
? (integer) $this->query->getParam('size') |
70
|
|
|
: null; |
71
|
|
|
|
72
|
|
|
if (null !== $size && $size < $offset + $itemCountPerPage) { |
73
|
|
|
$itemCountPerPage = $size - $offset; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($itemCountPerPage < 1) { |
77
|
|
|
throw new InvalidArgumentException('$itemCountPerPage must be greater than zero'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$query = clone $this->query; |
81
|
|
|
$query->setFrom($offset); |
82
|
|
|
$query->setSize($itemCountPerPage); |
83
|
|
|
|
84
|
|
|
$resultSet = $this->searchable->search($query, $this->options); |
85
|
|
|
$this->totalHits = $resultSet->getTotalHits(); |
86
|
|
|
|
87
|
|
|
$this->aggregations = $resultSet->getAggregations(); |
88
|
|
|
|
89
|
|
|
return $resultSet; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function getResults($offset, $itemCountPerPage) |
96
|
|
|
{ |
97
|
|
|
return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the number of results. |
102
|
|
|
* |
103
|
|
|
* If genuineTotal is provided as true, total hits is returned from the |
104
|
|
|
* hits.total value from the search results instead of just returning |
105
|
|
|
* the requested size. |
106
|
|
|
* |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function getTotalHits($genuineTotal = false) |
110
|
|
|
{ |
111
|
|
|
if (! isset($this->totalHits)) { |
112
|
|
|
$this->totalHits = $this->searchable->count($this->query); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->query->hasParam('size') && !$genuineTotal |
116
|
|
|
? min($this->totalHits, (integer) $this->query->getParam('size')) |
117
|
|
|
: $this->totalHits; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getAggregations() |
124
|
|
|
{ |
125
|
|
|
if (!isset($this->aggregations)) { |
126
|
|
|
$this->aggregations = $this->searchable->search($this->query)->getAggregations(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->aggregations; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the Query. |
134
|
|
|
* |
135
|
|
|
* @return Query the search query |
136
|
|
|
*/ |
137
|
|
|
public function getQuery() |
138
|
|
|
{ |
139
|
|
|
return $this->query; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|