diff --git a/pysolr.py b/pysolr.py index 1b852fd7..fad7f185 100644 --- a/pysolr.py +++ b/pysolr.py @@ -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: diff --git a/tests/test_client.py b/tests/test_client.py index 9e96c3a4..da690bd4 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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('', doc_xml) + self.assertNotIn('', doc_xml) + self.assertNotIn('', doc_xml) + self.assertIn('doc_1', 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('', doc_xml) + self.assertNotIn('', doc_xml) + self.assertIn('', doc_xml) + self.assertIn('doc_1', doc_xml) + self.assertEqual(len(doc_xml), 134) + def test_build_json_doc_matches_xml(self): doc = { "id": "doc_1",