diff --git a/CHANGES.txt b/CHANGES.txt index c0e132d09a5..ad8a47c3fe9 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 diff --git a/src/java/org/apache/lucene/document/Field.java b/src/java/org/apache/lucene/document/Field.java index 5a1b4c3e581..16d932633ab 100644 --- a/src/java/org/apache/lucene/document/Field.java +++ b/src/java/org/apache/lucene/document/Field.java @@ -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. diff --git a/src/test/org/apache/lucene/document/TestDocument.java b/src/test/org/apache/lucene/document/TestDocument.java index 6b8b77bf66d..a28b1da28ef 100644 --- a/src/test/org/apache/lucene/document/TestDocument.java +++ b/src/test/org/apache/lucene/document/TestDocument.java @@ -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); + } }