LUCENE-8024: Remove unnecessary norms.advanceExact check in score()

This commit is contained in:
Robert Muir 2017-10-31 19:44:39 -04:00
parent 148d81c1e9
commit 42c49c25d6
3 changed files with 14 additions and 46 deletions

View File

@ -230,11 +230,9 @@ public class BM25Similarity extends Similarity {
if (norms == null) { if (norms == null) {
norm = k1; norm = k1;
} else { } else {
if (norms.advanceExact(doc)) { boolean found = norms.advanceExact(doc);
norm = cache[((byte) norms.longValue()) & 0xFF]; assert found;
} else { norm = cache[((byte) norms.longValue()) & 0xFF];
norm = cache[0];
}
} }
return weightValue * (float) (freq / (freq + norm)); return weightValue * (float) (freq / (freq + norm));
} }
@ -259,12 +257,9 @@ public class BM25Similarity extends Similarity {
(float) (freq.getValue() / (freq.getValue() + (double) k1)), (float) (freq.getValue() / (freq.getValue() + (double) k1)),
"tf, computed as freq / (freq + k1) from:", subs); "tf, computed as freq / (freq + k1) from:", subs);
} else { } else {
byte norm; boolean found = norms.advanceExact(doc);
if (norms.advanceExact(doc)) { assert found;
norm = (byte) norms.longValue(); byte norm = (byte) norms.longValue();
} else {
norm = 0;
}
float doclen = lengthCache[norm & 0xff]; float doclen = lengthCache[norm & 0xff];
subs.add(Explanation.match(b, "b, length normalization parameter")); subs.add(Explanation.match(b, "b, length normalization parameter"));
if ((norm & 0xFF) > 39) { if ((norm & 0xFF) > 39) {

View File

@ -265,16 +265,13 @@ public abstract class SimilarityBase extends Similarity {
if (norms == null) { if (norms == null) {
return 1D; return 1D;
} }
if (norms.advanceExact(doc)) { boolean found = norms.advanceExact(doc);
return LENGTH_TABLE[Byte.toUnsignedInt((byte) norms.longValue())]; assert found;
} else { return LENGTH_TABLE[Byte.toUnsignedInt((byte) norms.longValue())];
return 0;
}
} }
@Override @Override
public float score(int doc, float freq) throws IOException { public float score(int doc, float freq) throws IOException {
// We have to supply something in case norms are omitted
return (float) SimilarityBase.this.score(stats, freq, getLengthValue(doc)); return (float) SimilarityBase.this.score(stats, freq, getLengthValue(doc));
} }

View File

@ -583,12 +583,9 @@ public abstract class TFIDFSimilarity extends Similarity {
if (norms == null) { if (norms == null) {
return raw; return raw;
} else { } else {
float normValue; boolean found = norms.advanceExact(doc);
if (norms.advanceExact(doc)) { assert found;
normValue = normTable[(int) (norms.longValue() & 0xFF)]; float normValue = normTable[(int) (norms.longValue() & 0xFF)];
} else {
normValue = 0;
}
return raw * normValue; // normalize for field 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 { private Explanation explainScore(int doc, Explanation freq, IDFStats stats, NumericDocValues norms, float[] normTable) throws IOException {
List<Explanation> subs = new ArrayList<Explanation>(); List<Explanation> subs = new ArrayList<Explanation>();
if (stats.boost != 1F) { if (stats.boost != 1F) {
@ -662,9 +638,9 @@ public abstract class TFIDFSimilarity extends Similarity {
float norm; float norm;
if (norms == null) { if (norms == null) {
norm = 1f; norm = 1f;
} else if (norms.advanceExact(doc) == false) {
norm = 0f;
} else { } else {
boolean found = norms.advanceExact(doc);
assert found;
norm = normTable[(int) (norms.longValue() & 0xFF)]; norm = normTable[(int) (norms.longValue() & 0xFF)];
} }