Skip to content

Commit

Permalink
Fix indexing of null values
Browse files Browse the repository at this point in the history
  • Loading branch information
reebalazs committed Jan 31, 2024
1 parent e70366c commit 97f1709
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/collective/solr/solr.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ def add(self, boost_values=None, atomic_updates=True, **fields):
lst.append("<doc>")

for f, v in fields.items():

# Add update="set" attribute to each field except for the uniqueKey
# field.
if f == uniqueKey:
Expand Down Expand Up @@ -316,7 +315,16 @@ def add(self, boost_values=None, atomic_updates=True, **fields):
tmpl = '<field name="%s" update="set" null="true"/>'
lst.append(tmpl % (self.escapeKey(f)))
else:
lst.append(tmpl % self.escapeVal(v))
if v is None:
if f in boost_values:
tmpl = '<field name="%s" boost="%s" update="set" null="true"/>'
tmpl = tmpl % (self.escapeKey(f), boost_values[f])
else:
tmpl = '<field name="%s" update="set" null="true"/>'
tmpl = tmpl % (self.escapeKey(f))
lst.append(tmpl)
else:
lst.append(tmpl % self.escapeVal(v))
lst.append("</doc>")
lst.append("</add>")
xstr = "".join(lst)
Expand Down
7 changes: 7 additions & 0 deletions src/collective/solr/tests/data/add_request_with_none.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POST /solr/plone/update HTTP/1.1
Host: localhost
Accept-Encoding: identity
Content-Length: 95
Content-Type: text/xml; charset=utf-8

<add><doc><field name="id">500</field><field name="name" update="set" null="true"/></doc></add>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POST /solr/plone/update HTTP/1.1
Host: localhost
Accept-Encoding: identity
Content-Length: 115
Content-Type: text/xml; charset=utf-8

<add><doc boost="2"><field name="id">500</field><field name="name" boost="5" update="set" null="true"/></doc></add>
53 changes: 52 additions & 1 deletion src/collective/solr/tests/test_solr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class TestSolr(TestCase):

layer = COLLECTIVE_SOLR_MOCK_REGISTRY_FIXTURE

def test_add(self):
Expand Down Expand Up @@ -63,6 +62,58 @@ def test_add_with_boost_values(self):
self.assertEqual(len(res), 1) # one request was sent
self.failUnlessEqual(str(output), add_request.decode("utf-8"))

def test_add_none(self):
config = getConfig()
config.atomic_updates = True
add_request = getData("add_request_with_none.txt").rstrip(b"\n")
add_response = getData("add_response.txt")

c = SolrConnection(host="localhost:8983", persistent=True)

# fake schema response - caches the schema
fakehttp(c, getData("schema.xml"))
c.get_schema()

output = fakehttp(c, add_response)
c.add(id=500, name=None)
res = c.flush()
self.assertEqual(len(res), 1) # one request was sent
res = res[0]
self.failUnlessEqual(str(output), add_request.decode("utf-8"))
# Status
node = res.findall(".//int")[0]
self.failUnlessEqual(node.attrib["name"], "status")
self.failUnlessEqual(node.text, "0")
# QTime
node = res.findall(".//int")[1]
self.failUnlessEqual(node.attrib["name"], "QTime")
self.failUnlessEqual(node.text, "4")
res.find("QTime")

def test_add_none_with_boost_values(self):
config = getConfig()
config.atomic_updates = False
add_request = getData("add_request_with_none_and_boost_value.txt").rstrip(b"\n")
add_response = getData("add_response.txt")
c = SolrConnection(host="localhost:8983", persistent=True)

# fake schema response - caches the schema
fakehttp(c, getData("schema.xml"))
c.get_schema()

output = fakehttp(c, add_response)
boost = {"": 2, "id": 0.5, "name": 5}
c.add(
boost_values=boost,
atomic_updates=False, # Force disabling atomic updates
id="500",
name=None,
)

res = c.flush()
self.assertEqual(len(res), 1) # one request was sent
self.failUnlessEqual(str(output), add_request.decode("utf-8"))

def test_connection_str(self):
c = SolrConnection(host="localhost:8983", persistent=True)
self.assertEqual(
Expand Down

0 comments on commit 97f1709

Please sign in to comment.