LUCENE-963: add setters to Field to allow re-using Field instances during indexing (for better performance)

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@557445 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-07-19 01:33:59 +00:00
parent 4293019522
commit 66bacd0b13
3 changed files with 74 additions and 0 deletions

View File

@ -65,6 +65,10 @@ Optimizations
4. LUCENE-959: Remove synchronization in Document (yonik)
5. LUCENE-963: Add setters to Field to allow for re-using a single
Field instance during indexing. This is a sizable performance
gain, especially for small documents. (Mike McCandless)
Documentation
Build

View File

@ -155,6 +155,35 @@ public final class Field extends AbstractField implements Fieldable, Serializabl
* readerValue(), binaryValue(), and tokenStreamValue() must be set. */
public TokenStream tokenStreamValue() { return fieldsData instanceof TokenStream ? (TokenStream)fieldsData : null; }
/** Expert: change the value of this field. This can be
* used during indexing to re-use a single Field instance
* to improve indexing speed. */
public void setValue(String value) {
fieldsData = value;
}
/** Expert: change the value of this field. This can be
* used during indexing to re-use a single Field instance
* to improve indexing speed. */
public void setValue(Reader value) {
fieldsData = value;
}
/** Expert: change the value of this field. This can be
* used during indexing to re-use a single Field instance
* to improve indexing speed. */
public void setValue(byte[] value) {
fieldsData = value;
}
/** Expert: change the value of this field. This can be
* used during indexing to re-use a single Field instance
* to improve indexing speed. */
public void setValue(TokenStream value) {
fieldsData = value;
}
/**
* Create a field by specifying its name, value and how it will
* be saved in the index. Term vectors will not be stored in the index.

View File

@ -222,4 +222,45 @@ public class TestDocument extends TestCase
assertTrue(unstoredFieldValues[1].equals("test2"));
}
}
public void testFieldSetValue() throws Exception {
Field field = new Field("id", "id1", Field.Store.YES, Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(field);
doc.add(new Field("keyword", "test", Field.Store.YES, Field.Index.UN_TOKENIZED));
RAMDirectory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
writer.addDocument(doc);
field.setValue("id2");
writer.addDocument(doc);
field.setValue("id3");
writer.addDocument(doc);
writer.close();
Searcher searcher = new IndexSearcher(dir);
Query query = new TermQuery(new Term("keyword", "test"));
// ensure that queries return expected results without DateFilter first
Hits hits = searcher.search(query);
assertEquals(3, hits.length());
int result = 0;
for(int i=0;i<3;i++) {
Document doc2 = hits.doc(i);
Field f = doc2.getField("id");
if (f.stringValue().equals("id1"))
result |= 1;
else if (f.stringValue().equals("id2"))
result |= 2;
else if (f.stringValue().equals("id3"))
result |= 4;
else
fail("unexpected id field");
}
searcher.close();
dir.close();
assertEquals("did not see all IDs", 7, result);
}
}