-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timeout support to AbstractKnnVectorQuery #13202
Changes from all commits
d700b91
b7f0ed2
31a2643
6908af4
e984c80
8ad040a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 configured {@link QueryTimeout} for terminating graph and exact 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); | ||
Comment on lines
+85
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we simply return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the |
||
} | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so both
searchLevel()
andfindBestEntryPoint()
inHnswGraphSearcher
checkearlyTerminated()
on their collector to abort the search, so configuring the timeout check allows us to preempt the approximate search.