Skip to content

Commit 72f3b15

Browse files
committed
Update SearchResponseIterator to remove old-style scan/scroll flow
1 parent 90cbdfb commit 72f3b15

File tree

2 files changed

+54
-70
lines changed

2 files changed

+54
-70
lines changed

src/Elasticsearch/Helper/Iterators/SearchResponseIterator.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ private function clearScroll()
108108
/**
109109
* Rewinds the iterator by performing the initial search.
110110
*
111-
* The "search_type" parameter will determine if the first "page" contains
112-
* hits or if the first page contains just a "scroll_id"
113111
*
114112
* @return void
115113
* @see Iterator::rewind()
@@ -130,27 +128,27 @@ public function rewind()
130128
*/
131129
public function next()
132130
{
131+
if ($this->current_key !== 0) {
132+
$this->current_scrolled_response = $this->client->scroll(
133+
array(
134+
'scroll_id' => $this->scroll_id,
135+
'scroll' => $this->scroll_ttl
136+
)
137+
);
138+
$this->scroll_id = $this->current_scrolled_response['_scroll_id'];
139+
}
133140
$this->current_key++;
134-
$this->current_scrolled_response = $this->client->scroll(
135-
array(
136-
'scroll_id' => $this->scroll_id,
137-
'scroll' => $this->scroll_ttl
138-
)
139-
);
140-
$this->scroll_id = $this->current_scrolled_response['_scroll_id'];
141141
}
142142

143143
/**
144144
* Returns a boolean value indicating if the current page is valid or not
145-
* based on the number of hits in the page considering that the first page
146-
* might not include any hits
147145
*
148146
* @return bool
149147
* @see Iterator::valid()
150148
*/
151149
public function valid()
152150
{
153-
return ($this->current_key === 0) || isset($this->current_scrolled_response['hits']['hits'][0]);
151+
return isset($this->current_scrolled_response['hits']['hits'][0]);
154152
}
155153

156154
/**

tests/Elasticsearch/Tests/Helper/Iterators/SearchResponseIteratorTest.php

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function tearDown()
2323
public function testWithNoResults()
2424
{
2525
$search_params = array(
26-
'search_type' => 'scan',
2726
'scroll' => '5m',
2827
'index' => 'twitter',
2928
'size' => 1000,
@@ -43,32 +42,7 @@ public function testWithNoResults()
4342
->andReturn(array('_scroll_id' => 'scroll_id_01'));
4443

4544
$mock_client->shouldReceive('scroll')
46-
->once()
47-
->ordered()
48-
->with(
49-
array(
50-
'scroll_id' => 'scroll_id_01',
51-
'scroll' => '5m'
52-
)
53-
)
54-
->andReturn(
55-
array(
56-
'_scroll_id' => 'scroll_id_02',
57-
'hits' => array(
58-
'hits' => array(
59-
)
60-
)
61-
)
62-
);
63-
64-
$mock_client->shouldReceive('scroll')
65-
->never()
66-
->with(
67-
array(
68-
'scroll_id' => 'scroll_id_02',
69-
'scroll' => '5m'
70-
)
71-
);
45+
->never();
7246

7347
$mock_client->shouldReceive('clearScroll')
7448
->once()
@@ -81,10 +55,9 @@ public function testWithNoResults()
8155
$this->assertCount(0, $responses);
8256
}
8357

84-
public function testWithScan()
58+
public function testWithHits()
8559
{
8660
$search_params = array(
87-
'search_type' => 'scan',
8861
'scroll' => '5m',
8962
'index' => 'twitter',
9063
'size' => 1000,
@@ -101,72 +74,85 @@ public function testWithScan()
10174
->once()
10275
->ordered()
10376
->with($search_params)
104-
->andReturn(array('_scroll_id' => 'scroll_id_01'));
77+
->andReturn([
78+
'_scroll_id' => 'scroll_id_01',
79+
'hits' => [
80+
'hits' => [
81+
[
82+
'foo' => 'bar'
83+
]
84+
]
85+
]
86+
]);
10587

10688
$mock_client->shouldReceive('scroll')
10789
->once()
10890
->ordered()
10991
->with(
110-
array(
92+
[
11193
'scroll_id' => 'scroll_id_01',
11294
'scroll' => '5m'
113-
)
95+
]
11496
)
11597
->andReturn(
116-
array(
98+
[
11799
'_scroll_id' => 'scroll_id_02',
118-
'hits' => array(
119-
'hits' => array(
120-
array()
121-
)
122-
)
123-
)
124-
);
100+
'hits' => [
101+
'hits' => [
102+
[
103+
'foo' => 'bar'
104+
]
105+
]
106+
]
107+
]);
125108

126109
$mock_client->shouldReceive('scroll')
127110
->once()
128111
->ordered()
129112
->with(
130-
array(
113+
[
131114
'scroll_id' => 'scroll_id_02',
132115
'scroll' => '5m'
133-
)
116+
]
134117
)
135118
->andReturn(
136-
array(
119+
[
137120
'_scroll_id' => 'scroll_id_03',
138-
'hits' => array(
139-
'hits' => array(
140-
array()
141-
)
142-
)
143-
)
121+
'hits' => [
122+
'hits' => [
123+
[
124+
'foo' => 'bar'
125+
]
126+
]
127+
]
128+
]
144129
);
145130

146131
$mock_client->shouldReceive('scroll')
147132
->once()
148133
->ordered()
149134
->with(
150-
array(
135+
[
151136
'scroll_id' => 'scroll_id_03',
152137
'scroll' => '5m'
153-
)
138+
]
154139
)
155140
->andReturn(
156-
array(
141+
[
157142
'_scroll_id' => 'scroll_id_04',
158-
'hits' => array(
159-
)
160-
)
143+
'hits' => [
144+
'hits' => []
145+
]
146+
]
161147
);
162148

163149
$mock_client->shouldReceive('scroll')
164150
->never()
165151
->with(
166-
array(
152+
[
167153
'scroll_id' => 'scroll_id_04',
168154
'scroll' => '5m'
169-
)
155+
]
170156
);
171157

172158
$mock_client->shouldReceive('clearScroll')
@@ -176,6 +162,6 @@ public function testWithScan()
176162

177163
$responses = new SearchResponseIterator($mock_client, $search_params);
178164

179-
$this->assertCount(2, $responses);
165+
$this->assertCount(4, $responses);
180166
}
181167
}

0 commit comments

Comments
 (0)