LUCENE-3146: IndexReader.setNorms is no op if one of the field instances omits norms

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1130039 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2011-06-01 07:55:55 +00:00
parent 41c0976f9e
commit a890a8f5b0
6 changed files with 29 additions and 15 deletions

View File

@ -236,6 +236,9 @@ Changes in Runtime Behavior
(Mike McCandless, Michael Busch, Simon Willnauer) (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 API Changes
* LUCENE-2302, LUCENE-1458, LUCENE-2111, LUCENE-2514: Terms are no longer * LUCENE-2302, LUCENE-1458, LUCENE-2111, LUCENE-2514: Terms are no longer

View File

@ -86,7 +86,11 @@ public class TestFieldNormModifier extends LuceneTestCase {
public void testMissingField() throws Exception { public void testMissingField() throws Exception {
FieldNormModifier fnm = new FieldNormModifier(store, s); FieldNormModifier fnm = new FieldNormModifier(store, s);
fnm.reSetNorms("nobodyherebutuschickens"); try {
fnm.reSetNorms("nobodyherebutuschickens");
} catch (IllegalStateException e) {
// expected
}
} }
public void testFieldWithNoNorm() throws Exception { public void testFieldWithNoNorm() throws Exception {
@ -101,7 +105,11 @@ public class TestFieldNormModifier extends LuceneTestCase {
r.close(); r.close();
FieldNormModifier fnm = new FieldNormModifier(store, s); FieldNormModifier fnm = new FieldNormModifier(store, s);
fnm.reSetNorms("nonorm"); try {
fnm.reSetNorms("nonorm");
} catch (IllegalStateException e) {
// expected
}
// nothing should have changed // nothing should have changed
r = IndexReader.open(store, false); r = IndexReader.open(store, false);

View File

@ -92,12 +92,12 @@ public class TestLengthNormModifier extends LuceneTestCase {
super.tearDown(); super.tearDown();
} }
public void testMissingField() { public void testMissingField() throws Exception {
FieldNormModifier fnm = new FieldNormModifier(store, s); FieldNormModifier fnm = new FieldNormModifier(store, s);
try { try {
fnm.reSetNorms("nobodyherebutuschickens"); fnm.reSetNorms("nobodyherebutuschickens");
} catch (Exception e) { } catch (IllegalStateException e) {
assertNull("caught something", e); // expected
} }
} }
@ -115,8 +115,8 @@ public class TestLengthNormModifier extends LuceneTestCase {
FieldNormModifier fnm = new FieldNormModifier(store, s); FieldNormModifier fnm = new FieldNormModifier(store, s);
try { try {
fnm.reSetNorms("nonorm"); fnm.reSetNorms("nonorm");
} catch (Exception e) { } catch (IllegalStateException e) {
assertNull("caught something", e); // expected
} }
// nothing should have changed // nothing should have changed

View File

@ -96,7 +96,7 @@ public final class Field extends AbstractField implements Fieldable {
}, },
/** Expert: Index the field's value without an Analyzer, /** 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 * can also separately enable/disable norms by calling
* {@link Field#setOmitNorms}. No norms means that * {@link Field#setOmitNorms}. No norms means that
* index-time field and document boosting and field * 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 * less memory usage as norms take up one byte of RAM
* per indexed field for every document in the index, * per indexed field for every document in the index,
* during searching. Note that once you index a given * 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 * 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 * that field must be indexed with NOT_ANALYZED_NO_NORMS
* from the beginning. */ * at some point. */
NOT_ANALYZED_NO_NORMS { NOT_ANALYZED_NO_NORMS {
@Override @Override
public boolean isIndexed() { return true; } public boolean isIndexed() { return true; }

View File

@ -1025,8 +1025,8 @@ public abstract class IndexReader implements Cloneable,Closeable {
* length normalization}. Thus, to preserve the length normalization * length normalization}. Thus, to preserve the length normalization
* values when resetting this, one should base the new value upon the old. * values when resetting this, one should base the new value upon the old.
* *
* <b>NOTE:</b> If this field does not store norms, then * <b>NOTE:</b> If this field does not index norms, then
* this method call will silently do nothing. * this method throws {@link IllegalStateException}.
* *
* @see #norms(String) * @see #norms(String)
* @see Similarity#decodeNormValue(byte) * @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 * has this index open (<code>write.lock</code> could not
* be obtained) * be obtained)
* @throws IOException if there is a low-level IO error * @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) public synchronized void setNorm(int doc, String field, byte value)
throws StaleReaderException, CorruptIndexException, LockObtainFailedException, IOException { throws StaleReaderException, CorruptIndexException, LockObtainFailedException, IOException {

View File

@ -566,8 +566,10 @@ public class SegmentReader extends IndexReader implements Cloneable {
protected void doSetNorm(int doc, String field, byte value) protected void doSetNorm(int doc, String field, byte value)
throws IOException { throws IOException {
SegmentNorms norm = norms.get(field); SegmentNorms norm = norms.get(field);
if (norm == null) // not an indexed field if (norm == null) {
return; // field does not store norms
throw new IllegalStateException("Cannot setNorm for field " + field + ": norms were omitted");
}
normsDirty = true; normsDirty = true;
norm.copyOnWrite()[doc] = value; // set the value norm.copyOnWrite()[doc] = value; // set the value