You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ju...@apache.org on 2023/02/14 18:45:32 UTC

[lucene] branch jtibshirani/doc-score-query updated (378e1bdfb23 -> 691966467a4)

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

julietibs pushed a change to branch jtibshirani/doc-score-query
in repository https://gitbox.apache.org/repos/asf/lucene.git


 discard 378e1bdfb23 Improve DocAndScoreQuery#toString
     new 691966467a4 Improve DocAndScoreQuery#toString

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (378e1bdfb23)
            \
             N -- N -- N   refs/heads/jtibshirani/doc-score-query (691966467a4)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/test/org/apache/lucene/search/TestKnnByteVectorQuery.java  | 7 +++----
 .../src/test/org/apache/lucene/search/TestKnnFloatVectorQuery.java | 6 +++---
 2 files changed, 6 insertions(+), 7 deletions(-)


[lucene] 01/01: Improve DocAndScoreQuery#toString

Posted by ju...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

julietibs pushed a commit to branch jtibshirani/doc-score-query
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit 691966467a4d01c2cd949bfea77412aae6770613
Author: Julie Tibshirani <ju...@sourcegraph.com>
AuthorDate: Tue Feb 14 10:14:14 2023 -0800

    Improve DocAndScoreQuery#toString
    
    Tiny improvements to DocAndScoreQuery:
    * Make toString more informative
    * Remove unnecessary 'k' parameter
---
 .../apache/lucene/search/AbstractKnnVectorQuery.java    | 14 +++++---------
 .../lucene/search/BaseKnnVectorQueryTestCase.java       |  8 ++++----
 .../apache/lucene/search/TestKnnByteVectorQuery.java    | 17 ++++++++++++++---
 .../apache/lucene/search/TestKnnFloatVectorQuery.java   | 13 ++++++++++---
 4 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java
index 41264d94b1a..c87b2a8f08b 100644
--- a/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java
@@ -197,7 +197,7 @@ abstract class AbstractKnnVectorQuery extends Query {
       scores[i] = topK.scoreDocs[i].score;
     }
     int[] segmentStarts = findSegmentStarts(reader, docs);
-    return new DocAndScoreQuery(k, docs, scores, segmentStarts, reader.getContext().id());
+    return new DocAndScoreQuery(docs, scores, segmentStarts, reader.getContext().id());
   }
 
   private int[] findSegmentStarts(IndexReader reader, int[] docs) {
@@ -263,7 +263,6 @@ abstract class AbstractKnnVectorQuery extends Query {
   /** Caches the results of a KnnVector search: a list of docs and their scores */
   static class DocAndScoreQuery extends Query {
 
-    private final int k;
     private final int[] docs;
     private final float[] scores;
     private final int[] segmentStarts;
@@ -272,7 +271,6 @@ abstract class AbstractKnnVectorQuery extends Query {
     /**
      * Constructor
      *
-     * @param k the number of documents requested
      * @param docs the global docids of documents that match, in ascending order
      * @param scores the scores of the matching documents
      * @param segmentStarts the indexes in docs and scores corresponding to the first matching
@@ -282,9 +280,7 @@ abstract class AbstractKnnVectorQuery extends Query {
      * @param contextIdentity an object identifying the reader context that was used to build this
      *     query
      */
-    DocAndScoreQuery(
-        int k, int[] docs, float[] scores, int[] segmentStarts, Object contextIdentity) {
-      this.k = k;
+    DocAndScoreQuery(int[] docs, float[] scores, int[] segmentStarts, Object contextIdentity) {
       this.docs = docs;
       this.scores = scores;
       this.segmentStarts = segmentStarts;
@@ -302,9 +298,9 @@ abstract class AbstractKnnVectorQuery extends Query {
         public Explanation explain(LeafReaderContext context, int doc) {
           int found = Arrays.binarySearch(docs, doc + context.docBase);
           if (found < 0) {
-            return Explanation.noMatch("not in top " + k);
+            return Explanation.noMatch("not in top docs");
           }
-          return Explanation.match(scores[found] * boost, "within top " + k);
+          return Explanation.match(scores[found] * boost, "within top docs");
         }
 
         @Override
@@ -405,7 +401,7 @@ abstract class AbstractKnnVectorQuery extends Query {
 
     @Override
     public String toString(String field) {
-      return "DocAndScore[" + k + "]";
+      return "DocAndScoreQuery[" + docs[0] + ",...][" + scores[0] + ",...]";
     }
 
     @Override
diff --git a/lucene/core/src/test/org/apache/lucene/search/BaseKnnVectorQueryTestCase.java b/lucene/core/src/test/org/apache/lucene/search/BaseKnnVectorQueryTestCase.java
index af91b19444b..65f532fa7a0 100644
--- a/lucene/core/src/test/org/apache/lucene/search/BaseKnnVectorQueryTestCase.java
+++ b/lucene/core/src/test/org/apache/lucene/search/BaseKnnVectorQueryTestCase.java
@@ -379,13 +379,13 @@ abstract class BaseKnnVectorQueryTestCase extends LuceneTestCase {
         assertTrue(matched.isMatch());
         assertEquals(1 / 2f, matched.getValue());
         assertEquals(0, matched.getDetails().length);
-        assertEquals("within top 3", matched.getDescription());
+        assertEquals("within top docs", matched.getDescription());
 
         Explanation nomatch = searcher.explain(query, 4);
         assertFalse(nomatch.isMatch());
         assertEquals(0f, nomatch.getValue());
         assertEquals(0, matched.getDetails().length);
-        assertEquals("not in top 3", nomatch.getDescription());
+        assertEquals("not in top docs", nomatch.getDescription());
       }
     }
   }
@@ -407,13 +407,13 @@ abstract class BaseKnnVectorQueryTestCase extends LuceneTestCase {
         assertTrue(matched.isMatch());
         assertEquals(1 / 2f, matched.getValue());
         assertEquals(0, matched.getDetails().length);
-        assertEquals("within top 3", matched.getDescription());
+        assertEquals("within top docs", matched.getDescription());
 
         Explanation nomatch = searcher.explain(query, 4);
         assertFalse(nomatch.isMatch());
         assertEquals(0f, nomatch.getValue());
         assertEquals(0, matched.getDetails().length);
-        assertEquals("not in top 3", nomatch.getDescription());
+        assertEquals("not in top docs", nomatch.getDescription());
       }
     }
   }
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestKnnByteVectorQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestKnnByteVectorQuery.java
index b3c9a84e98c..33c8ebee293 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestKnnByteVectorQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestKnnByteVectorQuery.java
@@ -16,10 +16,14 @@
  */
 package org.apache.lucene.search;
 
+import java.io.IOException;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.KnnByteVectorField;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.store.Directory;
 import org.apache.lucene.util.TestVectorUtil;
 
 public class TestKnnByteVectorQuery extends BaseKnnVectorQueryTestCase {
@@ -65,9 +69,16 @@ public class TestKnnByteVectorQuery extends BaseKnnVectorQueryTestCase {
     return bytes;
   }
 
-  public void testToString() {
-    AbstractKnnVectorQuery q1 = getKnnVectorQuery("f1", new float[] {0, 1}, 10);
-    assertEquals("KnnByteVectorQuery:f1[0,...][10]", q1.toString("ignored"));
+  public void testToString() throws IOException {
+    try (Directory indexStore =
+            getIndexStore("field", new float[] {0, 1}, new float[] {1, 2}, new float[] {0, 0});
+        IndexReader reader = DirectoryReader.open(indexStore)) {
+      AbstractKnnVectorQuery query = getKnnVectorQuery("field", new float[] {0, 1}, 10);
+      assertEquals("KnnByteVectorQuery:field[0,...][10]", query.toString("ignored"));
+
+      Query rewritten = query.rewrite(newSearcher(reader));
+      assertEquals("DocAndScoreQuery[0,...][1.0,...]", rewritten.toString("ignored"));
+    }
   }
 
   public void testGetTarget() {
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestKnnFloatVectorQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestKnnFloatVectorQuery.java
index 4091586cd06..c4f10f874be 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestKnnFloatVectorQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestKnnFloatVectorQuery.java
@@ -60,9 +60,16 @@ public class TestKnnFloatVectorQuery extends BaseKnnVectorQueryTestCase {
     return new KnnFloatVectorField(name, vector);
   }
 
-  public void testToString() {
-    AbstractKnnVectorQuery q1 = getKnnVectorQuery("f1", new float[] {0, 1}, 10);
-    assertEquals("KnnFloatVectorQuery:f1[0.0,...][10]", q1.toString("ignored"));
+  public void testToString() throws IOException {
+    try (Directory indexStore =
+            getIndexStore("field", new float[] {0, 1}, new float[] {1, 2}, new float[] {0, 0});
+        IndexReader reader = DirectoryReader.open(indexStore)) {
+      AbstractKnnVectorQuery query = getKnnVectorQuery("field", new float[] {0.0f, 1.0f}, 10);
+      assertEquals("KnnFloatVectorQuery:field[0.0,...][10]", query.toString("ignored"));
+
+      Query rewritten = query.rewrite(newSearcher(reader));
+      assertEquals("DocAndScoreQuery[0,...][1.0,...]", rewritten.toString("ignored"));
+    }
   }
 
   public void testGetTarget() {