Skip to content

Commit 06c8aa7

Browse files
TobionMarek Hernik
authored and
Marek Hernik
committed
Keep track of neccessary scroll pages to skip useless last request (ruflin#1273)
1 parent e71d392 commit 06c8aa7

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file based on the
2020
- added support for the "explain" flag of AnalyzeAPI [#1254](https://github.com/ruflin/Elastica/pull/1254)
2121
- added support for the "request_cache" search option [#1243](https://github.com/ruflin/Elastica/pull/1243)
2222
- skip sending "retry_on_conflict=0" default query param to improve compatibility with Amazon Elasticsearch [#1047](https://github.com/ruflin/Elastica/pull/1047)
23+
- optimized `\Elastica\Scroll` to avoid one request [#1273](https://github.com/ruflin/Elastica/pull/1273)
2324

2425
### Deprecated
2526

lib/Elastica/Scroll.php

+24-19
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ class Scroll implements \Iterator
3232

3333
/**
3434
* 0: scroll<br>
35-
* 1: scroll id<br>
36-
* 2: search type.
35+
* 1: scroll id
3736
*
3837
* @var array
3938
*/
40-
protected $_options = [null, null, null];
39+
protected $_options = [null, null];
40+
41+
private $totalPages = 0;
42+
private $currentPage = 0;
4143

4244
/**
4345
* Constructor.
@@ -70,13 +72,21 @@ public function current()
7072
*/
7173
public function next()
7274
{
73-
$this->_saveOptions();
75+
if ($this->currentPage < $this->totalPages) {
76+
$this->_saveOptions();
7477

75-
$this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
76-
$this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId);
77-
$this->_setScrollId($this->_search->search());
78+
$this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
79+
$this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId);
7880

79-
$this->_revertOptions();
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+
}
8090
}
8191

8292
/**
@@ -100,10 +110,7 @@ public function key()
100110
*/
101111
public function valid()
102112
{
103-
return
104-
$this->_nextScrollId !== null
105-
&& $this->_currentResultSet !== null
106-
&& $this->_currentResultSet->count() > 0;
113+
return $this->_nextScrollId !== null;
107114
}
108115

109116
/**
@@ -114,8 +121,8 @@ public function valid()
114121
public function rewind()
115122
{
116123
// reset state
117-
$this->_nextScrollId = null;
118-
$this->_options = [null, null, null];
124+
$this->_options = [null, null];
125+
$this->currentPage = 0;
119126

120127
// initial search
121128
$this->_saveOptions();
@@ -135,11 +142,9 @@ public function rewind()
135142
protected function _setScrollId(ResultSet $resultSet)
136143
{
137144
$this->_currentResultSet = $resultSet;
138-
139-
$this->_nextScrollId = null;
140-
if ($resultSet->getResponse()->isOk()) {
141-
$this->_nextScrollId = $resultSet->getResponse()->getScrollId();
142-
}
145+
$this->currentPage++;
146+
$this->totalPages = $resultSet->count() > 0 ? ceil($resultSet->getTotalHits() / $resultSet->count()) : 0;
147+
$this->_nextScrollId = $resultSet->getResponse()->isOk() ? $resultSet->getResponse()->getScrollId() : null;
143148
}
144149

145150
/**

0 commit comments

Comments
 (0)