Skip to content

Commit

Permalink
Fixed ValueError exception when SILENTLY_FAIL=True
Browse files Browse the repository at this point in the history
  • Loading branch information
pabluk authored and justin caratzas committed Oct 17, 2013
1 parent 07cfa63 commit ab145f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion haystack/backends/elasticsearch_backend.py
Expand Up @@ -180,7 +180,13 @@ def update(self, index, iterable, commit=True):
}
})

self.conn.bulk_index(self.index_name, 'modelresult', prepped_docs, id_field=ID)
try:
self.conn.bulk_index(self.index_name, 'modelresult', prepped_docs, id_field=ID)
except ValueError:
# pyelasticsearch raise a ValueError exception
# when prepped_docs is empty
if not self.silently_fail:
raise

if commit:
self.conn.refresh(index=self.index_name)
Expand Down
14 changes: 14 additions & 0 deletions tests/elasticsearch_tests/tests/test_elasticsearch_backend.py
Expand Up @@ -266,6 +266,20 @@ def test_non_silent(self):
except:
pass

def test_update_no_documents(self):
url = settings.HAYSTACK_CONNECTIONS['default']['URL']
index_name = settings.HAYSTACK_CONNECTIONS['default']['INDEX_NAME']

sb = connections['default'].backend('default', URL=url, INDEX_NAME=index_name, SILENTLY_FAIL=True)
self.assertEqual(sb.update(self.smmi, []), None)

sb = connections['default'].backend('default', URL=url, INDEX_NAME=index_name, SILENTLY_FAIL=False)
try:
sb.update(self.smmi, [])
self.fail()
except:
pass

def test_update(self):
self.sb.update(self.smmi, self.sample_objs)

Expand Down

0 comments on commit ab145f3

Please sign in to comment.