Skip to content

Commit 1345b36

Browse files
committed
Make sure actions aren't modified when passed into bulk
1 parent 79c59d6 commit 1345b36

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

elasticsearch/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def expand_action(data):
2020
action/data lines needed for elasticsearch's
2121
:meth:`~elasticsearch.Elasticsearch.bulk` api.
2222
"""
23+
# make sure we don't alter the action
24+
data = data.copy()
2325
op_type = data.pop('_op_type', 'index')
2426
action = {op_type: {}}
2527
for key in ('_index', '_parent', '_percolate', '_routing', '_timestamp',
@@ -57,8 +59,7 @@ def streaming_bulk(client, actions, chunk_size=500, raise_on_error=False, expand
5759
}
5860
5961
Alternatively, if `_source` is not present, it will pop all metadata fields
60-
from the doc and use the rest as the document data. The dict passed in will
61-
be modified - metadata fields will be popped out.
62+
from the doc and use the rest as the document data.
6263
6364
Alternative actions (`_op_type` field defaults to `index`) can be sent as
6465
well::

test_elasticsearch/test_server/test_helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
from ..test_cases import SkipTest
55

66
class TestStreamingBulk(ElasticTestCase):
7+
def test_actions_remain_unchanged(self):
8+
actions = [{'_id': 1}, {'_id': 2}]
9+
for ok, item in helpers.streaming_bulk(self.client, actions, index='test-index', doc_type='answers'):
10+
self.assertTrue(ok)
11+
self.assertEquals([{'_id': 1}, {'_id': 2}], actions)
12+
713
def test_all_documents_get_inserted(self):
814
docs = [{"answer": x, '_id': x} for x in range(100)]
915
for ok, item in helpers.streaming_bulk(self.client, docs, index='test-index', doc_type='answers', refresh=True):

0 commit comments

Comments
 (0)