From 50bdefc55a30bb4d36e5ea2157738c0426effa99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Re=C3=A9?= Date: Fri, 26 Jan 2024 13:20:02 +0100 Subject: [PATCH] Fix indexing of null values --- CHANGES.rst | 2 +- src/collective/solr/solr.py | 12 ++++- .../solr/tests/data/add_request_with_none.txt | 7 +++ .../add_request_with_none_and_boost_value.txt | 7 +++ src/collective/solr/tests/test_solr.py | 53 ++++++++++++++++++- 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/collective/solr/tests/data/add_request_with_none.txt create mode 100644 src/collective/solr/tests/data/add_request_with_none_and_boost_value.txt diff --git a/CHANGES.rst b/CHANGES.rst index 25b130ad..ae5abbd0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,7 @@ Changelog 9.2.1 (unreleased) ------------------ -- Nothing changed yet. +- Fix indexing of null values [reebalazs] 9.2.0 (2024-01-31) diff --git a/src/collective/solr/solr.py b/src/collective/solr/solr.py index 3d9aaaf4..055a6e4e 100644 --- a/src/collective/solr/solr.py +++ b/src/collective/solr/solr.py @@ -284,7 +284,6 @@ def add(self, boost_values=None, atomic_updates=True, **fields): lst.append("") for f, v in fields.items(): - # Add update="set" attribute to each field except for the uniqueKey # field. if f == uniqueKey: @@ -316,7 +315,16 @@ def add(self, boost_values=None, atomic_updates=True, **fields): tmpl = '' lst.append(tmpl % (self.escapeKey(f))) else: - lst.append(tmpl % self.escapeVal(v)) + if v is None: + if f in boost_values: + tmpl = '' + tmpl = tmpl % (self.escapeKey(f), boost_values[f]) + else: + tmpl = '' + tmpl = tmpl % (self.escapeKey(f)) + lst.append(tmpl) + else: + lst.append(tmpl % self.escapeVal(v)) lst.append("") lst.append("") xstr = "".join(lst) diff --git a/src/collective/solr/tests/data/add_request_with_none.txt b/src/collective/solr/tests/data/add_request_with_none.txt new file mode 100644 index 00000000..0e1380d1 --- /dev/null +++ b/src/collective/solr/tests/data/add_request_with_none.txt @@ -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 + +500 diff --git a/src/collective/solr/tests/data/add_request_with_none_and_boost_value.txt b/src/collective/solr/tests/data/add_request_with_none_and_boost_value.txt new file mode 100644 index 00000000..6ed17d9e --- /dev/null +++ b/src/collective/solr/tests/data/add_request_with_none_and_boost_value.txt @@ -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 + +500 diff --git a/src/collective/solr/tests/test_solr.py b/src/collective/solr/tests/test_solr.py index 5f866acf..a42273e4 100644 --- a/src/collective/solr/tests/test_solr.py +++ b/src/collective/solr/tests/test_solr.py @@ -8,7 +8,6 @@ class TestSolr(TestCase): - layer = COLLECTIVE_SOLR_MOCK_REGISTRY_FIXTURE def test_add(self): @@ -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(