Apache Lucene open-source search software
removeField and removeFields added to Document enhancement was proposed in bug 28462
Christoph Goller6a9665f792b3
diff --git a/src/java/org/apache/lucene/document/Document.java b/src/java/org/apache/lucene/document/Document.javaindex e7a7ed76666..fb500341795 100644--- a/src/java/org/apache/lucene/document/Document.java+++ b/src/java/org/apache/lucene/document/Document.java@@ -17,6 +17,7 @@ package org.apache.lucene.document;*/import java.util.Enumeration;+import java.util.Iterator;import java.util.List;import java.util.ArrayList;import java.util.Vector;@@ -80,6 +81,36 @@ public final class Document implements java.io.Serializable {public final void add(Field field) {fields.add(field);}++ /**+ * Removes field with the given name from the document.+ * If multiple fields exist with this name, this method returns the first value added.+ * If there is no field with the specified name, the document remains unchanged.+ */+ public final void removeField(String name) {+ Iterator it = fields.iterator();+ while (it.hasNext()) {+ Field field = (Field)it.next();+ if (field.name().equals(name)) {+ it.remove();+ return;+ }+ }+ }++ /**+ * Removes all fields with the given name from the document.+ * If there is no field with the specified name, the document remains unchanged.+ */+ public final void removeFields(String name) {+ Iterator it = fields.iterator();+ while (it.hasNext()) {+ Field field = (Field)it.next();+ if (field.name().equals(name)) {+ it.remove();+ }+ }+ }/** Returns a field with the given name if any exist in this document, or* null. If multiple fields exists with this name, this method returns thediff --git a/src/test/org/apache/lucene/document/TestDocument.java b/src/test/org/apache/lucene/document/TestDocument.javaindex 7cc2e7c4a8d..09f21936c53 100644--- a/src/test/org/apache/lucene/document/TestDocument.java+++ b/src/test/org/apache/lucene/document/TestDocument.java@@ -40,6 +40,38 @@ import java.io.IOException;*/public class TestDocument extends TestCase{++ /**+ * Tests {@link Document#remove()} method for a brand new Document+ * that has not been indexed yet.+ *+ * @throws Exception on error+ */+ public void testRemoveForNewDocument() throws Exception+ {+ Document doc = makeDocumentWithFields();+ assertEquals(8, doc.fields.size());+ doc.removeFields("keyword");+ assertEquals(6, doc.fields.size());+ doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored+ doc.removeFields("keyword"); // removing a field more than once+ assertEquals(6, doc.fields.size());+ doc.removeField("text");+ assertEquals(5, doc.fields.size());+ doc.removeField("text");+ assertEquals(4, doc.fields.size());+ doc.removeField("text");+ assertEquals(4, doc.fields.size());+ doc.removeField("doesnotexists"); // removing non-existing fields is siltenlty ignored+ assertEquals(4, doc.fields.size());+ doc.removeFields("unindexed");+ assertEquals(2, doc.fields.size());+ doc.removeFields("unstored");+ assertEquals(0, doc.fields.size());+ doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored+ assertEquals(0, doc.fields.size());+ }+/*** Tests {@link Document#getValues()} method for a brand new Document* that has not been indexed yet.