You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by be...@apache.org on 2024/03/11 12:08:35 UTC

(lucene) branch main updated: Fix TestFloatVectorSimilarityQuery.testBoostQuery (#13176)

This is an automated email from the ASF dual-hosted git repository.

benwtrent pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 3f4413567d2 Fix TestFloatVectorSimilarityQuery.testBoostQuery (#13176)
3f4413567d2 is described below

commit 3f4413567d2d41ecee16fcfe2c91a4b84bec0df8
Author: Tim Grein <ti...@elastic.co>
AuthorDate: Mon Mar 11 13:08:29 2024 +0100

    Fix TestFloatVectorSimilarityQuery.testBoostQuery (#13176)
    
    ### Description
    
    The boosted doc scores were the same due to floating point precision limitations, when multiplying the original scores with tiny differences with a fairly large multiplier.
    
    My change removes the assertion expecting the exact order to be the same (AFAIU this won't be possible for every scores/boost configurations due to precision limitations), but rather asserts that the `original doc score * boost factor` and the `boosted doc score` are equals within a certain delta.
    
    Closes https://github.com/apache/lucene/issues/13173.
---
 .../lucene/search/BaseVectorSimilarityQueryTestCase.java   | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java b/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java
index 40f31fb2901..5ad66b90d59 100644
--- a/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java
+++ b/lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java
@@ -23,6 +23,7 @@ import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.stream.IntStream;
 import org.apache.lucene.document.Document;
@@ -356,11 +357,18 @@ abstract class BaseVectorSimilarityQueryTestCase<
       Query query2 = new BoostQuery(query1, boost);
       ScoreDoc[] scoreDocs2 = searcher.search(query2, numDocs).scoreDocs;
 
-      // Check that all docs are identical, with boosted scores
+      // Check that original scores and boosted scores are equal considering the delta to account
+      // for floating point precision limitations. Don't take the exact result order into
+      // consideration as for small original scores with tiny differences
+      // the boosted scores can be become the same, which might affect the result order.
       assertEquals(scoreDocs1.length, scoreDocs2.length);
       for (int i = 0; i < scoreDocs1.length; i++) {
-        assertEquals(scoreDocs1[i].doc, scoreDocs2[i].doc);
-        assertEquals(boost * scoreDocs1[i].score, scoreDocs2[i].score, delta);
+        int idx = i;
+        Optional<ScoreDoc> boostedDoc =
+            Arrays.stream(scoreDocs2).filter(d -> d.doc == scoreDocs1[idx].doc).findFirst();
+
+        assertTrue(boostedDoc.isPresent());
+        assertEquals(boost * scoreDocs1[i].score, boostedDoc.get().score, delta);
       }
     }
   }