Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix commitWithin for JSON updates w/o boost #421

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading