Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ranking: add tiebreakers to BM25 #914

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions index/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

const (
ScoreOffset = 10_000_000
ScoreOffset = 10_000_000
ScoreOffsetBM25 = 1_000_000_000
)

type chunkScore struct {
Expand Down Expand Up @@ -299,35 +300,22 @@ func (d *indexData) scoreFile(fileMatch *zoekt.FileMatch, doc uint32, mt matchTr
fileMatch.ChunkMatches[i].Score += scoreLineOrderFactor * (1.0 - (float64(i) / float64(len(fileMatch.ChunkMatches))))
}

// Maintain ordering of input files. This
// strictly dominates the in-file ordering of
// the matches.
// Maintain ordering of input files. This strictly dominates the in-file ordering of the matches.
addScore("fragment", maxFileScore)

// Add tiebreakers
//
// ScoreOffset shifts the score 7 digits to the left.
fileMatch.Score = math.Trunc(fileMatch.Score) * ScoreOffset

md := d.repoMetaData[d.repos[doc]]

// md.Rank lies in the range [0, 65535]. Hence, we have to allocate 5 digits for
// the rank. The scoreRepoRankFactor shifts the rank score 2 digits to the left,
// reserving digits 3-7 for the repo rank.
addScore("repo-rank", scoreRepoRankFactor*float64(md.Rank))
// Truncate score to avoid overlap with the tiebreakers.
fileMatch.Score = math.Trunc(fileMatch.Score)

// digits 1-2 and the decimals are reserved for the doc order. Doc order
// (without the scaling factor) lies in the range [0, 1]. The upper bound is
// achieved for matches in the first document of a shard.
addScore("doc-order", scoreFileOrderFactor*(1.0-float64(doc)/float64(len(d.boundaries))))
// Add tiebreakers
repoRank := d.repoMetaData[d.repos[doc]].Rank // [0, 65535]
docOrderScore := 1.0 - float64(doc)/float64(len(d.boundaries)) // [0, 1]

if opts.DebugScore {
// To make the debug output easier to read, we split the score into the query
// dependent score and the tiebreaker
score := math.Trunc(fileMatch.Score / ScoreOffset)
tiebreaker := fileMatch.Score - score*ScoreOffset
fileMatch.Debug = fmt.Sprintf("score: %d (%.2f) <- %s", int(score), tiebreaker, strings.TrimSuffix(fileMatch.Debug, ", "))
// We log the score components individually for better readability.
fileMatch.Debug = fmt.Sprintf("score: %d (tiebreaker: [%d, %.2f]) <- %s", int(fileMatch.Score), repoRank, docOrderScore, strings.TrimSuffix(fileMatch.Debug, ", "))
}

fileMatch.Score = ScoreOffset*fileMatch.Score + scoreRepoRankFactor*float64(repoRank) + scoreFileOrderFactor*docOrderScore
}

// scoreFilesUsingBM25 computes the score according to BM25, the most common scoring algorithm for text search:
Expand Down Expand Up @@ -361,10 +349,26 @@ func (d *indexData) scoreFilesUsingBM25(fileMatch *zoekt.FileMatch, doc uint32,
sumTF += f
score += tfScore(k, b, L, f)
}
// 2 digits of precision
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using 2 digits here simply so we can add the tiebreaker? Or does it aid in "collapsing" similar BM25 scores together, to allow tiebreakers to actually become relevant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We caught up about this over Zoom. We don't believe this will actually collapse BM25 scores, it's just so we can apply the tiebreaker.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2 digits are just to avoid an overlap with the tiebreaker. In my experiments and based on the evaluations, 2 digits seemed sufficient, but we can simply increase precision if we see that's necessary.

score = math.Trunc(score*100) / 100

fileMatch.Score = score
md := d.repoMetaData[d.repos[doc]]
fileOrderScore := 1.0 - float64(doc)/float64(len(d.boundaries))

// Offset score by 9 digits and add the tiebreaker.
//
// Example: For a BM25 score of 1.23, a repo rank of 456789 and a file order score of 0.12, we have a final score of
// 12345678901.2
// ^^^
// bm25
// ^^^^^^
// repo rank
// ^^^^
// doc order
fileMatch.Score = score*ScoreOffsetBM25 + scoreRepoRankFactor*float64(md.Rank) + scoreFileOrderFactor*fileOrderScore

if opts.DebugScore {
fileMatch.Debug = fmt.Sprintf("bm25-score: %.2f <- sum-termFrequencies: %d, length-ratio: %.2f", score, sumTF, L)
// To make the debug output easier to read, we split the score into the query dependent score and the tiebreaker
fileMatch.Debug = fmt.Sprintf("bm25-score: %.2f (tiebreaker: [%d, %.2f]) <- sum-termFrequencies: %d, length-ratio: %.2f, ", score, md.Rank, fileOrderScore, sumTF, L)
}
}
3 changes: 2 additions & 1 deletion internal/e2e/scoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ func checkScoring(t *testing.T, c scoreCase, useBM25 bool, parserType ctags.CTag
// helper to remove the tiebreaker from the score for easier comparison
func withoutTiebreaker(fullScore float64, useBM25 bool) float64 {
if useBM25 {
return fullScore
// Shift by ScoreOffsetBM25 and truncate to 2 decimal places
return math.Trunc((fullScore/index.ScoreOffsetBM25)*100) / 100
}
return math.Trunc(fullScore / index.ScoreOffset)
}
Expand Down
Loading