-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add l2_norm normalization support to linear retriever #128504
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
Merged
mridula-s109
merged 20 commits into
elastic:main
from
mridula-s109:SEARCH-921-add-l-2-norm-normalization-support-to-linear-retriever
Jun 2, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4575e00
New l2 normalizer added
mridula-s109 e3c158b
L2 score normaliser is registered
mridula-s109 7f0195d
test case added to the yaml
mridula-s109 3743e5b
Documentation added
mridula-s109 a153599
Resolved checkstyle issues
mridula-s109 cc8378e
Merge branch 'main' into SEARCH-921-add-l-2-norm-normalization-suppor…
mridula-s109 9e93652
Update docs/changelog/128504.yaml
mridula-s109 7756928
Update docs/reference/elasticsearch/rest-apis/retrievers.md
mridula-s109 48ae44f
Score 0 test case added to check for corner cases
mridula-s109 7cbcf94
Edited the markdown doc description
mridula-s109 c5187bf
Pruned the comment
mridula-s109 b4e7c70
Renamed the variable
mridula-s109 9a29ebd
Added comment to the class
mridula-s109 a9a2327
Unit tests added
mridula-s109 1b59dac
Spotless and checkstyle fixed
mridula-s109 46a7d3f
Merge branch 'main' into SEARCH-921-add-l-2-norm-normalization-suppor…
mridula-s109 2ccb6ac
Fixed build failure
mridula-s109 a0ab7e3
Fixed the forbidden test
mridula-s109 64ab227
Merge branch 'main' into SEARCH-921-add-l-2-norm-normalization-suppor…
mridula-s109 51c9518
Merge branch 'main' into SEARCH-921-add-l-2-norm-normalization-suppor…
mridula-s109 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 128504 | ||
| summary: Add l2_norm normalization support to linear retriever | ||
| area: Relevance | ||
| type: enhancement | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../plugin/rank-rrf/src/main/java/org/elasticsearch/xpack/rank/linear/L2ScoreNormalizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
|
|
||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.rank.linear; | ||
|
|
||
| import org.apache.lucene.search.ScoreDoc; | ||
|
|
||
| /** | ||
| * A score normalizer that applies L2 normalization to a set of scores. | ||
| * <p> | ||
| * This normalizer scales the scores so that the L2 norm of the score vector is 1, | ||
| * if possible. If all scores are zero or NaN, normalization is skipped and the original scores are returned. | ||
| * </p> | ||
| */ | ||
| public class L2ScoreNormalizer extends ScoreNormalizer { | ||
mridula-s109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static final L2ScoreNormalizer INSTANCE = new L2ScoreNormalizer(); | ||
|
|
||
| public static final String NAME = "l2_norm"; | ||
|
|
||
| private static final float EPSILON = 1e-6f; | ||
|
|
||
| public L2ScoreNormalizer() {} | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public ScoreDoc[] normalizeScores(ScoreDoc[] docs) { | ||
| if (docs.length == 0) { | ||
| return docs; | ||
| } | ||
| double sumOfSquares = 0.0; | ||
| boolean atLeastOneValidScore = false; | ||
| for (ScoreDoc doc : docs) { | ||
| if (Float.isNaN(doc.score) == false) { | ||
| atLeastOneValidScore = true; | ||
| sumOfSquares += doc.score * doc.score; | ||
| } | ||
| } | ||
| if (atLeastOneValidScore == false) { | ||
| // No valid scores to normalize | ||
| return docs; | ||
| } | ||
| double norm = Math.sqrt(sumOfSquares); | ||
| if (norm < EPSILON) { | ||
| return docs; | ||
mridula-s109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| ScoreDoc[] scoreDocs = new ScoreDoc[docs.length]; | ||
| for (int i = 0; i < docs.length; i++) { | ||
| float score = (float) (docs[i].score / norm); | ||
| scoreDocs[i] = new ScoreDoc(docs[i].doc, score, docs[i].shardIndex); | ||
| } | ||
| return scoreDocs; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...in/rank-rrf/src/test/java/org/elasticsearch/xpack/rank/linear/L2ScoreNormalizerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.rank.linear; | ||
|
|
||
| import org.apache.lucene.search.ScoreDoc; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| public class L2ScoreNormalizerTests extends ESTestCase { | ||
|
|
||
| public void testNormalizeTypicalVector() { | ||
| ScoreDoc[] docs = { new ScoreDoc(1, 3.0f, 0), new ScoreDoc(2, 4.0f, 0) }; | ||
| ScoreDoc[] normalized = L2ScoreNormalizer.INSTANCE.normalizeScores(docs); | ||
| assertEquals(0.6f, normalized[0].score, 1e-5); | ||
| assertEquals(0.8f, normalized[1].score, 1e-5); | ||
| } | ||
|
|
||
| public void testAllZeros() { | ||
| ScoreDoc[] docs = { new ScoreDoc(1, 0.0f, 0), new ScoreDoc(2, 0.0f, 0) }; | ||
| ScoreDoc[] normalized = L2ScoreNormalizer.INSTANCE.normalizeScores(docs); | ||
| assertEquals(0.0f, normalized[0].score, 0.0f); | ||
| assertEquals(0.0f, normalized[1].score, 0.0f); | ||
| } | ||
|
|
||
| public void testAllNaN() { | ||
| ScoreDoc[] docs = { new ScoreDoc(1, Float.NaN, 0), new ScoreDoc(2, Float.NaN, 0) }; | ||
| ScoreDoc[] normalized = L2ScoreNormalizer.INSTANCE.normalizeScores(docs); | ||
| assertTrue(Float.isNaN(normalized[0].score)); | ||
| assertTrue(Float.isNaN(normalized[1].score)); | ||
| } | ||
|
|
||
| public void testMixedZeroAndNaN() { | ||
| ScoreDoc[] docs = { new ScoreDoc(1, 0.0f, 0), new ScoreDoc(2, Float.NaN, 0) }; | ||
| ScoreDoc[] normalized = L2ScoreNormalizer.INSTANCE.normalizeScores(docs); | ||
| assertEquals(0.0f, normalized[0].score, 0.0f); | ||
| assertTrue(Float.isNaN(normalized[1].score)); | ||
| } | ||
|
|
||
| public void testSingleElement() { | ||
| ScoreDoc[] docs = { new ScoreDoc(1, 42.0f, 0) }; | ||
| ScoreDoc[] normalized = L2ScoreNormalizer.INSTANCE.normalizeScores(docs); | ||
| assertEquals(1.0f, normalized[0].score, 1e-5); | ||
| } | ||
|
|
||
| public void testEmptyArray() { | ||
| ScoreDoc[] docs = {}; | ||
| ScoreDoc[] normalized = L2ScoreNormalizer.INSTANCE.normalizeScores(docs); | ||
| assertEquals(0, normalized.length); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.