mirror of https://github.com/apache/lucene.git
removeField and removeFields added to Document
enhancement was proposed in bug 28462 git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150298 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
420d1d7c07
commit
6a9665f792
|
@ -17,6 +17,7 @@ package org.apache.lucene.document;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
@ -81,6 +82,36 @@ public final class Document implements java.io.Serializable {
|
||||||
fields.add(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
|
/** 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 the
|
* null. If multiple fields exists with this name, this method returns the
|
||||||
* first value added.
|
* first value added.
|
||||||
|
|
|
@ -40,6 +40,38 @@ import java.io.IOException;
|
||||||
*/
|
*/
|
||||||
public class TestDocument extends TestCase
|
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
|
* Tests {@link Document#getValues()} method for a brand new Document
|
||||||
* that has not been indexed yet.
|
* that has not been indexed yet.
|
||||||
|
|
Loading…
Reference in New Issue