Skip to content

Allow '_shards.skipped' to be absent on scroll responses #1451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions elasticsearch/_async/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,24 +332,28 @@ async def async_scan(
for hit in resp["hits"]["hits"]:
yield hit

# Default to 0 if the value isn't included in the response
shards_successful = resp["_shards"].get("successful", 0)
shards_skipped = resp["_shards"].get("skipped", 0)
shards_total = resp["_shards"].get("total", 0)

# check if we have any errors
if (resp["_shards"]["successful"] + resp["_shards"]["skipped"]) < resp[
"_shards"
]["total"]:
if (shards_successful + shards_skipped) < shards_total:
shards_message = "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
logger.warning(
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d.",
resp["_shards"]["successful"],
resp["_shards"]["skipped"],
resp["_shards"]["total"],
shards_message,
shards_successful,
shards_skipped,
shards_total,
)
if raise_on_error:
raise ScanError(
scroll_id,
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
shards_message
% (
resp["_shards"]["successful"],
resp["_shards"]["skipped"],
resp["_shards"]["total"],
shards_successful,
shards_skipped,
shards_total,
),
)
resp = await client.scroll(
Expand Down
26 changes: 15 additions & 11 deletions elasticsearch/helpers/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,24 +531,28 @@ def scan(
for hit in resp["hits"]["hits"]:
yield hit

# Default to 0 if the value isn't included in the response
shards_successful = resp["_shards"].get("successful", 0)
shards_skipped = resp["_shards"].get("skipped", 0)
shards_total = resp["_shards"].get("total", 0)

# check if we have any errors
if (resp["_shards"]["successful"] + resp["_shards"]["skipped"]) < resp[
"_shards"
]["total"]:
if (shards_successful + shards_skipped) < shards_total:
shards_message = "Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
logger.warning(
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d.",
resp["_shards"]["successful"],
resp["_shards"]["skipped"],
resp["_shards"]["total"],
shards_message,
shards_successful,
shards_skipped,
shards_total,
)
if raise_on_error:
raise ScanError(
scroll_id,
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d."
shards_message
% (
resp["_shards"]["successful"],
resp["_shards"]["skipped"],
resp["_shards"]["total"],
shards_successful,
shards_skipped,
shards_total,
),
)
resp = client.scroll(
Expand Down
27 changes: 27 additions & 0 deletions test_elasticsearch/test_server/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,33 @@ def test_clear_scroll(self):
)
spy.assert_not_called()

def test_shards_no_skipped_field(self):
with patch.object(self, "client") as client_mock:
client_mock.search.return_value = {
"_scroll_id": "dummy_id",
"_shards": {"successful": 5, "total": 5},
"hits": {"hits": [{"search_data": 1}]},
}
client_mock.scroll.side_effect = [
{
"_scroll_id": "dummy_id",
"_shards": {"successful": 5, "total": 5},
"hits": {"hits": [{"scroll_data": 42}]},
},
{
"_scroll_id": "dummy_id",
"_shards": {"successful": 5, "total": 5},
"hits": {"hits": []},
},
]

data = list(
helpers.scan(
self.client, index="test_index", size=2, raise_on_error=True
)
)
self.assertEqual(data, [{"search_data": 1}, {"scroll_data": 42}])


class TestReindex(ElasticsearchTestCase):
def setup_method(self, _):
Expand Down