Skip to content

Commit 3dd596d

Browse files
authored
Update search iterators to send "scroll_id" inside the request body (#1134)
* Update search iterators to send "scroll_id" inside the request body * Use the page hits "total" property instead of counting the hits
1 parent 62304ec commit 3dd596d

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

src/Elasticsearch/Helper/Iterators/SearchHitIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function rewind(): void
7777

7878
$this->count = 0;
7979
if (isset($current_page['hits']) && isset($current_page['hits']['total'])) {
80-
$this->count = $current_page['hits']['total'];
80+
$this->count = $current_page['hits']['total']['value'] ?? $current_page['hits']['total'];
8181
}
8282

8383
$this->readPageData();

src/Elasticsearch/Helper/Iterators/SearchResponseIterator.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ private function clearScroll(): void
100100
{
101101
if (!empty($this->scroll_id)) {
102102
$this->client->clearScroll(
103-
array(
104-
'scroll_id' => $this->scroll_id,
105-
'client' => array(
103+
[
104+
'body' => [
105+
'scroll_id' => $this->scroll_id
106+
],
107+
'client' => [
106108
'ignore' => 404
107-
)
108-
)
109+
]
110+
]
109111
);
110112
$this->scroll_id = null;
111113
}
@@ -135,8 +137,10 @@ public function next(): void
135137
{
136138
$this->current_scrolled_response = $this->client->scroll(
137139
[
138-
'scroll_id' => $this->scroll_id,
139-
'scroll' => $this->scroll_ttl
140+
'body' => [
141+
'scroll_id' => $this->scroll_id,
142+
'scroll' => $this->scroll_ttl
143+
]
140144
]
141145
);
142146
$this->scroll_id = $this->current_scrolled_response['_scroll_id'];

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public function testWithHits()
6464
[ 'foo' => 'bar1' ],
6565
[ 'foo' => 'bar2' ]
6666
],
67-
'total' => 3
67+
'total' => [
68+
'value' => 3,
69+
'relation' => 'eq'
70+
]
6871
]
6972
],
7073
[
@@ -74,7 +77,10 @@ public function testWithHits()
7477
[ 'foo' => 'bar1' ],
7578
[ 'foo' => 'bar2' ]
7679
],
77-
'total' => 3
80+
'total' => [
81+
'value' => 3,
82+
'relation' => 'eq'
83+
]
7884
]
7985
],
8086
[
@@ -84,7 +90,10 @@ public function testWithHits()
8490
[ 'foo' => 'bar1' ],
8591
[ 'foo' => 'bar2' ]
8692
],
87-
'total' => 3
93+
'total' => [
94+
'value' => 3,
95+
'relation' => 'eq'
96+
]
8897
]
8998
],
9099
[
@@ -94,7 +103,10 @@ public function testWithHits()
94103
[ 'foo' => 'bar1' ],
95104
[ 'foo' => 'bar2' ]
96105
],
97-
'total' => 3
106+
'total' => [
107+
'value' => 3,
108+
'relation' => 'eq'
109+
]
98110
]
99111
],
100112
[
@@ -103,7 +115,10 @@ public function testWithHits()
103115
[ 'foo' => 'bar3' ],
104116
[ 'foo' => 'bar4' ]
105117
],
106-
'total' => 2
118+
'total' => [
119+
'value' => 2,
120+
'relation' => 'eq'
121+
]
107122
]
108123
],
109124
[
@@ -112,7 +127,10 @@ public function testWithHits()
112127
[ 'foo' => 'bar3' ],
113128
[ 'foo' => 'bar4' ]
114129
],
115-
'total' => 2
130+
'total' => [
131+
'value' => 2,
132+
'relation' => 'eq'
133+
]
116134
]
117135
]
118136
);

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ public function testWithHits()
100100
->ordered()
101101
->with(
102102
[
103-
'scroll_id' => 'scroll_id_01',
104-
'scroll' => '5m'
103+
'body' => [
104+
'scroll_id' => 'scroll_id_01',
105+
'scroll' => '5m'
106+
]
105107
]
106108
)
107109
->andReturn(
@@ -122,8 +124,10 @@ public function testWithHits()
122124
->ordered()
123125
->with(
124126
[
125-
'scroll_id' => 'scroll_id_02',
126-
'scroll' => '5m'
127+
'body' => [
128+
'scroll_id' => 'scroll_id_02',
129+
'scroll' => '5m'
130+
]
127131
]
128132
)
129133
->andReturn(
@@ -144,8 +148,10 @@ public function testWithHits()
144148
->ordered()
145149
->with(
146150
[
147-
'scroll_id' => 'scroll_id_03',
148-
'scroll' => '5m'
151+
'body' => [
152+
'scroll_id' => 'scroll_id_03',
153+
'scroll' => '5m'
154+
]
149155
]
150156
)
151157
->andReturn(
@@ -161,8 +167,10 @@ public function testWithHits()
161167
->never()
162168
->with(
163169
[
164-
'scroll_id' => 'scroll_id_04',
165-
'scroll' => '5m'
170+
'body' => [
171+
'scroll_id' => 'scroll_id_04',
172+
'scroll' => '5m'
173+
]
166174
]
167175
);
168176

0 commit comments

Comments
 (0)