Skip to content

Commit

Permalink
fix commitWithin for JSON updates w/o boost or fieldUpdates (#421)
Browse files Browse the repository at this point in the history
- 3.9.0 included changes such that the `commitWithin` arg
   to the `add` method was no longer used unless either
   `boost` or `fieldValues` was provided, since the new logic
   to support sending JSON docs doesn't use `commitWithin`
 - these changes add `commit_within` to the `Solr._update`
   method and modify that method to include `commitWithin`
   as a query parameter if it is provided, which is simpler
   than needing different logic for the two cases of XML
   and JSON docs
 - also added a test case for `commitWithin`, which wasn't
   tested previously

Co-authored-by: Calvin Smith <[email protected]>
  • Loading branch information
eukaryote and Calvin Smith authored Sep 4, 2023
1 parent 30a2c01 commit 1ab9351
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pysolr.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def _update(
clean_ctrl_chars=True,
commit=None,
softCommit=False,
commitWithin=None,
waitFlush=None,
waitSearcher=None,
overwrite=None,
Expand Down Expand Up @@ -540,6 +541,8 @@ def _update(
query_vars.append("commit=%s" % str(bool(commit)).lower())
elif softCommit:
query_vars.append("softCommit=%s" % str(bool(softCommit)).lower())
elif commitWithin is not None:
query_vars.append("commitWithin=%s" % str(int(commitWithin)))

if waitFlush is not None:
query_vars.append("waitFlush=%s" % str(bool(waitFlush)).lower())
Expand Down Expand Up @@ -909,7 +912,7 @@ def suggest_terms(self, fields, prefix, handler="terms", **kwargs):
)
return res

def _build_docs(self, docs, boost=None, fieldUpdates=None, commitWithin=None):
def _build_docs(self, docs, boost=None, fieldUpdates=None):
# if no boost needed use json multidocument api
# The JSON API skips the XML conversion and speedup load from 15 to 20 times.
# CPU Usage is drastically lower.
Expand All @@ -934,9 +937,6 @@ def _build_docs(self, docs, boost=None, fieldUpdates=None, commitWithin=None):
solrapi = "XML"
message = ElementTree.Element("add")

if commitWithin:
message.set("commitWithin", commitWithin)

for doc in docs:
el = self._build_xml_doc(doc, boost=boost, fieldUpdates=fieldUpdates)
message.append(el)
Expand Down Expand Up @@ -1066,7 +1066,9 @@ def add(
start_time = time.time()
self.log.debug("Starting to build add request...")
solrapi, m, len_message = self._build_docs(
docs, boost, fieldUpdates, commitWithin
docs,
boost,
fieldUpdates,
)
end_time = time.time()
self.log.debug(
Expand All @@ -1078,6 +1080,7 @@ def add(
m,
commit=commit,
softCommit=softCommit,
commitWithin=commitWithin,
waitFlush=waitFlush,
waitSearcher=waitSearcher,
overwrite=overwrite,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datetime
import random
import time
import unittest
from io import StringIO
from xml.etree import ElementTree
Expand Down Expand Up @@ -823,6 +824,22 @@ def test_add_with_boost(self):
self.assertEqual(len(res), 5)
self.assertEqual("doc_6", res.docs[0]["id"])

def test_add_with_commit_within(self):
self.assertEqual(len(self.solr.search("commitWithin")), 0)

commit_within_ms = 50
self.solr.add(
[
{"id": "doc_6", "title": "commitWithin test"},
],
commitWithin=commit_within_ms,
)
# we should not see the doc immediately
self.assertEqual(len(self.solr.search("commitWithin")), 0)
# but we should see it after commitWithin period (+ small grace period)
time.sleep((commit_within_ms / 1000.0) + 0.01)
self.assertEqual(len(self.solr.search("commitWithin")), 1)

def test_field_update_inc(self):
originalDocs = self.solr.search("doc")
self.assertEqual(len(originalDocs), 3)
Expand Down

0 comments on commit 1ab9351

Please sign in to comment.