Apache Lucene open-source search software
LUCENE-8024: Remove unnecessary norms.advanceExact check in score()
Robert Muir42c49c25d6b7
diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.javaindex 1485101f657..e56932bcb81 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java@@ -230,11 +230,9 @@ public class BM25Similarity extends Similarity {if (norms == null) {norm = k1;} else {- if (norms.advanceExact(doc)) {- norm = cache[((byte) norms.longValue()) & 0xFF];- } else {- norm = cache[0];- }+ boolean found = norms.advanceExact(doc);+ assert found;+ norm = cache[((byte) norms.longValue()) & 0xFF];}return weightValue * (float) (freq / (freq + norm));}@@ -259,12 +257,9 @@ public class BM25Similarity extends Similarity {(float) (freq.getValue() / (freq.getValue() + (double) k1)),"tf, computed as freq / (freq + k1) from:", subs);} else {- byte norm;- if (norms.advanceExact(doc)) {- norm = (byte) norms.longValue();- } else {- norm = 0;- }+ boolean found = norms.advanceExact(doc);+ assert found;+ byte norm = (byte) norms.longValue();float doclen = lengthCache[norm & 0xff];subs.add(Explanation.match(b, "b, length normalization parameter"));if ((norm & 0xFF) > 39) {diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.java b/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.javaindex d8ec244a6a7..68f8e5d0109 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.java@@ -265,16 +265,13 @@ public abstract class SimilarityBase extends Similarity {if (norms == null) {return 1D;}- if (norms.advanceExact(doc)) {- return LENGTH_TABLE[Byte.toUnsignedInt((byte) norms.longValue())];- } else {- return 0;- }+ boolean found = norms.advanceExact(doc);+ assert found;+ return LENGTH_TABLE[Byte.toUnsignedInt((byte) norms.longValue())];}@Overridepublic float score(int doc, float freq) throws IOException {- // We have to supply something in case norms are omittedreturn (float) SimilarityBase.this.score(stats, freq, getLengthValue(doc));}diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.javaindex 97e522697dd..5765e6bfae3 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.java@@ -583,12 +583,9 @@ public abstract class TFIDFSimilarity extends Similarity {if (norms == null) {return raw;} else {- float normValue;- if (norms.advanceExact(doc)) {- normValue = normTable[(int) (norms.longValue() & 0xFF)];- } else {- normValue = 0;- }+ boolean found = norms.advanceExact(doc);+ assert found;+ float normValue = normTable[(int) (norms.longValue() & 0xFF)];return raw * normValue; // normalize for field}}@@ -629,27 +626,6 @@ public abstract class TFIDFSimilarity extends Similarity {}}- private Explanation explainField(int doc, Explanation freq, IDFStats stats, NumericDocValues norms, float[] normTable) throws IOException {- Explanation tfExplanation = Explanation.match(tf(freq.getValue()), "tf(freq="+freq.getValue()+"), with freq of:", freq);- float norm;- if (norms == null) {- norm = 1f;- } else if (norms.advanceExact(doc) == false) {- norm = 0f;- } else {- norm = normTable[(int) (norms.longValue() & 0xFF)];- }-- Explanation fieldNormExpl = Explanation.match(- norm,- "fieldNorm(doc=" + doc + ")");-- return Explanation.match(- tfExplanation.getValue() * fieldNormExpl.getValue(),- "fieldWeight in " + doc + ", product of:",- tfExplanation, fieldNormExpl);- }-private Explanation explainScore(int doc, Explanation freq, IDFStats stats, NumericDocValues norms, float[] normTable) throws IOException {List<Explanation> subs = new ArrayList<Explanation>();if (stats.boost != 1F) {@@ -662,9 +638,9 @@ public abstract class TFIDFSimilarity extends Similarity {float norm;if (norms == null) {norm = 1f;- } else if (norms.advanceExact(doc) == false) {- norm = 0f;} else {+ boolean found = norms.advanceExact(doc);+ assert found;norm = normTable[(int) (norms.longValue() & 0xFF)];}