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


##########
lucene/core/src/test/org/apache/lucene/search/TestKnnByteVectorQuery.java:
##########
@@ -102,14 +103,34 @@ public void testVectorEncodingMismatch() throws 
IOException {
     }
   }
 
+  public void testTimeout() throws IOException {

Review Comment:
   We could also add a test for the partial result case. You could create a 
mock query timeout that returns `false` only for the first time it's called, 
and then flips to returning `true`.



##########
lucene/core/src/java/org/apache/lucene/search/TimeLimitingKnnCollectorManager.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.QueryTimeout;
+import org.apache.lucene.search.knn.KnnCollectorManager;
+
+/** A {@link KnnCollectorManager} that collects results with a timeout. */
+public class TimeLimitingKnnCollectorManager implements KnnCollectorManager {
+  private final KnnCollectorManager delegate;
+  private final QueryTimeout queryTimeout;
+
+  public TimeLimitingKnnCollectorManager(KnnCollectorManager delegate, 
QueryTimeout timeout) {
+    this.delegate = delegate;
+    this.queryTimeout = timeout;
+  }
+
+  /** Get the {@link QueryTimeout} for terminating graph searches. */
+  public QueryTimeout getQueryTimeout() {
+    return queryTimeout;
+  }
+
+  @Override
+  public KnnCollector newCollector(int visitedLimit, LeafReaderContext 
context) throws IOException {
+    KnnCollector collector = delegate.newCollector(visitedLimit, context);
+    if (queryTimeout == null) {
+      return collector;
+    }
+    return new KnnCollector() {
+      @Override
+      public boolean earlyTerminated() {
+        return queryTimeout.shouldExit() || collector.earlyTerminated();
+      }
+
+      @Override
+      public void incVisitedCount(int count) {
+        collector.incVisitedCount(count);
+      }
+
+      @Override
+      public long visitedCount() {
+        return collector.visitedCount();
+      }
+
+      @Override
+      public long visitLimit() {
+        return collector.visitLimit();
+      }
+
+      @Override
+      public int k() {
+        return collector.k();
+      }
+
+      @Override
+      public boolean collect(int docId, float similarity) {
+        return collector.collect(docId, similarity);
+      }
+
+      @Override
+      public float minCompetitiveSimilarity() {
+        return collector.minCompetitiveSimilarity();
+      }
+
+      @Override
+      public TopDocs topDocs() {
+        TopDocs docs = collector.topDocs();
+
+        // Mark results as partial if timeout is met
+        TotalHits.Relation relation =
+            queryTimeout.shouldExit()
+                ? TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO
+                : docs.totalHits.relation;
+
+        return new TopDocs(new TotalHits(docs.totalHits.value, relation), 
docs.scoreDocs);

Review Comment:
   Can we simply return `collector.topDocs();`  and let collectors decide how 
to handle this? All implementations of `AbstractKnnCollector` already set 
relation to `GTE` based on `earlyTerminated()` check.
   



-- 
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