1
|
|
|
<?php |
2
|
|
|
namespace Elastica; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Scroll Iterator. |
6
|
|
|
* |
7
|
|
|
* @author Manuel Andreo Garcia <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html |
10
|
|
|
*/ |
11
|
|
|
class Scroll implements \Iterator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $expiryTime; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Search |
20
|
|
|
*/ |
21
|
|
|
protected $_search; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var null|string |
25
|
|
|
*/ |
26
|
|
|
protected $_nextScrollId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var null|ResultSet |
30
|
|
|
*/ |
31
|
|
|
protected $_currentResultSet; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 0: scroll<br> |
35
|
|
|
* 1: scroll id. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $_options = [null, null]; |
40
|
|
|
|
41
|
|
|
private $totalPages = 0; |
42
|
|
|
private $currentPage = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param Search $search |
48
|
|
|
* @param string $expiryTime |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Search $search, $expiryTime = '1m') |
51
|
|
|
{ |
52
|
|
|
$this->_search = $search; |
53
|
|
|
$this->expiryTime = $expiryTime; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns current result set. |
58
|
|
|
* |
59
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
60
|
|
|
* |
61
|
|
|
* @return ResultSet |
62
|
|
|
*/ |
63
|
|
|
public function current() |
64
|
|
|
{ |
65
|
|
|
return $this->_currentResultSet; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Next scroll search. |
70
|
|
|
* |
71
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
72
|
|
|
*/ |
73
|
|
|
public function next() |
74
|
|
|
{ |
75
|
|
|
if ($this->currentPage < $this->totalPages) { |
76
|
|
|
$this->_saveOptions(); |
77
|
|
|
|
78
|
|
|
$this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime); |
79
|
|
|
$this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId); |
80
|
|
|
|
81
|
|
|
$this->_setScrollId($this->_search->search()); |
82
|
|
|
|
83
|
|
|
$this->_revertOptions(); |
84
|
|
|
} else { |
85
|
|
|
// If there are no pages left, we do not need to query ES. |
86
|
|
|
// Reset scroll ID so valid() returns false. |
87
|
|
|
$this->_nextScrollId = null; |
88
|
|
|
$this->_currentResultSet = null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns scroll id. |
94
|
|
|
* |
95
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function key() |
100
|
|
|
{ |
101
|
|
|
return $this->_nextScrollId; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns true if current result set contains at least one hit. |
106
|
|
|
* |
107
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function valid() |
112
|
|
|
{ |
113
|
|
|
return $this->_nextScrollId !== null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Initial scroll search. |
118
|
|
|
* |
119
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
120
|
|
|
*/ |
121
|
|
|
public function rewind() |
122
|
|
|
{ |
123
|
|
|
// reset state |
124
|
|
|
$this->_options = [null, null]; |
125
|
|
|
$this->currentPage = 0; |
126
|
|
|
|
127
|
|
|
// initial search |
128
|
|
|
$this->_saveOptions(); |
129
|
|
|
|
130
|
|
|
$this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime); |
131
|
|
|
$this->_search->setOption(Search::OPTION_SCROLL_ID, null); |
132
|
|
|
$this->_setScrollId($this->_search->search()); |
133
|
|
|
|
134
|
|
|
$this->_revertOptions(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Prepares Scroll for next request. |
139
|
|
|
* |
140
|
|
|
* @param ResultSet $resultSet |
141
|
|
|
*/ |
142
|
|
|
protected function _setScrollId(ResultSet $resultSet) |
143
|
|
|
{ |
144
|
|
|
if ($this->currentPage === 0) { |
145
|
|
|
$this->totalPages = $resultSet->count() > 0 ? ceil($resultSet->getTotalHits() / $resultSet->count()) : 0; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->_currentResultSet = $resultSet; |
149
|
|
|
++$this->currentPage; |
150
|
|
|
$this->_nextScrollId = $resultSet->getResponse()->isOk() && $resultSet->count() > 0 ? $resultSet->getResponse()->getScrollId() : null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Save all search options manipulated by Scroll. |
155
|
|
|
*/ |
156
|
|
|
protected function _saveOptions() |
157
|
|
|
{ |
158
|
|
View Code Duplication |
if ($this->_search->hasOption(Search::OPTION_SCROLL)) { |
|
|
|
|
159
|
|
|
$this->_options[0] = $this->_search->getOption(Search::OPTION_SCROLL); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
if ($this->_search->hasOption(Search::OPTION_SCROLL_ID)) { |
|
|
|
|
163
|
|
|
$this->_options[1] = $this->_search->getOption(Search::OPTION_SCROLL_ID); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Revert search options to previously saved state. |
169
|
|
|
*/ |
170
|
|
|
protected function _revertOptions() |
171
|
|
|
{ |
172
|
|
|
$this->_search->setOption(Search::OPTION_SCROLL, $this->_options[0]); |
173
|
|
|
$this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_options[1]); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.