Skip to content

Commit

Permalink
Add softCommit support (closes #98)
Browse files Browse the repository at this point in the history
add() and commit() may now be called with softCommit=True

Thanks to @sicarrots for the patch
  • Loading branch information
acdha committed Oct 24, 2014
1 parent 211b26b commit 6e62fad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -29,3 +29,4 @@ Contributors:
* pabluk for Tika integration improvements.
* gthb for a patch to add grouping support.
* timsavage for a patch making add() compatible with generators
* Karol Sikora (@sicarrots) for Solr 4 softCommit support
16 changes: 11 additions & 5 deletions pysolr.py
Expand Up @@ -352,7 +352,7 @@ def _suggest_terms(self, params):
path = 'terms/?%s' % safe_urlencode(params, True)
return self._send_request('get', path)

def _update(self, message, clean_ctrl_chars=True, commit=True, waitFlush=None, waitSearcher=None):
def _update(self, message, clean_ctrl_chars=True, commit=True, softCommit=False, waitFlush=None, waitSearcher=None):
"""
Posts the given xml message to http://<self.url>/update and
returns the result.
Expand All @@ -371,6 +371,8 @@ def _update(self, message, clean_ctrl_chars=True, commit=True, waitFlush=None, w

if commit is not None:
query_vars.append('commit=%s' % str(bool(commit)).lower())
elif softCommit is not None:
query_vars.append('softCommit=%s' % str(bool(softCommit)).lower())

if waitFlush is not None:
query_vars.append('waitFlush=%s' % str(bool(waitFlush)).lower())
Expand Down Expand Up @@ -740,7 +742,7 @@ def _build_doc(self, doc, boost=None):

return doc_elem

def add(self, docs, commit=True, boost=None, commitWithin=None, waitFlush=None, waitSearcher=None):
def add(self, docs, commit=True, softCommit=False, boost=None, commitWithin=None, waitFlush=None, waitSearcher=None):
"""
Adds or updates documents.
Expand All @@ -749,6 +751,8 @@ def add(self, docs, commit=True, boost=None, commitWithin=None, waitFlush=None,
Optionally accepts ``commit``. Default is ``True``.
Optionally accepts ``softCommit``. Default is ``False``.
Optionally accepts ``boost``. Default is ``None``.
Optionally accepts ``commitWithin``. Default is ``None``.
Expand Down Expand Up @@ -787,7 +791,7 @@ def add(self, docs, commit=True, boost=None, commitWithin=None, waitFlush=None,

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

def delete(self, id=None, q=None, commit=True, waitFlush=None, waitSearcher=None):
"""
Expand Down Expand Up @@ -820,7 +824,7 @@ def delete(self, id=None, q=None, commit=True, waitFlush=None, waitSearcher=None

return self._update(m, commit=commit, waitFlush=waitFlush, waitSearcher=waitSearcher)

def commit(self, waitFlush=None, waitSearcher=None, expungeDeletes=None):
def commit(self, softCommit=False, waitFlush=None, waitSearcher=None, expungeDeletes=None):
"""
Forces Solr to write the index data to disk.
Expand All @@ -830,6 +834,8 @@ def commit(self, waitFlush=None, waitSearcher=None, expungeDeletes=None):
Optionally accepts ``waitSearcher``. Default is ``None``.
Optionally accepts ``softCommit``. Default is ``False``.
Usage::
solr.commit()
Expand All @@ -840,7 +846,7 @@ def commit(self, waitFlush=None, waitSearcher=None, expungeDeletes=None):
else:
msg = '<commit />'

return self._update(msg, waitFlush=waitFlush, waitSearcher=waitSearcher)
return self._update(msg, softCommit=softCommit, waitFlush=waitFlush, waitSearcher=waitSearcher)

def optimize(self, waitFlush=None, waitSearcher=None, maxSegments=None):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/client.py
Expand Up @@ -226,6 +226,11 @@ def test__update(self):
resp_body = self.solr._update(xml_body)
self.assertTrue('<int name="status">0</int>' in resp_body)

def test__soft_commit(self):
xml_body = '<add><doc><field name="id">doc_12</field><field name="title">Whee!</field></doc></add>'
resp_body = self.solr._update(xml_body, softCommit=True)
self.assertTrue('<int name="status">0</int>' in resp_body)

def test__extract_error(self):
class RubbishResponse(object):
def __init__(self, content, headers=None):
Expand Down

0 comments on commit 6e62fad

Please sign in to comment.