File tree Expand file tree Collapse file tree 3 files changed +57
-22
lines changed
test_elasticsearch/test_server Expand file tree Collapse file tree 3 files changed +57
-22
lines changed Original file line number Diff line number Diff line change @@ -336,24 +336,28 @@ async def async_scan(
336
336
for hit in resp ["hits" ]["hits" ]:
337
337
yield hit
338
338
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
+
339
344
# 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."
343
347
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 ,
348
352
)
349
353
if raise_on_error :
350
354
raise ScanError (
351
355
scroll_id ,
352
- "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
356
+ shards_message
353
357
% (
354
- resp [ "_shards" ][ "successful" ] ,
355
- resp [ "_shards" ][ "skipped" ] ,
356
- resp [ "_shards" ][ "total" ] ,
358
+ shards_successful ,
359
+ shards_skipped ,
360
+ shards_total ,
357
361
),
358
362
)
359
363
resp = await client .scroll (
Original file line number Diff line number Diff line change @@ -531,24 +531,28 @@ def scan(
531
531
for hit in resp ["hits" ]["hits" ]:
532
532
yield hit
533
533
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
+
534
539
# 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."
538
542
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 ,
543
547
)
544
548
if raise_on_error :
545
549
raise ScanError (
546
550
scroll_id ,
547
- "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
551
+ shards_message
548
552
% (
549
- resp [ "_shards" ][ "successful" ] ,
550
- resp [ "_shards" ][ "skipped" ] ,
551
- resp [ "_shards" ][ "total" ] ,
553
+ shards_successful ,
554
+ shards_skipped ,
555
+ shards_total ,
552
556
),
553
557
)
554
558
resp = client .scroll (
Original file line number Diff line number Diff line change @@ -508,6 +508,33 @@ def test_clear_scroll(self):
508
508
)
509
509
spy .assert_not_called ()
510
510
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
+
511
538
512
539
class TestReindex (ElasticsearchTestCase ):
513
540
def setup_method (self , _ ):
You can’t perform that action at this time.
0 commit comments