fix Elasticsearch.index with id=0

Fixes #164 Thanks larsmans!
This commit is contained in:
Honza Král
2014-12-30 18:50:52 +01:00
parent 72d24906d7
commit 953c66e650
2 changed files with 12 additions and 1 deletions
+2 -1
View File
@@ -252,7 +252,8 @@ class Elasticsearch(object):
for param in (index, doc_type, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
_, data = self.transport.perform_request('PUT' if id else 'POST',
method = 'POST' if id in SKIP_IN_PATH else 'PUT'
_, data = self.transport.perform_request(method,
_make_path(index, doc_type, id), params=params, body=body)
return data
@@ -68,3 +68,13 @@ class TestClient(ElasticsearchTestCase):
def test_repr_truncates_host_to_10(self):
hosts = [{"host": "es" + str(i)} for i in range(20)]
self.assertNotIn("es5", repr(Elasticsearch(hosts)))
def test_index_uses_post_if_id_is_empty(self):
self.client.index(index='my-index', doc_type='test-doc', id='', body={})
self.assert_url_called('POST', '/my-index/test-doc')
def test_index_uses_put_if_id_is_not_empty(self):
self.client.index(index='my-index', doc_type='test-doc', id=0, body={})
self.assert_url_called('PUT', '/my-index/test-doc/0')