Skip to content

Commit ddceeb9

Browse files
committed
New: overwrite flag for Solr.add (closes django-haystack#182)
Thanks to @robinsonkwame for the patch
1 parent cbc07af commit ddceeb9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ Contributors:
3535
* Emmanuel Leblond (@touilleMan) for fixing error handling on Python 3
3636
* Michał Jaworski (@swistakm) for improved Sentry-friendly logging
3737
* Upayavira (@upayavira) for SolrCloud support
38+
* Kwame Porter Robinson (@robinsonkwame) for adding overwrite support to Solr.add

pysolr.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def _suggest_terms(self, params):
426426
path = 'terms/?%s' % safe_urlencode(params, True)
427427
return self._send_request('get', path)
428428

429-
def _update(self, message, clean_ctrl_chars=True, commit=True, softCommit=False, waitFlush=None, waitSearcher=None):
429+
def _update(self, message, clean_ctrl_chars=True, commit=True, softCommit=False, waitFlush=None, waitSearcher=None, overwrite=None):
430430
"""
431431
Posts the given xml message to http://<self.url>/update and
432432
returns the result.
@@ -451,6 +451,9 @@ def _update(self, message, clean_ctrl_chars=True, commit=True, softCommit=False,
451451
if waitFlush is not None:
452452
query_vars.append('waitFlush=%s' % str(bool(waitFlush)).lower())
453453

454+
if overwrite is not None:
455+
query_vars.append('overwrite=%s' % str(bool(overwrite)).lower())
456+
454457
if waitSearcher is not None:
455458
query_vars.append('waitSearcher=%s' % str(bool(waitSearcher)).lower())
456459

@@ -810,7 +813,7 @@ def _build_doc(self, doc, boost=None, fieldUpdates=None):
810813

811814
return doc_elem
812815

813-
def add(self, docs, boost=None, fieldUpdates=None, commit=True, softCommit=False, commitWithin=None, waitFlush=None, waitSearcher=None):
816+
def add(self, docs, boost=None, fieldUpdates=None, commit=True, softCommit=False, commitWithin=None, waitFlush=None, waitSearcher=None, overwrite=None):
814817
"""
815818
Adds or updates documents.
816819
@@ -831,6 +834,8 @@ def add(self, docs, boost=None, fieldUpdates=None, commit=True, softCommit=False
831834
832835
Optionally accepts ``waitSearcher``. Default is ``None``.
833836
837+
Optionally accepts ``overwrite``. Default is ``None``.
838+
834839
Usage::
835840
836841
solr.add([
@@ -861,7 +866,7 @@ def add(self, docs, boost=None, fieldUpdates=None, commit=True, softCommit=False
861866

862867
end_time = time.time()
863868
self.log.debug("Built add request of %s docs in %0.2f seconds.", len(message), end_time - start_time)
864-
return self._update(m, commit=commit, softCommit=softCommit, waitFlush=waitFlush, waitSearcher=waitSearcher)
869+
return self._update(m, commit=commit, softCommit=softCommit, waitFlush=waitFlush, waitSearcher=waitSearcher, overwrite=overwrite)
865870

866871
def delete(self, id=None, q=None, commit=True, waitFlush=None, waitSearcher=None):
867872
"""
@@ -1167,11 +1172,10 @@ def _send_request(self, method, path='', body=None, headers=None, files=None):
11671172
time.sleep(self.retry_timeout) # give zookeeper time to notice
11681173
return self._randomized_request(method, path, body, headers, files)
11691174

1170-
def _update(self, message, clean_ctrl_chars=True, commit=True, softCommit=False, waitFlush=None,
1171-
waitSearcher=None):
1175+
def _update(self, *args, **kwargs):
11721176
self.url = self.zookeeper.getLeaderURL(self.collection)
11731177
LOG.debug('Using random leader URL: %s', self.url)
1174-
return Solr._update(self, message, clean_ctrl_chars, commit, softCommit, waitFlush, waitSearcher)
1178+
return Solr._update(self, *args, **kwargs)
11751179

11761180

11771181
class ZooKeeper(object):

tests/test_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,20 @@ def test_commit(self):
539539
self.solr.commit()
540540
self.assertEqual(len(self.solr.search('doc')), 4)
541541

542+
def test_overwrite(self):
543+
self.assertEqual(len(self.solr.search('id:doc_overwrite_1')), 0)
544+
self.solr.add([
545+
{
546+
'id': 'doc_overwrite_1',
547+
'title': 'Kim is awesome.',
548+
},
549+
{
550+
'id': 'doc_overwrite_1',
551+
'title': 'Kim is more awesome.',
552+
}
553+
], overwrite=False)
554+
self.assertEqual(len(self.solr.search('id:doc_overwrite_1')), 2)
555+
542556
def test_optimize(self):
543557
# Make sure it doesn't blow up. Side effects are hard to measure. :/
544558
self.assertEqual(len(self.solr.search('doc')), 3)

0 commit comments

Comments
 (0)