forked from FriendsOfSymfony/FOSElasticaBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRawPaginatorAdapter.php
167 lines (143 loc) · 4.07 KB
/
RawPaginatorAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Paginator;
use Elastica\Query;
use Elastica\ResultSet;
use Elastica\SearchableInterface;
use InvalidArgumentException;
/**
* Allows pagination of Elastica\Query. Does not map results.
*/
class RawPaginatorAdapter implements PaginatorAdapterInterface
{
/**
* @var SearchableInterface the object to search in
*/
private $searchable;
/**
* @var Query the query to search
*/
private $query;
/**
* @var array search options
*/
private $options;
/**
* @var int the number of hits
*/
private $totalHits;
/**
* @var array for the aggregations
*/
private $aggregations;
/**
* @var array for the suggesters
*/
private $suggests;
/**
* @see PaginatorAdapterInterface::__construct
*
* @param SearchableInterface $searchable the object to search in
* @param Query $query the query to search
* @param array $options
*/
public function __construct(SearchableInterface $searchable, Query $query, array $options = [])
{
$this->searchable = $searchable;
$this->query = $query;
$this->options = $options;
}
/**
* Returns the paginated results.
*
* @param int $offset
* @param int $itemCountPerPage
*
* @throws \InvalidArgumentException
*
* @return ResultSet
*/
protected function getElasticaResults($offset, $itemCountPerPage)
{
$offset = (int) $offset;
$itemCountPerPage = (int) $itemCountPerPage;
$size = $this->query->hasParam('size')
? (int) $this->query->getParam('size')
: null;
if (null !== $size && $size < $offset + $itemCountPerPage) {
$itemCountPerPage = $size - $offset;
}
if ($itemCountPerPage < 1) {
throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
}
$query = clone $this->query;
$query->setFrom($offset);
$query->setSize($itemCountPerPage);
$resultSet = $this->searchable->search($query, $this->options);
$this->totalHits = $resultSet->getTotalHits();
$this->aggregations = $resultSet->getAggregations();
$this->suggests = $resultSet->getSuggests();
return $resultSet;
}
/**
* {@inheritdoc}
*/
public function getResults($offset, $itemCountPerPage)
{
return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
}
/**
* Returns the number of results.
*
* If genuineTotal is provided as true, total hits is returned from the
* hits.total value from the search results instead of just returning
* the requested size.
*
* {@inheritdoc}
*/
public function getTotalHits($genuineTotal = false)
{
if (!isset($this->totalHits)) {
$this->totalHits = $this->searchable->count($this->query);
}
return $this->query->hasParam('size') && !$genuineTotal
? min($this->totalHits, (int) $this->query->getParam('size'))
: $this->totalHits;
}
/**
* {@inheritdoc}
*/
public function getAggregations()
{
if (!isset($this->aggregations)) {
$this->aggregations = $this->searchable->search($this->query)->getAggregations();
}
return $this->aggregations;
}
/**
* {@inheritdoc}
*/
public function getSuggests()
{
if (!isset($this->suggests)) {
$this->suggests = $this->searchable->search($this->query)->getSuggests();
}
return $this->suggests;
}
/**
* Returns the Query.
*
* @return Query the search query
*/
public function getQuery()
{
return $this->query;
}
}