SearchResponseIterator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Elasticsearch\Helper\Iterators;
  3. use ElasticSearch\Client;
  4. use Iterator;
  5. /**
  6. * Class SearchResponseIterator
  7. *
  8. * @category Elasticsearch
  9. * @package Elasticsearch\Helper\Iterators
  10. * @author Arturo Mejia <arturo.mejia@kreatetechnology.com>
  11. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  12. * @link http://elastic.co
  13. * @see Iterator
  14. */
  15. class SearchResponseIterator implements Iterator
  16. {
  17. /**
  18. * @var Client
  19. */
  20. private $client;
  21. /**
  22. * @var array
  23. */
  24. private $params;
  25. /**
  26. * @var int
  27. */
  28. private $current_key;
  29. /**
  30. * @var array
  31. */
  32. private $current_scrolled_response;
  33. /**
  34. * @var string
  35. */
  36. private $scroll_id;
  37. /**
  38. * @var duration
  39. */
  40. private $scroll_ttl;
  41. /**
  42. * Constructor
  43. *
  44. * @param Client $client
  45. * @param array $params Associative array of parameters
  46. * @see Client::search()
  47. */
  48. public function __construct(Client $client, array $search_params)
  49. {
  50. $this->client = $client;
  51. $this->params = $search_params;
  52. if (isset($search_params['scroll'])) {
  53. $this->scroll_ttl = $search_params['scroll'];
  54. }
  55. }
  56. /**
  57. * Destructor
  58. */
  59. public function __destruct()
  60. {
  61. $this->clearScroll();
  62. }
  63. /**
  64. * Sets the time to live duration of a scroll window
  65. *
  66. * @param string $time_to_live
  67. * @return $this
  68. */
  69. public function setScrollTimeout($time_to_live)
  70. {
  71. $this->scroll_ttl = $time_to_live;
  72. return $this;
  73. }
  74. /**
  75. * Clears the current scroll window if there is a scroll_id stored
  76. *
  77. * @return void
  78. */
  79. private function clearScroll()
  80. {
  81. if (!empty($this->scroll_id)) {
  82. $this->client->clearScroll(
  83. array(
  84. 'scroll_id' => $this->scroll_id,
  85. 'client' => array(
  86. 'ignore' => 404
  87. )
  88. )
  89. );
  90. $this->scroll_id = null;
  91. }
  92. }
  93. /**
  94. * Rewinds the iterator by performing the initial search.
  95. *
  96. *
  97. * @return void
  98. * @see Iterator::rewind()
  99. */
  100. public function rewind()
  101. {
  102. $this->clearScroll();
  103. $this->current_key = 0;
  104. $this->current_scrolled_response = $this->client->search($this->params);
  105. $this->scroll_id = $this->current_scrolled_response['_scroll_id'];
  106. }
  107. /**
  108. * Fetches every "page" after the first one using the lastest "scroll_id"
  109. *
  110. * @return void
  111. * @see Iterator::next()
  112. */
  113. public function next()
  114. {
  115. if ($this->current_key !== 0) {
  116. $this->current_scrolled_response = $this->client->scroll(
  117. array(
  118. 'scroll_id' => $this->scroll_id,
  119. 'scroll' => $this->scroll_ttl
  120. )
  121. );
  122. $this->scroll_id = $this->current_scrolled_response['_scroll_id'];
  123. }
  124. $this->current_key++;
  125. }
  126. /**
  127. * Returns a boolean value indicating if the current page is valid or not
  128. *
  129. * @return bool
  130. * @see Iterator::valid()
  131. */
  132. public function valid()
  133. {
  134. return isset($this->current_scrolled_response['hits']['hits'][0]);
  135. }
  136. /**
  137. * Returns the current "page"
  138. *
  139. * @return array
  140. * @see Iterator::current()
  141. */
  142. public function current()
  143. {
  144. return $this->current_scrolled_response;
  145. }
  146. /**
  147. * Returns the current "page number" of the current "page"
  148. *
  149. * @return int
  150. * @see Iterator::key()
  151. */
  152. public function key()
  153. {
  154. return $this->current_key;
  155. }
  156. }