Skip to content

fix(elasticsearch): avoid NaN score when first hit score is zero - #2748

Open
skyzhao1223 wants to merge 1 commit into
coze-dev:mainfrom
skyzhao1223:fix/es-score-zero-guard
Open

skyzhao1223 wants to merge 1 commit into
coze-dev:mainfrom
skyzhao1223:fix/es-score-zero-guard

Conversation

@skyzhao1223

Copy link
Copy Markdown

Fixes a NaN/Inf score bug in the Elasticsearch search store.

Problem

parseSearchResult normalized every hit's score by the first hit's _score:

score := float64(ptr.From(hit.Score_))
if i == 0 {
    firstScore = score
}
doc.WithScore(score / firstScore)

When the first hit's _score is 0 (e.g. constant-score or filter context with no ranking), 0/0 produces NaN for the first hit and Inf for the rest, corrupting downstream score-based sorting and threshold filtering.

Fix

Fall back to the raw score when the first score is 0 (there is nothing to normalize against):

if firstScore != 0 {
    doc.WithScore(score / firstScore)
} else {
    doc.WithScore(score)
}

Tests

elasticsearch_searchstore_test.go adds:

  • TestParseSearchResultZeroScore: zero scores produce no NaN (fails on old code).
  • TestParseSearchResultNormalizesScore: [2,4,1] normalizes to [1,2,0.5].
go test ./infra/document/searchstore/impl/elasticsearch/ -run TestParseSearchResult

parseSearchResult normalized every hit by the first hit's _score. When the
first _score is 0 (constant-score/filter context, no ranking), 0/0 produces
NaN for the first hit and Infinity for the rest, corrupting downstream
sorting and threshold filtering. Fall back to the raw score when the first
score is 0.

Tests cover the zero-score (no NaN) and normal normalization cases; the
zero-score test fails on the old code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant