diff --git a/elasticsearch/helpers.py b/elasticsearch/helpers.py index 1772533b..802dce9c 100644 --- a/elasticsearch/helpers.py +++ b/elasticsearch/helpers.py @@ -56,14 +56,14 @@ def bulk_index(client, docs, chunk_size=500, stats_only=False, **kwargs): for req, item in zip(bulk_actions[::2], resp['items']): # TODO: better reporting - act = 'index' if '_id' in req['index'] else 'create' + ok = item['index' if '_id' in req['index'] else 'create'].get('ok') if stats_only: - if item[act]['ok']: + if ok: success += 1 else: failed += 1 else: - if item[act]['ok']: + if ok: success.append(item) else: failed.append(item) diff --git a/test_elasticsearch/test_server/test_helpers.py b/test_elasticsearch/test_server/test_helpers.py index 3c83846b..967e74f6 100644 --- a/test_elasticsearch/test_server/test_helpers.py +++ b/test_elasticsearch/test_server/test_helpers.py @@ -20,6 +20,25 @@ class TestBulkIndex(ElasticTestCase): self.assertEquals(0, failed) self.assertEquals(100, self.client.count(index='test-index', doc_type='answers')['count']) + def test_errors_are_collected_properly(self): + self.client.indices.create("i", + { + "mappings": {"t": {"properties": {"a": {"type": "integer"}}}}, + "settings": {"number_of_shards": 1, "number_of_replicas": 0} + }) + self.client.cluster.health(wait_for_status="yellow") + + success, failed = helpers.bulk_index( + self.client, + [{"a": 42}, {"a": "c"}], + index="i", + doc_type="t", + stats_only=True + ) + self.assertEquals(1, success) + self.assertEquals(1, failed) + + class TestScan(ElasticTestCase): def test_all_documents_are_read(self): bulk = []