|
20 | 20 | package org.elasticsearch.index.codec.vectors.es910.hnsw; |
21 | 21 |
|
22 | 22 | import org.apache.lucene.search.KnnCollector; |
23 | | -import org.apache.lucene.search.TopKnnCollector; |
24 | 23 | import org.apache.lucene.util.BitSet; |
25 | 24 | import org.apache.lucene.util.Bits; |
26 | 25 | import org.apache.lucene.util.FixedBitSet; |
27 | | -import org.apache.lucene.util.SparseFixedBitSet; |
28 | 26 | import org.apache.lucene.util.hnsw.HnswGraph; |
| 27 | +import org.apache.lucene.util.hnsw.NeighborQueue; |
29 | 28 | import org.apache.lucene.util.hnsw.RandomVectorScorer; |
30 | 29 |
|
31 | 30 | import java.io.IOException; |
@@ -56,55 +55,6 @@ public HnswGraphSearcher(NeighborQueue candidates, BitSet visited) { |
56 | 55 | this.visited = visited; |
57 | 56 | } |
58 | 57 |
|
59 | | - /** |
60 | | - * Search {@link OnHeapHnswGraph}, this method is thread safe. |
61 | | - * |
62 | | - * @param scorer the scorer to compare the query with the nodes |
63 | | - * @param topK the number of nodes to be returned |
64 | | - * @param graph the graph values. May represent the entire graph, or a level in a hierarchical |
65 | | - * graph. |
66 | | - * @param acceptOrds {@link Bits} that represents the allowed document ordinals to match, or |
67 | | - * {@code null} if they are all allowed to match. |
68 | | - * @param visitedLimit the maximum number of nodes that the search is allowed to visit |
69 | | - * @return a set of collected vectors holding the nearest neighbors found |
70 | | - */ |
71 | | - public static KnnCollector search(RandomVectorScorer scorer, int topK, OnHeapHnswGraph graph, Bits acceptOrds, int visitedLimit) |
72 | | - throws IOException { |
73 | | - KnnCollector knnCollector = new TopKnnCollector(topK, visitedLimit, null); |
74 | | - OnHeapHnswGraphSearcher graphSearcher = new OnHeapHnswGraphSearcher( |
75 | | - new NeighborQueue(topK, true), |
76 | | - new SparseFixedBitSet(getGraphSize(graph)) |
77 | | - ); |
78 | | - graphSearcher.search(knnCollector, scorer, graph, acceptOrds); |
79 | | - return knnCollector; |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Searches for the nearest neighbors of a query vector in a given level. |
84 | | - * |
85 | | - * <p>If the search stops early because it reaches the visited nodes limit, then the results will |
86 | | - * be marked incomplete through {@link NeighborQueue#incomplete()}. |
87 | | - * |
88 | | - * @param scorer the scorer to compare the query with the nodes |
89 | | - * @param topK the number of nearest to query results to return |
90 | | - * @param level level to search |
91 | | - * @param eps the entry points for search at this level expressed as level 0th ordinals |
92 | | - * @param graph the graph values |
93 | | - * @return a set of collected vectors holding the nearest neighbors found |
94 | | - */ |
95 | | - public HnswGraphBuilder.GraphBuilderKnnCollector searchLevel( |
96 | | - // Note: this is only public because Lucene91HnswGraphBuilder needs it |
97 | | - RandomVectorScorer scorer, |
98 | | - int topK, |
99 | | - int level, |
100 | | - final int[] eps, |
101 | | - HnswGraph graph |
102 | | - ) throws IOException { |
103 | | - HnswGraphBuilder.GraphBuilderKnnCollector results = new HnswGraphBuilder.GraphBuilderKnnCollector(topK); |
104 | | - searchLevel(results, scorer, level, eps, graph, null); |
105 | | - return results; |
106 | | - } |
107 | | - |
108 | 58 | /** |
109 | 59 | * Function to find the best entry point from which to search the zeroth graph layer. |
110 | 60 | * |
@@ -271,36 +221,4 @@ int graphNextNeighbor(HnswGraph graph) throws IOException { |
271 | 221 | static int getGraphSize(HnswGraph graph) { |
272 | 222 | return graph.maxNodeId() + 1; |
273 | 223 | } |
274 | | - |
275 | | - /** |
276 | | - * This class allows {@link OnHeapHnswGraph} to be searched in a thread-safe manner by avoiding |
277 | | - * the unsafe methods (seek and nextNeighbor, which maintain state in the graph object) and |
278 | | - * instead maintaining the state in the searcher object. |
279 | | - * |
280 | | - * <p>Note the class itself is NOT thread safe, but since each search will create a new Searcher, |
281 | | - * the search methods using this class are thread safe. |
282 | | - */ |
283 | | - private static class OnHeapHnswGraphSearcher extends HnswGraphSearcher { |
284 | | - |
285 | | - private NeighborArray cur; |
286 | | - private int upto; |
287 | | - |
288 | | - private OnHeapHnswGraphSearcher(NeighborQueue candidates, BitSet visited) { |
289 | | - super(candidates, visited); |
290 | | - } |
291 | | - |
292 | | - @Override |
293 | | - void graphSeek(HnswGraph graph, int level, int targetNode) { |
294 | | - cur = ((OnHeapHnswGraph) graph).getNeighbors(level, targetNode); |
295 | | - upto = -1; |
296 | | - } |
297 | | - |
298 | | - @Override |
299 | | - int graphNextNeighbor(HnswGraph graph) { |
300 | | - if (++upto < cur.size()) { |
301 | | - return cur.nodes()[upto]; |
302 | | - } |
303 | | - return NO_MORE_DOCS; |
304 | | - } |
305 | | - } |
306 | 224 | } |
0 commit comments