1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | namespace OpenSearch\Helper\Iterators; |
23: | |
24: | use OpenSearch\Client; |
25: | use Iterator; |
26: | |
27: | |
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: | |
32: | |
33: | class SearchResponseIterator implements Iterator |
34: | { |
35: | |
36: | |
37: | |
38: | private $client; |
39: | |
40: | |
41: | |
42: | |
43: | private $params; |
44: | |
45: | |
46: | |
47: | |
48: | private $current_key = 0; |
49: | |
50: | |
51: | |
52: | |
53: | private $current_scrolled_response; |
54: | |
55: | |
56: | |
57: | |
58: | private $scroll_id; |
59: | |
60: | |
61: | |
62: | |
63: | private $scroll_ttl; |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
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: | |
84: | |
85: | public function __destruct() |
86: | { |
87: | $this->clearScroll(); |
88: | } |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
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: | |
104: | |
105: | |
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: | |
124: | |
125: | |
126: | |
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: | |
138: | |
139: | |
140: | |
141: | |
142: | public function next(): void |
143: | { |
144: | $this->current_scrolled_response = $this->client->scroll( |
145: | [ |
146: | 'scroll_id' => $this->scroll_id, |
147: | 'scroll' => $this->scroll_ttl |
148: | ] |
149: | ); |
150: | $this->scroll_id = $this->current_scrolled_response['_scroll_id']; |
151: | $this->current_key++; |
152: | } |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | public function valid(): bool |
161: | { |
162: | return isset($this->current_scrolled_response['hits']['hits'][0]); |
163: | } |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | public function current(): array |
172: | { |
173: | return $this->current_scrolled_response; |
174: | } |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | public function key(): int |
183: | { |
184: | return $this->current_key; |
185: | } |
186: | } |
187: | |