Skip to content

Commit 34dedb8

Browse files
[Backport 7.x] Scroll search should clear the scroll at the end (#1333)
Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent c901c78 commit 34dedb8

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
lines changed

lib/Helpers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ class Helpers {
104104
}
105105

106106
while (response.body.hits && response.body.hits.hits.length > 0) {
107+
// scroll id is always present in the response, but it might
108+
// change over time based on the number of shards
107109
scroll_id = response.body._scroll_id
108110
response.clear = clear
109111
addDocumentsGetter(response)
110112

111113
yield response
112114

113-
if (!scroll_id || stop === true) {
115+
if (stop === true) {
114116
break
115117
}
116118

@@ -127,6 +129,10 @@ class Helpers {
127129
throw new ResponseError(response)
128130
}
129131
}
132+
133+
if (stop === false) {
134+
await clear()
135+
}
130136
}
131137

132138
/**

test/unit/helpers/scroll.test.js

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,26 @@ test('Scroll search', async t => {
2727
var count = 0
2828
const MockConnection = connection.buildMockConnection({
2929
onRequest (params) {
30-
t.strictEqual(params.querystring, 'scroll=1m')
30+
count += 1
31+
if (params.method === 'POST') {
32+
t.strictEqual(params.querystring, 'scroll=1m')
33+
}
34+
if (count === 4) {
35+
// final automated clear
36+
t.strictEqual(params.method, 'DELETE')
37+
}
3138
return {
3239
body: {
33-
_scroll_id: count === 3 ? undefined : 'id',
40+
_scroll_id: 'id',
3441
count,
3542
hits: {
36-
hits: [
37-
{ _source: { one: 'one' } },
38-
{ _source: { two: 'two' } },
39-
{ _source: { three: 'three' } }
40-
]
43+
hits: count === 3
44+
? []
45+
: [
46+
{ _source: { one: 'one' } },
47+
{ _source: { two: 'two' } },
48+
{ _source: { three: 'three' } }
49+
]
4150
}
4251
}
4352
}
@@ -56,12 +65,7 @@ test('Scroll search', async t => {
5665

5766
for await (const result of scrollSearch) {
5867
t.strictEqual(result.body.count, count)
59-
if (count < 3) {
60-
t.strictEqual(result.body._scroll_id, 'id')
61-
} else {
62-
t.strictEqual(result.body._scroll_id, undefined)
63-
}
64-
count += 1
68+
t.strictEqual(result.body._scroll_id, 'id')
6569
}
6670
})
6771

@@ -115,21 +119,27 @@ test('Scroll search (retry)', async t => {
115119
var count = 0
116120
const MockConnection = connection.buildMockConnection({
117121
onRequest (params) {
122+
count += 1
118123
if (count === 1) {
119-
count += 1
120124
return { body: {}, statusCode: 429 }
121125
}
126+
if (count === 5) {
127+
// final automated clear
128+
t.strictEqual(params.method, 'DELETE')
129+
}
122130
return {
123131
statusCode: 200,
124132
body: {
125-
_scroll_id: count === 4 ? undefined : 'id',
133+
_scroll_id: 'id',
126134
count,
127135
hits: {
128-
hits: [
129-
{ _source: { one: 'one' } },
130-
{ _source: { two: 'two' } },
131-
{ _source: { three: 'three' } }
132-
]
136+
hits: count === 4
137+
? []
138+
: [
139+
{ _source: { one: 'one' } },
140+
{ _source: { two: 'two' } },
141+
{ _source: { three: 'three' } }
142+
]
133143
}
134144
}
135145
}
@@ -151,12 +161,7 @@ test('Scroll search (retry)', async t => {
151161
for await (const result of scrollSearch) {
152162
t.strictEqual(result.body.count, count)
153163
t.notStrictEqual(result.body.count, 1)
154-
if (count < 4) {
155-
t.strictEqual(result.body._scroll_id, 'id')
156-
} else {
157-
t.strictEqual(result.body._scroll_id, undefined)
158-
}
159-
count += 1
164+
t.strictEqual(result.body._scroll_id, 'id')
160165
}
161166
})
162167

@@ -198,20 +203,20 @@ test('Scroll search (retry throws and maxRetries)', async t => {
198203

199204
test('Scroll search (retry throws later)', async t => {
200205
const maxRetries = 5
201-
const expectedAttempts = maxRetries + 1
206+
const expectedAttempts = maxRetries + 2
202207
var count = 0
203208
const MockConnection = connection.buildMockConnection({
204209
onRequest (params) {
205-
// filter_path should not be added if is not already present
210+
count += 1
211+
// filter_path should not be added if is not already present
206212
t.strictEqual(params.querystring, 'scroll=1m')
207213
if (count > 1) {
208-
count += 1
209214
return { body: {}, statusCode: 429 }
210215
}
211216
return {
212217
statusCode: 200,
213218
body: {
214-
_scroll_id: count === 4 ? undefined : 'id',
219+
_scroll_id: 'id',
215220
count,
216221
hits: {
217222
hits: [
@@ -227,7 +232,8 @@ test('Scroll search (retry throws later)', async t => {
227232

228233
const client = new Client({
229234
node: 'http://localhost:9200',
230-
Connection: MockConnection
235+
Connection: MockConnection,
236+
maxRetries
231237
})
232238

233239
const scrollSearch = client.helpers.scrollSearch({
@@ -240,7 +246,6 @@ test('Scroll search (retry throws later)', async t => {
240246
try {
241247
for await (const result of scrollSearch) { // eslint-disable-line
242248
t.strictEqual(result.body.count, count)
243-
count += 1
244249
}
245250
} catch (err) {
246251
t.true(err instanceof errors.ResponseError)
@@ -256,19 +261,23 @@ test('Scroll search documents', async t => {
256261
if (count === 0) {
257262
t.strictEqual(params.querystring, 'filter_path=hits.hits._source%2C_scroll_id&scroll=1m')
258263
} else {
259-
t.strictEqual(params.querystring, 'scroll=1m')
260-
t.strictEqual(params.body, '{"scroll_id":"id"}')
264+
if (params.method !== 'DELETE') {
265+
t.strictEqual(params.querystring, 'scroll=1m')
266+
t.strictEqual(params.body, '{"scroll_id":"id"}')
267+
}
261268
}
262269
return {
263270
body: {
264-
_scroll_id: count === 3 ? undefined : 'id',
271+
_scroll_id: 'id',
265272
count,
266273
hits: {
267-
hits: [
268-
{ _source: { val: 1 * count } },
269-
{ _source: { val: 2 * count } },
270-
{ _source: { val: 3 * count } }
271-
]
274+
hits: count === 3
275+
? []
276+
: [
277+
{ _source: { val: 1 * count } },
278+
{ _source: { val: 2 * count } },
279+
{ _source: { val: 3 * count } }
280+
]
272281
}
273282
}
274283
}
@@ -339,15 +348,19 @@ test('Fix querystring for scroll search', async t => {
339348
if (count === 0) {
340349
t.strictEqual(params.querystring, 'size=1&scroll=1m')
341350
} else {
342-
t.strictEqual(params.querystring, 'scroll=1m')
351+
if (params.method !== 'DELETE') {
352+
t.strictEqual(params.querystring, 'scroll=1m')
353+
}
343354
}
344355
return {
345356
body: {
346-
_scroll_id: count === 3 ? undefined : 'id',
357+
_scroll_id: 'id',
347358
hits: {
348-
hits: [
349-
{ _source: { val: count } }
350-
]
359+
hits: count === 3
360+
? []
361+
: [
362+
{ _source: { val: count } }
363+
]
351364
}
352365
}
353366
}

0 commit comments

Comments
 (0)