Skip to content

Fix include original doc #1078

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

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 9 additions & 7 deletions elasticsearch/helpers/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,17 @@ def _process_bulk_chunk(
bulk_data, map(methodcaller("popitem"), resp["items"])
):
ok = 200 <= item.get("status", 500) < 300
if not ok and raise_on_error:
if not ok:
# include original document source
if len(data) > 1:
if op_type != "delete":
item["data"] = data[1]
errors.append({op_type: item})

if ok or not errors:
# if we are not just recording all errors to be able to raise
# them all at once, yield items individually
if raise_on_error:
errors.append({op_type: item})
else:
# if we are not just recording all errors to be able to raise
# them all at once, yield items individually
yield ok, {op_type: item}
else:
yield ok, {op_type: item}

if errors:
Expand Down
41 changes: 41 additions & 0 deletions test_elasticsearch/test_server/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,47 @@ def test_errors_are_collected_properly(self):
self.assertEquals(1, success)
self.assertEquals(1, failed)

def test_return_docs_when_not_raising_errors(self):
bulk_load_index_name = "test_bulk_load"
mappings = {
"properties": {
"test_field": {"type": "integer"}
}
}
self.client.indices.create(bulk_load_index_name, body={"mappings": mappings})
docs = [{"test_field": 1},
{"test_field": "string_error"},
{"test_field": "second_string_error"},
{"test_field": 5},
{"test_field": 6}
]
success, errors = helpers.bulk(self.client, docs, raise_on_error=False,
index=bulk_load_index_name)
self.assertEqual(success, 3)
self.assertEqual(len(errors), 2)
self.assertEqual(errors[0]["index"]["data"], {"test_field": "string_error"})
self.assertEqual(errors[1]["index"]["data"], {"test_field": "second_string_error"})

def test_return_docs_when_not_raising_errors_passing_docs_as_strings(self):
bulk_load_index_name = "test_bulk_load_string_docs"
mappings = {
"properties": {
"test_field": {"type": "integer"}
}
}
self.client.indices.create(bulk_load_index_name, body={"mappings": mappings})
docs = ['{"test_field": 1}',
'{"test_field": "string_error"}',
'{"test_field": "second_string_error"}',
'{"test_field": 5}',
'{"test_field": 6}'
]
success, errors = helpers.bulk(self.client, docs, raise_on_error=False,
index=bulk_load_index_name)
self.assertEqual(success, 3)
self.assertEqual(len(errors), 2)
self.assertEqual(errors[0]["index"]["data"], '{"test_field": "string_error"}')
self.assertEqual(errors[1]["index"]["data"], '{"test_field": "second_string_error"}')

class TestScan(ElasticsearchTestCase):
mock_scroll_responses = [
Expand Down