lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gol...@apache.org
Subject cvs commit: jakarta-lucene/src/java/org/apache/lucene/document Document.java
Date Tue, 20 Apr 2004 17:26:16 GMT
goller      2004/04/20 10:26:16

  Modified:    src/test/org/apache/lucene/document TestDocument.java
               src/java/org/apache/lucene/document Document.java
  Log:
  removeField and removeFields added to Document
  enhancement was proposed in bug 28462
  
  Revision  Changes    Path
  1.4       +33 -1     jakarta-lucene/src/test/org/apache/lucene/document/TestDocument.java
  
  Index: TestDocument.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/document/TestDocument.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestDocument.java	29 Mar 2004 22:48:06 -0000	1.3
  +++ TestDocument.java	20 Apr 2004 17:26:16 -0000	1.4
  @@ -40,6 +40,38 @@
    */
   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.
  
  
  
  1.17      +31 -0     jakarta-lucene/src/java/org/apache/lucene/document/Document.java
  
  Index: Document.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/document/Document.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Document.java	29 Mar 2004 22:48:01 -0000	1.16
  +++ Document.java	20 Apr 2004 17:26:16 -0000	1.17
  @@ -17,6 +17,7 @@
    */
   
   import java.util.Enumeration;
  +import java.util.Iterator;
   import java.util.List;
   import java.util.ArrayList;
   import java.util.Vector;
  @@ -79,6 +80,36 @@
      * treated as though appended for the purposes of search. */
     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
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message