diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php | 148 |
1 files changed, 39 insertions, 109 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php b/vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php index c83054e8..6713856e 100644 --- a/vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php +++ b/vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php @@ -1,150 +1,80 @@ <?php - namespace Elastica; /** - * scan and scroll object + * Scan and Scroll Iterator. * - * @category Xodoa - * @package Elastica * @author Manuel Andreo Garcia <andreo.garcia@gmail.com> - * @link http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/guide/current/scan-scroll.html */ -class ScanAndScroll implements \Iterator { - - /** - * time value parameter - * - * @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-scroll.html - * @var string - */ - public $expiryTime; - +class ScanAndScroll extends Scroll +{ /** * @var int */ public $sizePerShard; /** - * @var Search - */ - protected $_search; - - /** - * @var null|string - */ - protected $_nextScrollId = null; - - /** - * @var null|string - */ - protected $_lastScrollId = null; - - /** - * @var null|ResultSet - */ - protected $_currentResultSet = null; - - /** - * Constructs scroll iterator object + * Constructor. * * @param Search $search * @param string $expiryTime - * @param int $sizePerShard + * @param int $sizePerShard */ - public function __construct(Search $search, $expiryTime = '1m', $sizePerShard = 1000) { - $this->_search = $search; - $this->expiryTime = $expiryTime; + public function __construct(Search $search, $expiryTime = '1m', $sizePerShard = 1000) + { $this->sizePerShard = $sizePerShard; - } - /** - * Return the current result set - * - * @link http://php.net/manual/en/iterator.current.php - * @return ResultSet - */ - public function current() { - return $this->_currentResultSet; + parent::__construct($search, $expiryTime); } /** - * Perform next scroll search + * Initial scan search. * - * @link http://php.net/manual/en/iterator.next.php - * @return void - */ - public function next() { - $this->_scroll(); - } - - /** - * Return the scroll id of current scroll request - * - * @link http://php.net/manual/en/iterator.key.php - * @return string - */ - public function key() { - return $this->_lastScrollId; - } - - /** - * Returns true if current result set contains one hit - * - * @link http://php.net/manual/en/iterator.valid.php - * @return boolean - */ - public function valid() { - return - $this->_nextScrollId !== null - && $this->_currentResultSet !== null - && $this->_currentResultSet->count() > 0; - } - - /** - * Start the initial scan search * @link http://php.net/manual/en/iterator.rewind.php - * @throws \Elastica\Exception\InvalidException - * @return void */ - public function rewind() { - $this->_search->getQuery()->setSize($this->sizePerShard); + public function rewind() + { + // reset state + $this->_nextScrollId = null; + $this->_options = array(null, null, null, null); - $this->_search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_SCAN); - $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime); + $this->_saveOptions(); // initial scan request + $this->_search->getQuery()->setSize($this->sizePerShard); + $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime); + $this->_search->setOption(Search::OPTION_SCROLL_ID, null); + $this->_search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_SCAN); $this->_setScrollId($this->_search->search()); - // trigger first scroll request - $this->_scroll(); + $this->_revertOptions(); + + // first scroll request + $this->next(); } /** - * Perform next scroll search - * @throws \Elastica\Exception\InvalidException - * @return void + * Save all search options manipulated by Scroll. */ - protected function _scroll() { - $this->_search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_SCROLL); - $this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId); + protected function _saveOptions() + { + $query = $this->_search->getQuery(); + if ($query->hasParam('size')) { + $this->_options[3] = $query->getParam('size'); + } - $resultSet = $this->_search->search(); - $this->_currentResultSet = $resultSet; - $this->_setScrollId($resultSet); + parent::_saveOptions(); } /** - * Save last scroll id and extract the new one if possible - * @param ResultSet $resultSet + * Revert search options to previously saved state. */ - protected function _setScrollId(ResultSet $resultSet) { - $this->_lastScrollId = $this->_nextScrollId; + protected function _revertOptions() + { + $this->_search->getQuery()->setSize($this->_options[3]); - $this->_nextScrollId = null; - if($resultSet->getResponse()->isOk()) { - $this->_nextScrollId = $resultSet->getResponse()->getScrollId(); - } + parent::_revertOptions(); } - -}
\ No newline at end of file +} |