Skip to content

Commit 64fa5e0

Browse files
committed
elasticsearch response support added to search iterator response
1 parent f536eab commit 64fa5e0

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/Helper/Iterators/SearchResponseIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function rewind(): void
128128
{
129129
$this->clearScroll();
130130
$this->currentKey = 0;
131-
$this->currentScrolledResponse = $this->client->search($this->params);
131+
$this->currentScrolledResponse = $this->client->search($this->params)->asArray();
132132
$this->scrollId = $this->currentScrolledResponse['_scroll_id'];
133133
}
134134

@@ -147,7 +147,7 @@ public function next(): void
147147
'scroll' => $this->scroll_ttl
148148
]
149149
]
150-
);
150+
)->asArray();
151151
$this->scrollId = $this->currentScrolledResponse['_scroll_id'];
152152
$this->currentKey++;
153153
}

tests/Helper/Iterators/SearchResponseIteratorTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
use Elastic\Elasticsearch\ClientInterface;
2020
use Elastic\Elasticsearch\Helper\Iterators\SearchResponseIterator;
21+
use Elastic\Elasticsearch\Response\Elasticsearch;
2122
use Mockery as m;
23+
use Nyholm\Psr7\Factory\Psr17Factory;
2224
use PHPUnit\Framework\TestCase;
2325

2426
/**
@@ -33,6 +35,18 @@ public function tearDown(): void
3335
m::close();
3436
}
3537

38+
private function elasticsearchResponse($data): Elasticsearch
39+
{
40+
$elasticsearch = new Elasticsearch();
41+
$body = (new Psr17Factory())->createStream(json_encode($data));
42+
$response = (new Psr17Factory())->createResponse(200)->withBody($body)
43+
->withHeader('X-Elastic-Product', 'Elasticsearch')
44+
->withHeader('Content-Type', 'application/json');
45+
$elasticsearch->setResponse($response);
46+
47+
return $elasticsearch;
48+
}
49+
3650
public function testWithNoResults(): void
3751
{
3852
$search_params = [
@@ -51,7 +65,7 @@ public function testWithNoResults(): void
5165
$mock_client->shouldReceive('search')
5266
->twice()
5367
->with($search_params)
54-
->andReturn(['_scroll_id' => 'scroll_id_01']);
68+
->andReturn($this->elasticsearchResponse(['_scroll_id' => 'scroll_id_01']));
5569

5670
$mock_client->shouldReceive('clearScroll')
5771
->twice()
@@ -81,8 +95,7 @@ public function testWithHits()
8195
->once()
8296
->ordered()
8397
->with($search_params)
84-
->andReturn(
85-
[
98+
->andReturn($this->elasticsearchResponse([
8699
'_scroll_id' => 'scroll_id_01',
87100
'hits' => [
88101
'hits' => [
@@ -91,7 +104,7 @@ public function testWithHits()
91104
]
92105
]
93106
]
94-
]
107+
])
95108
);
96109

97110
$mock_client->shouldReceive('scroll')
@@ -105,8 +118,7 @@ public function testWithHits()
105118
]
106119
]
107120
)
108-
->andReturn(
109-
[
121+
->andReturn($this->elasticsearchResponse([
110122
'_scroll_id' => 'scroll_id_02',
111123
'hits' => [
112124
'hits' => [
@@ -115,7 +127,7 @@ public function testWithHits()
115127
]
116128
]
117129
]
118-
]
130+
])
119131
);
120132

121133
$mock_client->shouldReceive('scroll')
@@ -129,8 +141,7 @@ public function testWithHits()
129141
]
130142
]
131143
)
132-
->andReturn(
133-
[
144+
->andReturn($this->elasticsearchResponse([
134145
'_scroll_id' => 'scroll_id_03',
135146
'hits' => [
136147
'hits' => [
@@ -139,7 +150,7 @@ public function testWithHits()
139150
]
140151
]
141152
]
142-
]
153+
])
143154
);
144155

145156
$mock_client->shouldReceive('scroll')
@@ -153,13 +164,13 @@ public function testWithHits()
153164
]
154165
]
155166
)
156-
->andReturn(
167+
->andReturn($this->elasticsearchResponse(
157168
[
158169
'_scroll_id' => 'scroll_id_04',
159170
'hits' => [
160171
'hits' => []
161172
]
162-
]
173+
])
163174
);
164175

165176
$mock_client->shouldReceive('scroll')

0 commit comments

Comments
 (0)