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

Add accepts empty values #353

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
16 changes: 12 additions & 4 deletions pysolr.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,18 +940,26 @@ def _build_xml_doc(self, doc, boost=None, fieldUpdates=None):
else:
values = (value,)

use_field_updates = fieldUpdates and key in fieldUpdates
if use_field_updates and not values:
values = ("",)
for bit in values:

attrs = {"name": key}

if self._is_null_value(bit):
continue
if use_field_updates:
bit = ""
attrs["null"] = "true"
else:
continue

if key == "_doc":
child = self._build_xml_doc(bit, boost)
doc_elem.append(child)
continue

attrs = {"name": key}

if fieldUpdates and key in fieldUpdates:
if use_field_updates:
attrs["update"] = fieldUpdates[key]

if boost and key in boost:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,45 @@ def test__build_xml_doc_with_sub_docs(self):
self.assertEqual(children_docs[0].find("*[@name='id']").text, sub_docs[0]["id"])
self.assertEqual(children_docs[1].find("*[@name='id']").text, sub_docs[1]["id"])

def test__build_xml_doc_with_empty_values(self):
doc = {
"id": "doc_1",
"title": "",
"price": None,
"tags": [],
}
doc_xml = force_unicode(
ElementTree.tostring(self.solr._build_xml_doc(doc), encoding="utf-8")
)
self.assertNotIn('<field name="title" />', doc_xml)
self.assertNotIn('<field name="price" />', doc_xml)
self.assertNotIn('<field name="tags" />', doc_xml)
self.assertIn('<field name="id">doc_1</field>', doc_xml)
self.assertEqual(len(doc_xml), 41)

def test__build_xml_doc_with_empty_values_and_field_updates(self):
doc = {
"id": "doc_1",
"title": "",
"price": None,
"tags": [],
}
fieldUpdates = {
"title": "set",
"tags": "set",
}
doc_xml = force_unicode(
ElementTree.tostring(
self.solr._build_xml_doc(doc, fieldUpdates=fieldUpdates),
encoding="utf-8",
)
)
self.assertIn('<field name="title" null="true" update="set" />', doc_xml)
self.assertNotIn('<field name="price" />', doc_xml)
self.assertIn('<field name="tags" null="true" update="set" />', doc_xml)
self.assertIn('<field name="id">doc_1</field>', doc_xml)
self.assertEqual(len(doc_xml), 134)

def test_build_json_doc_matches_xml(self):
doc = {
"id": "doc_1",
Expand Down