Scroll.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Elastica;
  3. /**
  4. * Scroll Iterator.
  5. *
  6. * @author Manuel Andreo Garcia <andreo.garcia@gmail.com>
  7. *
  8. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
  9. */
  10. class Scroll implements \Iterator
  11. {
  12. /**
  13. * @var string
  14. */
  15. public $expiryTime;
  16. /**
  17. * @var Search
  18. */
  19. protected $_search;
  20. /**
  21. * @var string|null
  22. */
  23. protected $_nextScrollId;
  24. /**
  25. * @var ResultSet|null
  26. */
  27. protected $_currentResultSet;
  28. /**
  29. * 0: scroll<br>
  30. * 1: scroll id.
  31. *
  32. * @var array
  33. */
  34. protected $_options = [null, null];
  35. private $totalPages = 0;
  36. private $currentPage = 0;
  37. /**
  38. * Constructor.
  39. *
  40. * @param Search $search
  41. * @param string $expiryTime
  42. */
  43. public function __construct(Search $search, $expiryTime = '1m')
  44. {
  45. $this->_search = $search;
  46. $this->expiryTime = $expiryTime;
  47. }
  48. /**
  49. * Returns current result set.
  50. *
  51. * @see http://php.net/manual/en/iterator.current.php
  52. *
  53. * @return ResultSet
  54. */
  55. public function current()
  56. {
  57. return $this->_currentResultSet;
  58. }
  59. /**
  60. * Next scroll search.
  61. *
  62. * @see http://php.net/manual/en/iterator.next.php
  63. */
  64. public function next()
  65. {
  66. if ($this->currentPage < $this->totalPages) {
  67. $this->_saveOptions();
  68. $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
  69. $this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId);
  70. $this->_setScrollId($this->_search->search());
  71. $this->_revertOptions();
  72. } else {
  73. // If there are no pages left, we do not need to query ES.
  74. $this->clear();
  75. }
  76. }
  77. /**
  78. * Returns scroll id.
  79. *
  80. * @see http://php.net/manual/en/iterator.key.php
  81. *
  82. * @return string
  83. */
  84. public function key()
  85. {
  86. return $this->_nextScrollId;
  87. }
  88. /**
  89. * Returns true if current result set contains at least one hit.
  90. *
  91. * @see http://php.net/manual/en/iterator.valid.php
  92. *
  93. * @return bool
  94. */
  95. public function valid()
  96. {
  97. return null !== $this->_nextScrollId;
  98. }
  99. /**
  100. * Initial scroll search.
  101. *
  102. * @see http://php.net/manual/en/iterator.rewind.php
  103. */
  104. public function rewind()
  105. {
  106. // reset state
  107. $this->_options = [null, null];
  108. $this->currentPage = 0;
  109. // initial search
  110. $this->_saveOptions();
  111. $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
  112. $this->_search->setOption(Search::OPTION_SCROLL_ID, null);
  113. $this->_setScrollId($this->_search->search());
  114. $this->_revertOptions();
  115. }
  116. /**
  117. * Cleares the search context on ES and marks this Scroll instance as finished.
  118. */
  119. public function clear()
  120. {
  121. if (null !== $this->_nextScrollId) {
  122. $this->_search->getClient()->request(
  123. '_search/scroll',
  124. Request::DELETE,
  125. [Search::OPTION_SCROLL_ID => [$this->_nextScrollId]]
  126. );
  127. // Reset scroll ID so valid() returns false.
  128. $this->_nextScrollId = null;
  129. $this->_currentResultSet = null;
  130. }
  131. }
  132. /**
  133. * Prepares Scroll for next request.
  134. *
  135. * @param ResultSet $resultSet
  136. */
  137. protected function _setScrollId(ResultSet $resultSet)
  138. {
  139. if (0 === $this->currentPage) {
  140. $this->totalPages = $resultSet->count() > 0 ? ceil($resultSet->getTotalHits() / $resultSet->count()) : 0;
  141. }
  142. $this->_currentResultSet = $resultSet;
  143. ++$this->currentPage;
  144. $this->_nextScrollId = $resultSet->getResponse()->isOk() && $resultSet->count() > 0 ? $resultSet->getResponse()->getScrollId() : null;
  145. }
  146. /**
  147. * Save all search options manipulated by Scroll.
  148. */
  149. protected function _saveOptions()
  150. {
  151. if ($this->_search->hasOption(Search::OPTION_SCROLL)) {
  152. $this->_options[0] = $this->_search->getOption(Search::OPTION_SCROLL);
  153. }
  154. if ($this->_search->hasOption(Search::OPTION_SCROLL_ID)) {
  155. $this->_options[1] = $this->_search->getOption(Search::OPTION_SCROLL_ID);
  156. }
  157. }
  158. /**
  159. * Revert search options to previously saved state.
  160. */
  161. protected function _revertOptions()
  162. {
  163. $this->_search->setOption(Search::OPTION_SCROLL, $this->_options[0]);
  164. $this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_options[1]);
  165. }
  166. }