Skip to content

Commit d8a9ccd

Browse files
[7.10] Allow '_shards.skipped' to be absent on scroll responses
Co-authored-by: Seth Michael Larson <seth.larson@elastic.co>
1 parent 231a013 commit d8a9ccd

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

elasticsearch/_async/helpers.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,24 +336,28 @@ async def async_scan(
336336
for hit in resp["hits"]["hits"]:
337337
yield hit
338338

339+
# Default to 0 if the value isn't included in the response
340+
shards_successful = resp["_shards"].get("successful", 0)
341+
shards_skipped = resp["_shards"].get("skipped", 0)
342+
shards_total = resp["_shards"].get("total", 0)
343+
339344
# check if we have any errors
340-
if (resp["_shards"]["successful"] + resp["_shards"]["skipped"]) < resp[
341-
"_shards"
342-
]["total"]:
345+
if (shards_successful + shards_skipped) < shards_total:
346+
shards_message = "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
343347
logger.warning(
344-
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d.",
345-
resp["_shards"]["successful"],
346-
resp["_shards"]["skipped"],
347-
resp["_shards"]["total"],
348+
shards_message,
349+
shards_successful,
350+
shards_skipped,
351+
shards_total,
348352
)
349353
if raise_on_error:
350354
raise ScanError(
351355
scroll_id,
352-
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
356+
shards_message
353357
% (
354-
resp["_shards"]["successful"],
355-
resp["_shards"]["skipped"],
356-
resp["_shards"]["total"],
358+
shards_successful,
359+
shards_skipped,
360+
shards_total,
357361
),
358362
)
359363
resp = await client.scroll(

elasticsearch/helpers/actions.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,24 +531,28 @@ def scan(
531531
for hit in resp["hits"]["hits"]:
532532
yield hit
533533

534+
# Default to 0 if the value isn't included in the response
535+
shards_successful = resp["_shards"].get("successful", 0)
536+
shards_skipped = resp["_shards"].get("skipped", 0)
537+
shards_total = resp["_shards"].get("total", 0)
538+
534539
# check if we have any errors
535-
if (resp["_shards"]["successful"] + resp["_shards"]["skipped"]) < resp[
536-
"_shards"
537-
]["total"]:
540+
if (shards_successful + shards_skipped) < shards_total:
541+
shards_message = "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
538542
logger.warning(
539-
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d.",
540-
resp["_shards"]["successful"],
541-
resp["_shards"]["skipped"],
542-
resp["_shards"]["total"],
543+
shards_message,
544+
shards_successful,
545+
shards_skipped,
546+
shards_total,
543547
)
544548
if raise_on_error:
545549
raise ScanError(
546550
scroll_id,
547-
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
551+
shards_message
548552
% (
549-
resp["_shards"]["successful"],
550-
resp["_shards"]["skipped"],
551-
resp["_shards"]["total"],
553+
shards_successful,
554+
shards_skipped,
555+
shards_total,
552556
),
553557
)
554558
resp = client.scroll(

test_elasticsearch/test_server/test_helpers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,33 @@ def test_clear_scroll(self):
508508
)
509509
spy.assert_not_called()
510510

511+
def test_shards_no_skipped_field(self):
512+
with patch.object(self, "client") as client_mock:
513+
client_mock.search.return_value = {
514+
"_scroll_id": "dummy_id",
515+
"_shards": {"successful": 5, "total": 5},
516+
"hits": {"hits": [{"search_data": 1}]},
517+
}
518+
client_mock.scroll.side_effect = [
519+
{
520+
"_scroll_id": "dummy_id",
521+
"_shards": {"successful": 5, "total": 5},
522+
"hits": {"hits": [{"scroll_data": 42}]},
523+
},
524+
{
525+
"_scroll_id": "dummy_id",
526+
"_shards": {"successful": 5, "total": 5},
527+
"hits": {"hits": []},
528+
},
529+
]
530+
531+
data = list(
532+
helpers.scan(
533+
self.client, index="test_index", size=2, raise_on_error=True
534+
)
535+
)
536+
self.assertEqual(data, [{"search_data": 1}, {"scroll_data": 42}])
537+
511538

512539
class TestReindex(ElasticsearchTestCase):
513540
def setup_method(self, _):

0 commit comments

Comments
 (0)