Skip to content

Commit 1ab9351

Browse files
eukaryoteCalvin Smith
andauthored
fix commitWithin for JSON updates w/o boost or fieldUpdates (#421)
- 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]>
1 parent 30a2c01 commit 1ab9351

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

pysolr.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ def _update(
502502
clean_ctrl_chars=True,
503503
commit=None,
504504
softCommit=False,
505+
commitWithin=None,
505506
waitFlush=None,
506507
waitSearcher=None,
507508
overwrite=None,
@@ -540,6 +541,8 @@ def _update(
540541
query_vars.append("commit=%s" % str(bool(commit)).lower())
541542
elif softCommit:
542543
query_vars.append("softCommit=%s" % str(bool(softCommit)).lower())
544+
elif commitWithin is not None:
545+
query_vars.append("commitWithin=%s" % str(int(commitWithin)))
543546

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

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

937-
if commitWithin:
938-
message.set("commitWithin", commitWithin)
939-
940940
for doc in docs:
941941
el = self._build_xml_doc(doc, boost=boost, fieldUpdates=fieldUpdates)
942942
message.append(el)
@@ -1066,7 +1066,9 @@ def add(
10661066
start_time = time.time()
10671067
self.log.debug("Starting to build add request...")
10681068
solrapi, m, len_message = self._build_docs(
1069-
docs, boost, fieldUpdates, commitWithin
1069+
docs,
1070+
boost,
1071+
fieldUpdates,
10701072
)
10711073
end_time = time.time()
10721074
self.log.debug(
@@ -1078,6 +1080,7 @@ def add(
10781080
m,
10791081
commit=commit,
10801082
softCommit=softCommit,
1083+
commitWithin=commitWithin,
10811084
waitFlush=waitFlush,
10821085
waitSearcher=waitSearcher,
10831086
overwrite=overwrite,

tests/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datetime
66
import random
7+
import time
78
import unittest
89
from io import StringIO
910
from xml.etree import ElementTree
@@ -823,6 +824,22 @@ def test_add_with_boost(self):
823824
self.assertEqual(len(res), 5)
824825
self.assertEqual("doc_6", res.docs[0]["id"])
825826

827+
def test_add_with_commit_within(self):
828+
self.assertEqual(len(self.solr.search("commitWithin")), 0)
829+
830+
commit_within_ms = 50
831+
self.solr.add(
832+
[
833+
{"id": "doc_6", "title": "commitWithin test"},
834+
],
835+
commitWithin=commit_within_ms,
836+
)
837+
# we should not see the doc immediately
838+
self.assertEqual(len(self.solr.search("commitWithin")), 0)
839+
# but we should see it after commitWithin period (+ small grace period)
840+
time.sleep((commit_within_ms / 1000.0) + 0.01)
841+
self.assertEqual(len(self.solr.search("commitWithin")), 1)
842+
826843
def test_field_update_inc(self):
827844
originalDocs = self.solr.search("doc")
828845
self.assertEqual(len(originalDocs), 3)

0 commit comments

Comments
 (0)