diff --git a/CHANGES.txt b/CHANGES.txt index fb982034747..43889c5eae0 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -21,6 +21,10 @@ API Changes and is supposed to be replaced with the WordlistLoader class in package org.apache.lucene.analysis (Daniel Naber) +10. LUCENE-609: Revert return type of Document.getField(s) to Field + for backward compatibility, added new Document.getFieldable(s) + for access to new lazy loaded fields. (Yonik Seeley) + Bug fixes 1. Fixed the web application demo (built with "ant war-demo") which @@ -51,7 +55,7 @@ Bug fixes 9. LUCENE-415: A previously unclean shutdown during indexing can cause a non-empty segment file to be re-used, causing index corruption. (Andy Hind via Yonik Seeley) - + Optimizations 1. LUCENE-586: TermDocs.skipTo() is now more efficient for multi-segment diff --git a/src/java/org/apache/lucene/document/Document.java b/src/java/org/apache/lucene/document/Document.java index a19ca9fa48d..f47a75104af 100644 --- a/src/java/org/apache/lucene/document/Document.java +++ b/src/java/org/apache/lucene/document/Document.java @@ -129,16 +129,31 @@ public final class Document implements java.io.Serializable { /** 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 * first value added. + * Do not use this method with lazy loaded fields. */ - public final Fieldable getField(String name) { + public final Field getField(String name) { for (int i = 0; i < fields.size(); i++) { - Fieldable field = (Fieldable)fields.get(i); + Field field = (Field)fields.get(i); if (field.name().equals(name)) - return field; + return field; } return null; } + + /** 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 + * first value added. + */ + public Fieldable getFieldable(String name) { + for (int i = 0; i < fields.size(); i++) { + Fieldable field = (Fieldable)fields.get(i); + if (field.name().equals(name)) + return field; + } + return null; + } + /** Returns the string value of the field with the given name if any exist in * this document, or null. If multiple fields exist with this name, this * method returns the first value added. If only binary fields with this name @@ -158,6 +173,30 @@ public final class Document implements java.io.Serializable { return ((Vector)fields).elements(); } + /** + * Returns an array of {@link Field}s with the given name. + * This method can return null. + * Do not use with lazy loaded fields. + * + * @param name the name of the field + * @return a Field[] array + */ + public final Field[] getFields(String name) { + List result = new ArrayList(); + for (int i = 0; i < fields.size(); i++) { + Field field = (Field)fields.get(i); + if (field.name().equals(name)) { + result.add(field); + } + } + + if (result.size() == 0) + return null; + + return (Field[])result.toArray(new Field[result.size()]); + } + + /** * Returns an array of {@link Fieldable}s with the given name. * This method can return null. @@ -165,7 +204,7 @@ public final class Document implements java.io.Serializable { * @param name the name of the field * @return a Fieldable[] array */ - public final Fieldable[] getFields(String name) { + public Fieldable[] getFieldables(String name) { List result = new ArrayList(); for (int i = 0; i < fields.size(); i++) { Fieldable field = (Fieldable)fields.get(i); @@ -180,6 +219,7 @@ public final class Document implements java.io.Serializable { return (Fieldable[])result.toArray(new Fieldable[result.size()]); } + /** * Returns an array of values of the field specified as the method parameter. * This method can return null. diff --git a/src/test/org/apache/lucene/index/TestFieldsReader.java b/src/test/org/apache/lucene/index/TestFieldsReader.java index e53619cd425..720e47aedf1 100644 --- a/src/test/org/apache/lucene/index/TestFieldsReader.java +++ b/src/test/org/apache/lucene/index/TestFieldsReader.java @@ -97,26 +97,26 @@ public class TestFieldsReader extends TestCase { SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(loadFieldNames, lazyFieldNames); Document doc = reader.doc(0, fieldSelector); assertTrue("doc is null and it shouldn't be", doc != null); - Fieldable field = doc.getField(DocHelper.LAZY_FIELD_KEY); + Fieldable field = doc.getFieldable(DocHelper.LAZY_FIELD_KEY); assertTrue("field is null and it shouldn't be", field != null); assertTrue("field is not lazy and it should be", field.isLazy()); String value = field.stringValue(); assertTrue("value is null and it shouldn't be", value != null); assertTrue(value + " is not equal to " + DocHelper.LAZY_FIELD_TEXT, value.equals(DocHelper.LAZY_FIELD_TEXT) == true); - field = doc.getField(DocHelper.TEXT_FIELD_1_KEY); + field = doc.getFieldable(DocHelper.TEXT_FIELD_1_KEY); assertTrue("field is null and it shouldn't be", field != null); assertTrue("Field is lazy and it should not be", field.isLazy() == false); - field = doc.getField(DocHelper.TEXT_FIELD_UTF1_KEY); + field = doc.getFieldable(DocHelper.TEXT_FIELD_UTF1_KEY); assertTrue("field is null and it shouldn't be", field != null); assertTrue("Field is lazy and it should not be", field.isLazy() == false); assertTrue(field.stringValue() + " is not equal to " + DocHelper.FIELD_UTF1_TEXT, field.stringValue().equals(DocHelper.FIELD_UTF1_TEXT) == true); - field = doc.getField(DocHelper.TEXT_FIELD_UTF2_KEY); + field = doc.getFieldable(DocHelper.TEXT_FIELD_UTF2_KEY); assertTrue("field is null and it shouldn't be", field != null); assertTrue("Field is lazy and it should not be", field.isLazy() == true); assertTrue(field.stringValue() + " is not equal to " + DocHelper.FIELD_UTF2_TEXT, field.stringValue().equals(DocHelper.FIELD_UTF2_TEXT) == true); - field = doc.getField(DocHelper.LAZY_FIELD_BINARY_KEY); + field = doc.getFieldable(DocHelper.LAZY_FIELD_BINARY_KEY); assertTrue("field is null and it shouldn't be", field != null); byte [] bytes = field.binaryValue(); assertTrue("bytes is null and it shouldn't be", bytes != null); @@ -182,7 +182,7 @@ public class TestFieldsReader extends TestCase { Document doc; doc = reader.doc(0, null);//Load all of them assertTrue("doc is null and it shouldn't be", doc != null); - Fieldable field = doc.getField(DocHelper.LARGE_LAZY_FIELD_KEY); + Fieldable field = doc.getFieldable(DocHelper.LARGE_LAZY_FIELD_KEY); assertTrue("field is lazy", field.isLazy() == false); String value; long start; @@ -201,7 +201,7 @@ public class TestFieldsReader extends TestCase { System.gc(); reader = new FieldsReader(tmpDir, "test", fieldInfos); doc = reader.doc(0, fieldSelector); - field = doc.getField(DocHelper.LARGE_LAZY_FIELD_KEY); + field = doc.getFieldable(DocHelper.LARGE_LAZY_FIELD_KEY); assertTrue("field is not lazy", field.isLazy() == true); start = System.currentTimeMillis(); //On my machine this took around 50 - 70ms