vigyasharma commented on code in PR #13285:
URL: https://github.com/apache/lucene/pull/13285#discussion_r1704745053


##########
lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java:
##########
@@ -475,6 +479,62 @@ public void testApproximate() throws IOException {
     }
   }
 
+  /** Test that the query times out correctly. */
+  public void testTimeout() throws IOException {
+    V[] vectors = getRandomVectors(numDocs, dim);
+    V queryVector = getRandomVector(dim);
+
+    try (Directory indexStore = getIndexStore(vectors);
+        IndexReader reader = DirectoryReader.open(indexStore)) {
+      IndexSearcher searcher = newSearcher(reader);
+
+      // This query is cacheable, explicitly prevent it
+      searcher.setQueryCache(null);
+
+      Query query =
+          new CountingQuery(
+              getVectorQuery(
+                  vectorField,
+                  queryVector,
+                  Float.NEGATIVE_INFINITY,
+                  Float.NEGATIVE_INFINITY,
+                  null));
+
+      assertEquals(numDocs, searcher.count(query)); // Expect some results 
without timeout
+
+      searcher.setTimeout(() -> true); // Immediately timeout
+      assertEquals(0, searcher.count(query)); // Expect no results with the 
timeout
+
+      searcher.setTimeout(new CountingQueryTimeout(numDocs - 1)); // Do not 
score all docs
+      int count = searcher.count(query);
+      assertTrue(
+          "0 < count=" + count + " < numDocs=" + numDocs,
+          count > 0 && count < numDocs); // Expect partial results
+
+      // Test timeout with filter
+      int numFiltered = random().nextInt(numDocs / 2, numDocs);
+      Query filter = IntField.newSetQuery(idField, getFiltered(numFiltered));
+      Query filteredQuery =
+          new CountingQuery(
+              getVectorQuery(
+                  vectorField,
+                  queryVector,
+                  Float.NEGATIVE_INFINITY,
+                  Float.NEGATIVE_INFINITY,
+                  filter));
+
+      searcher.setTimeout(() -> false); // Set a timeout which is never met
+      assertEquals(numFiltered, searcher.count(filteredQuery));
+
+      searcher.setTimeout(
+          new CountingQueryTimeout(numFiltered - 1)); // Timeout before 
scoring all filtered docs
+      int filteredCount = searcher.count(filteredQuery);
+      assertTrue(
+          "0 < filteredCount=" + filteredCount + " < numFiltered=" + 
numFiltered,
+          filteredCount > 0 && filteredCount < numFiltered); // Expect partial 
results

Review Comment:
   So this tests for cases where we timeout before exhausting the filter, nice!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to