kaivalnp commented on code in PR #13202: URL: https://github.com/apache/lucene/pull/13202#discussion_r1536943966
########## lucene/core/src/java/org/apache/lucene/search/TimeLimitingKnnCollectorManager.java: ########## @@ -0,0 +1,91 @@ +/* + * 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() { + if (queryTimeout.shouldExit()) { + // Quickly exit + return TopDocsCollector.EMPTY_TOPDOCS; + } Review Comment: Returning partial results makes sense, but I passed the empty results here purely because they would be discarded later (so we could save on computations used for [creating the `TopDocs`](https://github.com/apache/lucene/blob/04f335ad79cae019d1a552e41e0029d061d8e3d9/lucene/core/src/java/org/apache/lucene/search/TopKnnCollector.java#L52-L64)) > `TimeLimitingBulkScorer` sees the query as timed out, and doesn't even run the first `interval` > But for queries with cached results like `DocAndScoreQuery`, maybe we shouldn't? I don't think returning partial results would add value _today_, can we add a `TODO` to return them once the above behavior is fixed? > Most KnnCollector implementations (like [this one](https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/search/TopKnnCollector.java#L59-L63)) tend to return whatever results we've collected so far, and set the relation as `TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO` if we early terminated On a separate note, this seems to be a no-op as well -- since we [return fresh results from exact search](https://github.com/apache/lucene/blob/04f335ad79cae019d1a552e41e0029d061d8e3d9/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java#L140-L145) if approximate results are incomplete. I wonder if we can return empty results here as well to save on some computations? -- 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