msokolov commented on code in PR #14226: URL: https://github.com/apache/lucene/pull/14226#discussion_r1965419004
########## lucene/core/src/java/org/apache/lucene/search/OptimisticKnnVectorQuery.java: ########## @@ -0,0 +1,205 @@ +/* + * 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 java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; +import org.apache.lucene.codecs.lucene90.IndexedDISI; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.KnnVectorValues; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.search.knn.KnnCollectorManager; +import org.apache.lucene.search.knn.KnnSearchStrategy; + +/** + * Like {@link KnnFloatVectorQuery} but makes an optimistic assumption about the distribution of + * documents among segments: namely that they are uniform-random w.r.t. vector distance. This is + * unsafe, so it checks the assumption after running the queries and runs a second pass if needed in + * any segments for which the assumption proves to be false. The check is simple: is the worst hit + * in the result queue for a segment in the global top K? If so, explore that segment further using + * seeded KNN search query, seeding with the initial results. + */ +// TODO: rename as float? move methods to AbstractKnnVectorQuery? make a Strategy? Replace existing +// collection strategy? +// yes, I think we should merge this stuff w/AbstractKnnVectorQuery, enable it with a +// KnnSearchStrategy, +// and extend KnnFloatVectorQuery/KnnByteVectorQuery in a simple way +public class OptimisticKnnVectorQuery extends KnnFloatVectorQuery { + + public OptimisticKnnVectorQuery(String field, float[] target, int k, Query filter) { + super(field, target, k, filter); + } + + public OptimisticKnnVectorQuery(String field, float[] target, int k) { + super(field, target, k, null); + } + + @Override + public Query rewrite(IndexSearcher indexSearcher) throws IOException { + IndexReader reader = indexSearcher.getIndexReader(); + + final Weight filterWeight; + if (filter != null) { + BooleanQuery booleanQuery = + new BooleanQuery.Builder() + .add(filter, BooleanClause.Occur.FILTER) + .add(new FieldExistsQuery(field), BooleanClause.Occur.FILTER) + .build(); + Query rewritten = indexSearcher.rewrite(booleanQuery); + filterWeight = indexSearcher.createWeight(rewritten, ScoreMode.COMPLETE_NO_SCORES, 1f); + } else { + filterWeight = null; + } + + TimeLimitingKnnCollectorManager knnCollectorManager = + new TimeLimitingKnnCollectorManager( + getKnnCollectorManager(k, indexSearcher), indexSearcher.getTimeout()); + TaskExecutor taskExecutor = indexSearcher.getTaskExecutor(); + List<LeafReaderContext> leafReaderContexts = new ArrayList<>(reader.leaves()); + List<Callable<TopDocs>> tasks = new ArrayList<>(leafReaderContexts.size()); + for (LeafReaderContext context : leafReaderContexts) { + tasks.add(() -> searchLeaf(context, filterWeight, knnCollectorManager)); + } + TopDocs topK = null; + Map<Integer, TopDocs> perLeafResults = new HashMap<>(leafReaderContexts.size()); + int kInLoop = k; + while (tasks.isEmpty() == false) { + List<TopDocs> taskResults = taskExecutor.invokeAll(tasks); + for (int i = 0; i < taskResults.size(); i++) { + perLeafResults.put(leafReaderContexts.get(i).ord, taskResults.get(i)); + } + tasks.clear(); + // Merge sort the results + topK = mergeLeafResults(perLeafResults.values().toArray(TopDocs[]::new)); + if (topK.scoreDocs.length == 0 || perLeafResults.size() <= 1) { + break; + } + float minTopKScore = topK.scoreDocs[topK.scoreDocs.length - 1].score; + kInLoop *= 2; + TimeLimitingKnnCollectorManager knnCollectorManagerInner = + new TimeLimitingKnnCollectorManager( + new ReentrantKnnCollectorManager( + getKnnCollectorManager(kInLoop, indexSearcher), perLeafResults), + indexSearcher.getTimeout()); + // System.out.println("k=" + k + " kloop=" + kInLoop); + Iterator<LeafReaderContext> ctxIter = leafReaderContexts.iterator(); + while (ctxIter.hasNext()) { + LeafReaderContext ctx = ctxIter.next(); + TopDocs perLeaf = perLeafResults.get(ctx.ord); + if (perLeaf.scoreDocs.length > 0 + && perLeaf.scoreDocs[perLeaf.scoreDocs.length - 1].score >= minTopKScore + && perLeafTopKCalculation(kInLoop / 2, ctx.reader().maxDoc() / (float) reader.maxDoc()) + < k) { + // All this leaf's hits are at or above the global topK min score; explore it further, and + // we have not yet tried perLeafK >= k. + // System.out.println("leaf " + ctx.ord + " #hits=" + perLeaf.scoreDocs.length + " + // #min-score=" + perLeaf.scoreDocs[perLeaf.scoreDocs.length - 1].score + " #global-min=" + // + minTopKScore); + // System.out.println("re-try search of leaf " + ctx.ord + "; K'=" + kInLoop); + tasks.add(() -> searchLeaf(ctx, filterWeight, knnCollectorManagerInner)); + } else { + // This leaf is tapped out; discard the context from the active list so we maintain + // correspondence between tasks and leaves + ctxIter.remove(); + } + } + assert leafReaderContexts.size() == tasks.size(); + assert perLeafResults.size() == reader.leaves().size(); + } + + if (topK == null || topK.scoreDocs.length == 0) { + return new MatchNoDocsQuery(); + } + return createRewrittenQuery(reader, topK); + } + + @Override + protected KnnCollectorManager getKnnCollectorManager(int k, IndexSearcher searcher) { + KnnCollectorManager manager = + (visitedLimit, strategy, context) -> { + @SuppressWarnings("resource") + float leafProportion = + context.reader().maxDoc() / (float) context.parent.reader().maxDoc(); + int perLeafTopK = perLeafTopKCalculation(k, leafProportion); + assert perLeafTopK + > 0; // if we divided by zero above, leafProportion can be NaN and then this would be + // 0 + return new TopKnnCollector(perLeafTopK, visitedLimit); Review Comment: yes, we can do it in perLeafTopkCalculation -- 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