-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[8.19] Add L2 norm normalization support to linear retriever #128972
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 13 commits into
elastic:8.19
from
mridula-s109:backport/8.19/pr-128504-pr-128808
Jun 11, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d9e586b
Add l2_norm normalization support to linear retriever (#128504)
mridula-s109 c10f782
Clarify Javadoc for L2ScoreNormalizer (l2_norm) (#128808)
mridula-s109 fe0d87e
Merge branch '8.19' into backport/8.19/pr-128504-pr-128808
mridula-s109 8191765
Add Cluster Feature for L2 Norm (#129181)
mridula-s109 59dc593
Remove changelog for 129181, keep only 128504.yaml as the changelog e…
mridula-s109 5a48427
Remove redundant retrievers.md, documentation is now in retrievers-ov…
mridula-s109 eea0d29
updated retriever-overview.asciidoc
mridula-s109 65b9c5b
Resolved duplicate tag issue
mridula-s109 67a3290
Merge branch '8.19' into backport/8.19/pr-128504-pr-128808
mridula-s109 e8192d9
Merge branch '8.19' into backport/8.19/pr-128504-pr-128808
mridula-s109 1aced59
Resolved the test case which caused because of merge
mridula-s109 cbd1cf0
Merge branch '8.19' into backport/8.19/pr-128504-pr-128808
mridula-s109 d644583
Merge branch '8.19' into backport/8.19/pr-128504-pr-128808
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
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
66 changes: 66 additions & 0 deletions
66
.../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,66 @@ | ||
|
|
||
| /* | ||
| * 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.features.NodeFeature; | ||
|
|
||
| /** | ||
| * A score normalizer that applies L2 normalization to a set of scores. | ||
| * <p> | ||
| * Each score is divided by the L2 norm of the scores if the norm is greater than a small EPSILON. | ||
| * If all scores are zero or NaN, normalization is skipped and the original scores are returned. | ||
| * </p> | ||
| */ | ||
| public class L2ScoreNormalizer extends ScoreNormalizer { | ||
|
|
||
| public static final L2ScoreNormalizer INSTANCE = new L2ScoreNormalizer(); | ||
|
|
||
| public static final String NAME = "l2_norm"; | ||
|
|
||
| private static final float EPSILON = 1e-6f; | ||
|
|
||
| public static final NodeFeature LINEAR_RETRIEVER_L2_NORM = new NodeFeature("linear_retriever.l2_norm"); | ||
|
|
||
| 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) { | ||
mridula-s109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| atLeastOneValidScore = true; | ||
| sumOfSquares += doc.score * doc.score; | ||
| } | ||
| } | ||
| if (atLeastOneValidScore == false) { | ||
mridula-s109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // No valid scores to normalize | ||
| return docs; | ||
| } | ||
| double norm = Math.sqrt(sumOfSquares); | ||
| if (norm < EPSILON) { | ||
| return docs; | ||
| } | ||
| 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
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.