Skip to content

Commit 6373949

Browse files
committed
Remove NeighborQueue
1 parent 8a0a968 commit 6373949

File tree

4 files changed

+2
-272
lines changed

4 files changed

+2
-272
lines changed

server/src/main/java/org/elasticsearch/index/codec/vectors/es910/hnsw/HnswGraphBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.lucene.util.FixedBitSet;
2828
import org.apache.lucene.util.InfoStream;
2929
import org.apache.lucene.util.hnsw.HnswGraph;
30+
import org.apache.lucene.util.hnsw.NeighborQueue;
3031
import org.apache.lucene.util.hnsw.RandomVectorScorer;
3132
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
3233
import org.apache.lucene.util.hnsw.UpdateableRandomVectorScorer;

server/src/main/java/org/elasticsearch/index/codec/vectors/es910/hnsw/HnswGraphSearcher.java

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
package org.elasticsearch.index.codec.vectors.es910.hnsw;
2121

2222
import org.apache.lucene.search.KnnCollector;
23-
import org.apache.lucene.search.TopKnnCollector;
2423
import org.apache.lucene.util.BitSet;
2524
import org.apache.lucene.util.Bits;
2625
import org.apache.lucene.util.FixedBitSet;
27-
import org.apache.lucene.util.SparseFixedBitSet;
2826
import org.apache.lucene.util.hnsw.HnswGraph;
27+
import org.apache.lucene.util.hnsw.NeighborQueue;
2928
import org.apache.lucene.util.hnsw.RandomVectorScorer;
3029

3130
import java.io.IOException;
@@ -56,55 +55,6 @@ public HnswGraphSearcher(NeighborQueue candidates, BitSet visited) {
5655
this.visited = visited;
5756
}
5857

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-
10858
/**
10959
* Function to find the best entry point from which to search the zeroth graph layer.
11060
*
@@ -271,36 +221,4 @@ int graphNextNeighbor(HnswGraph graph) throws IOException {
271221
static int getGraphSize(HnswGraph graph) {
272222
return graph.maxNodeId() + 1;
273223
}
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-
}
306224
}

server/src/main/java/org/elasticsearch/index/codec/vectors/es910/hnsw/HnswLock.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ final class HnswLock {
4141
}
4242
}
4343

44-
Lock read(int level, int node) {
45-
int lockid = hash(level, node) % NUM_LOCKS;
46-
Lock lock = locks[lockid].readLock();
47-
lock.lock();
48-
return lock;
49-
}
50-
5144
Lock write(int level, int node) {
5245
int lockid = hash(level, node) % NUM_LOCKS;
5346
Lock lock = locks[lockid].writeLock();

server/src/main/java/org/elasticsearch/index/codec/vectors/es910/hnsw/NeighborQueue.java

Lines changed: 0 additions & 182 deletions
This file was deleted.

0 commit comments

Comments
 (0)