1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * Copyright OpenSearch Contributors
7: * SPDX-License-Identifier: Apache-2.0
8: *
9: * OpenSearch PHP client
10: *
11: * @link https://github.com/opensearch-project/opensearch-php/
12: * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
13: * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
14: * @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
15: *
16: * Licensed to Elasticsearch B.V under one or more agreements.
17: * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
18: * the GNU Lesser General Public License, Version 2.1, at your option.
19: * See the LICENSE file in the project root for more information.
20: */
21:
22: namespace OpenSearch\Helper\Iterators;
23:
24: use OpenSearch\Client;
25: use Iterator;
26:
27: // @phpstan-ignore classConstant.deprecatedClass
28: @trigger_error(SearchResponseIterator::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
29:
30: /**
31: * @deprecated in 2.4.0 and will be removed in 3.0.0.
32: */
33: class SearchResponseIterator implements Iterator
34: {
35: /**
36: * @var Client
37: */
38: private $client;
39:
40: /**
41: * @var array
42: */
43: private $params;
44:
45: /**
46: * @var int
47: */
48: private $current_key = 0;
49:
50: /**
51: * @var array
52: */
53: private $current_scrolled_response;
54:
55: /**
56: * @var string|null
57: */
58: private $scroll_id;
59:
60: /**
61: * @var string duration
62: */
63: private $scroll_ttl;
64:
65: /**
66: * Constructor
67: *
68: * @param Client $client
69: * @param array $search_params Associative array of parameters
70: * @see Client::search()
71: */
72: public function __construct(Client $client, array $search_params)
73: {
74: $this->client = $client;
75: $this->params = $search_params;
76:
77: if (isset($search_params['scroll'])) {
78: $this->scroll_ttl = $search_params['scroll'];
79: }
80: }
81:
82: /**
83: * Destructor
84: */
85: public function __destruct()
86: {
87: $this->clearScroll();
88: }
89:
90: /**
91: * Sets the time to live duration of a scroll window
92: *
93: * @param string $time_to_live
94: * @return $this
95: */
96: public function setScrollTimeout(string $time_to_live): SearchResponseIterator
97: {
98: $this->scroll_ttl = $time_to_live;
99: return $this;
100: }
101:
102: /**
103: * Clears the current scroll window if there is a scroll_id stored
104: *
105: * @return void
106: */
107: private function clearScroll(): void
108: {
109: if (!empty($this->scroll_id)) {
110: $this->client->clearScroll(
111: array(
112: 'scroll_id' => $this->scroll_id,
113: 'client' => array(
114: 'ignore' => 404
115: )
116: )
117: );
118: $this->scroll_id = null;
119: }
120: }
121:
122: /**
123: * Rewinds the iterator by performing the initial search.
124: *
125: * @return void
126: * @see Iterator::rewind()
127: */
128: public function rewind(): void
129: {
130: $this->clearScroll();
131: $this->current_key = 0;
132: $this->current_scrolled_response = $this->client->search($this->params);
133: $this->scroll_id = $this->current_scrolled_response['_scroll_id'];
134: }
135:
136: /**
137: * Fetches every "page" after the first one using the lastest "scroll_id"
138: *
139: * @return void
140: * @see Iterator::next()
141: */
142: public function next(): void
143: {
144: $this->current_scrolled_response = $this->client->scroll(
145: [
146: 'scroll' => $this->scroll_ttl,
147: 'body' => [
148: 'scroll_id' => $this->scroll_id,
149: ],
150: ]
151: );
152: $this->scroll_id = $this->current_scrolled_response['_scroll_id'];
153: $this->current_key++;
154: }
155:
156: /**
157: * Returns a boolean value indicating if the current page is valid or not
158: *
159: * @return bool
160: * @see Iterator::valid()
161: */
162: public function valid(): bool
163: {
164: return isset($this->current_scrolled_response['hits']['hits'][0]);
165: }
166:
167: /**
168: * Returns the current "page"
169: *
170: * @return array
171: * @see Iterator::current()
172: */
173: public function current(): array
174: {
175: return $this->current_scrolled_response;
176: }
177:
178: /**
179: * Returns the current "page number" of the current "page"
180: *
181: * @return int
182: * @see Iterator::key()
183: */
184: public function key(): int
185: {
186: return $this->current_key;
187: }
188: }
189: