(index) Tune ranking for verbatim matches in the title, rewarding shorter titles

This commit is contained in:
Viktor Lofgren 2024-08-03 14:41:38 +02:00
parent dd15676d33
commit b21f8538a8

View File

@ -280,6 +280,9 @@ public class IndexResultScoreCalculator {
if (positions[i] != null && ctx.regularMask.get(i)) { if (positions[i] != null && ctx.regularMask.get(i)) {
searchableKeywordsCount ++; searchableKeywordsCount ++;
boolean titleMatch = false;
boolean headingMatch = false;
var iter = positions[i].iterator(); var iter = positions[i].iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
@ -288,11 +291,11 @@ public class IndexResultScoreCalculator {
firstPosition = Math.max(firstPosition, pos); firstPosition = Math.max(firstPosition, pos);
if (spans.title.containsPosition(pos)) { if (spans.title.containsPosition(pos)) {
unorderedMatchInTitleCount++; titleMatch = true;
weightedCounts[i] += 2.5f; weightedCounts[i] += 2.5f;
} }
else if (spans.heading.containsPosition(pos)) { else if (spans.heading.containsPosition(pos)) {
unorderedMatchInHeadingCount++; headingMatch = true;
weightedCounts[i] += 2.5f; weightedCounts[i] += 2.5f;
} }
else if (spans.code.containsPosition(pos)) else if (spans.code.containsPosition(pos))
@ -302,10 +305,17 @@ public class IndexResultScoreCalculator {
else if (spans.nav.containsPosition(pos)) else if (spans.nav.containsPosition(pos))
weightedCounts[i] += 0.1f; weightedCounts[i] += 0.1f;
} }
if (titleMatch) {
unorderedMatchInTitleCount++;
}
if (headingMatch) {
unorderedMatchInHeadingCount++;
}
} }
} }
if (!verbatimMatchInTitle && unorderedMatchInTitleCount == searchableKeywordsCount) { if (!verbatimMatchInTitle && searchableKeywordsCount > 2 && unorderedMatchInTitleCount == searchableKeywordsCount) {
coherenceScore += 2.5f * unorderedMatchInTitleCount; coherenceScore += 2.5f * unorderedMatchInTitleCount;
coherenceScore += 2.f * unorderedMatchInTitleCount / titleLength; coherenceScore += 2.f * unorderedMatchInTitleCount / titleLength;
} }