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 @@ -332,24 +332,28 @@ async def async_scan(
332
332
for hit in resp ["hits" ]["hits" ]:
333
333
yield hit
334
334
335
+ # Default to 0 if the value isn't included in the response
336
+ shards_successful = resp ["_shards" ].get ("successful" , 0 )
337
+ shards_skipped = resp ["_shards" ].get ("skipped" , 0 )
338
+ shards_total = resp ["_shards" ].get ("total" , 0 )
339
+
335
340
# check if we have any errors
336
- if (resp ["_shards" ]["successful" ] + resp ["_shards" ]["skipped" ]) < resp [
337
- "_shards"
338
- ]["total" ]:
341
+ if (shards_successful + shards_skipped ) < shards_total :
342
+ shards_message = "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
339
343
logger .warning (
340
- "Scroll request has only succeeded on %d (+%d skipped) shards out of %d." ,
341
- resp [ "_shards" ][ "successful" ] ,
342
- resp [ "_shards" ][ "skipped" ] ,
343
- resp [ "_shards" ][ "total" ] ,
344
+ shards_message ,
345
+ shards_successful ,
346
+ shards_skipped ,
347
+ shards_total ,
344
348
)
345
349
if raise_on_error :
346
350
raise ScanError (
347
351
scroll_id ,
348
- "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
352
+ shards_message
349
353
% (
350
- resp [ "_shards" ][ "successful" ] ,
351
- resp [ "_shards" ][ "skipped" ] ,
352
- resp [ "_shards" ][ "total" ] ,
354
+ shards_successful ,
355
+ shards_skipped ,
356
+ shards_total ,
353
357
),
354
358
)
355
359
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 @@ -500,6 +500,33 @@ def test_clear_scroll(self):
500
500
)
501
501
spy .assert_not_called ()
502
502
503
+ def test_shards_no_skipped_field (self ):
504
+ with patch .object (self , "client" ) as client_mock :
505
+ client_mock .search .return_value = {
506
+ "_scroll_id" : "dummy_id" ,
507
+ "_shards" : {"successful" : 5 , "total" : 5 },
508
+ "hits" : {"hits" : [{"search_data" : 1 }]},
509
+ }
510
+ client_mock .scroll .side_effect = [
511
+ {
512
+ "_scroll_id" : "dummy_id" ,
513
+ "_shards" : {"successful" : 5 , "total" : 5 },
514
+ "hits" : {"hits" : [{"scroll_data" : 42 }]},
515
+ },
516
+ {
517
+ "_scroll_id" : "dummy_id" ,
518
+ "_shards" : {"successful" : 5 , "total" : 5 },
519
+ "hits" : {"hits" : []},
520
+ },
521
+ ]
522
+
523
+ data = list (
524
+ helpers .scan (
525
+ self .client , index = "test_index" , size = 2 , raise_on_error = True
526
+ )
527
+ )
528
+ self .assertEqual (data , [{"search_data" : 1 }, {"scroll_data" : 42 }])
529
+
503
530
504
531
class TestReindex (ElasticsearchTestCase ):
505
532
def setup_method (self , _ ):
You can’t perform that action at this time.
0 commit comments