- 
                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 5 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
    
  
  
    
              
        
          
          
            57 changes: 57 additions & 0 deletions
          
          57 
        
  .../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,57 @@ | ||
| 
     | 
||
| /* | ||
| * 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; | ||
| 
     | 
||
| 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 rd : docs) { | ||
                
      
                  mridula-s109 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if (Float.isNaN(rd.score) == false) { | ||
| atLeastOneValidScore = true; | ||
| sumOfSquares += rd.score * rd.score; | ||
| } | ||
| } | ||
| if (atLeastOneValidScore == false) { | ||
| // No valid scores to normalize | ||
| return docs; | ||
| } | ||
| double norm = Math.sqrt(sumOfSquares); | ||
| if (norm < EPSILON) { | ||
| // Avoid division by zero, return original scores (or set all to zero if you prefer) | ||
| 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
    
  
  
    
              
  
    
      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.