Skip to content

Commit bf5145a

Browse files
author
Cosimo Streppone
committed
delete(): don't allow None documents in list of IDs
Doesn't make sense to delete None documents, so make it an exception. This means we need to filter the original list for not Nones. Not sure an exception is the wanted behaviour here, but at the same time I wanted to avoid the '<delete></delete>' command being sent over to Solr (although that does NOT delete all documents).
1 parent 74ed600 commit bf5145a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

pysolr.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,12 +825,17 @@ def delete(self, id=None, q=None, commit=True, waitFlush=None, waitSearcher=None
825825
elif id is not None and q is not None:
826826
raise ValueError('You many only specify "id" OR "q", not both.')
827827
elif id is not None:
828-
m = '<delete>'
829828
if type(id) not in (list, set):
830829
id = [ id ]
831-
for i in id:
832-
m += '<id>%s</id>' % i
833-
m += '</delete>'
830+
else:
831+
id = filter(lambda x: x is not None, id)
832+
if len(id) > 0:
833+
m = '<delete>'
834+
for i in id:
835+
m += '<id>%s</id>' % i
836+
m += '</delete>'
837+
else:
838+
raise ValueError('The list of documents to delete was empty.')
834839
elif q is not None:
835840
m = '<delete><query>%s</query></delete>' % q
836841

tests/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ def test_delete(self):
463463
self.solr.delete(id=to_delete)
464464
self.assertEqual(len(self.solr.search('*:*')), 0)
465465

466+
# Can't delete when the list of documents is empty
467+
self.assertRaises(ValueError, self.solr.delete, id=[None, None, None])
468+
self.assertRaises(ValueError, self.solr.delete, id=[None])
469+
466470
# Need at least one.
467471
self.assertRaises(ValueError, self.solr.delete)
468472
# Can't have both.

0 commit comments

Comments
 (0)