Apache Lucene open-source search software
LUCENE-3146: IndexReader.setNorms is no op if one of the field instances omits norms
Shai Ereraa890a8f5b015
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a7818829059..b8295f9b845 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -236,6 +236,9 @@ Changes in Runtime Behavior(Mike McCandless, Michael Busch, Simon Willnauer)+* LUCENE-3146: IndexReader.setNorm throws IllegalStateException if the field+ does not store norms. (Shai Erera, Mike McCandless)+API Changes* LUCENE-2302, LUCENE-1458, LUCENE-2111, LUCENE-2514: Terms are no longerdiff --git a/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java b/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.javaindex a3ad664e05e..9af69bba012 100644--- a/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java+++ b/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java@@ -86,7 +86,11 @@ public class TestFieldNormModifier extends LuceneTestCase {public void testMissingField() throws Exception {FieldNormModifier fnm = new FieldNormModifier(store, s);- fnm.reSetNorms("nobodyherebutuschickens");+ try {+ fnm.reSetNorms("nobodyherebutuschickens");+ } catch (IllegalStateException e) {+ // expected+ }}public void testFieldWithNoNorm() throws Exception {@@ -101,7 +105,11 @@ public class TestFieldNormModifier extends LuceneTestCase {r.close();FieldNormModifier fnm = new FieldNormModifier(store, s);- fnm.reSetNorms("nonorm");+ try {+ fnm.reSetNorms("nonorm");+ } catch (IllegalStateException e) {+ // expected+ }// nothing should have changedr = IndexReader.open(store, false);diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java b/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.javaindex ad290c7e490..0313e83f6ae 100644--- a/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java+++ b/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java@@ -92,12 +92,12 @@ public class TestLengthNormModifier extends LuceneTestCase {super.tearDown();}- public void testMissingField() {+ public void testMissingField() throws Exception {FieldNormModifier fnm = new FieldNormModifier(store, s);try {fnm.reSetNorms("nobodyherebutuschickens");- } catch (Exception e) {- assertNull("caught something", e);+ } catch (IllegalStateException e) {+ // expected}}@@ -115,8 +115,8 @@ public class TestLengthNormModifier extends LuceneTestCase {FieldNormModifier fnm = new FieldNormModifier(store, s);try {fnm.reSetNorms("nonorm");- } catch (Exception e) {- assertNull("caught something", e);+ } catch (IllegalStateException e) {+ // expected}// nothing should have changeddiff --git a/lucene/src/java/org/apache/lucene/document/Field.java b/lucene/src/java/org/apache/lucene/document/Field.javaindex 6c6bccb79f1..d83b81cf55f 100644--- a/lucene/src/java/org/apache/lucene/document/Field.java+++ b/lucene/src/java/org/apache/lucene/document/Field.java@@ -96,7 +96,7 @@ public final class Field extends AbstractField implements Fieldable {},/** Expert: Index the field's value without an Analyzer,- * and also disable the storing of norms. Note that you+ * and also disable the indexing of norms. Note that you* can also separately enable/disable norms by calling* {@link Field#setOmitNorms}. No norms means that* index-time field and document boosting and field@@ -104,11 +104,11 @@ public final class Field extends AbstractField implements Fieldable {* less memory usage as norms take up one byte of RAM* per indexed field for every document in the index,* during searching. Note that once you index a given- * field <i>with</i> norms enabled, disabling norms will+ * field <i>with</i> norms disabled, enabling norms will* have no effect. In other words, for this to have the- * above described effect on a field, all instances of+ * above described effect on a field, one instance of* that field must be indexed with NOT_ANALYZED_NO_NORMS- * from the beginning. */+ * at some point. */NOT_ANALYZED_NO_NORMS {@Overridepublic boolean isIndexed() { return true; }diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.javaindex b0f87f1913b..750cd75565a 100644--- a/lucene/src/java/org/apache/lucene/index/IndexReader.java+++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java@@ -1025,8 +1025,8 @@ public abstract class IndexReader implements Cloneable,Closeable {* length normalization}. Thus, to preserve the length normalization* values when resetting this, one should base the new value upon the old.*- * <b>NOTE:</b> If this field does not store norms, then- * this method call will silently do nothing.+ * <b>NOTE:</b> If this field does not index norms, then+ * this method throws {@link IllegalStateException}.** @see #norms(String)* @see Similarity#decodeNormValue(byte)@@ -1037,6 +1037,7 @@ public abstract class IndexReader implements Cloneable,Closeable {* has this index open (<code>write.lock</code> could not* be obtained)* @throws IOException if there is a low-level IO error+ * @throws IllegalStateException if the field does not index norms*/public synchronized void setNorm(int doc, String field, byte value)throws StaleReaderException, CorruptIndexException, LockObtainFailedException, IOException {diff --git a/lucene/src/java/org/apache/lucene/index/SegmentReader.java b/lucene/src/java/org/apache/lucene/index/SegmentReader.javaindex 0e674311384..21bbcc1d15c 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentReader.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentReader.java@@ -566,8 +566,10 @@ public class SegmentReader extends IndexReader implements Cloneable {protected void doSetNorm(int doc, String field, byte value)throws IOException {SegmentNorms norm = norms.get(field);- if (norm == null) // not an indexed field- return;+ if (norm == null) {+ // field does not store norms+ throw new IllegalStateException("Cannot setNorm for field " + field + ": norms were omitted");+ }normsDirty = true;norm.copyOnWrite()[doc] = value; // set the value