vigyasharma commented on code in PR #13202: URL: https://github.com/apache/lucene/pull/13202#discussion_r1536908711
########## 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(); Review Comment: Ah, so both `searchLevel()` and `findBestEntryPoint()` in `HnswGraphSearcher` check `earlyTerminated()` on their collector to abort the search, so configuring the timeout check allows us to preempt the approximate search. ########## 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: 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. Any reason why we don't want to keep the same behavior here? ########## 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: I think we should return whatever docs we were able to collect within the time budget. We have already done the heavy work of approximate search for those hits, what's left is only to return those cached results from the `DocAndScoreQuery`. It's not the behavior today, as `TimeLimitingBulkScorer` sees the query as timed out, and doesn't even run the first `interval` on DocAndScoreQuery. I need to think more of a good way around it. I'm thinking that if scorer is doing the heavy work of iterating through ImpactsEnum or PostingsEnum, then it makes sense to early terminate during scoring. But for queries with cached results like `DocAndScoreQuery`, maybe we shouldn't? -- 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