mayya-sharipova commented on a change in pull request #416: URL: https://github.com/apache/lucene/pull/416#discussion_r784284582
########## File path: lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraph.java ########## @@ -56,75 +57,124 @@ public final class HnswGraph extends KnnGraphValues { private final int maxConn; + private int numLevels; // the current number of levels in the graph + private int entryNode; // the current graph entry node on the top level - // Each entry lists the top maxConn neighbors of a node. The nodes correspond to vectors added to - // HnswBuilder, and the - // node values are the ordinals of those vectors. - private final List<NeighborArray> graph; + // Nodes by level expressed as the level 0's nodes' ordinals. + // As level 0 contains all nodes, nodesByLevel.get(0) is null. + private final List<int[]> nodesByLevel; + + // graph is a list of graph levels. + // Each level is represented as List<NeighborArray> – nodes' connections on this level. + // Each entry in the list has the top maxConn neighbors of a node. The nodes correspond to vectors + // added to HnswBuilder, and the node values are the ordinals of those vectors. + // Thus, on all levels, neighbors expressed as the level 0's nodes' ordinals. + private final List<List<NeighborArray>> graph; // KnnGraphValues iterator members private int upto; private NeighborArray cur; - HnswGraph(int maxConn) { - graph = new ArrayList<>(); - // Typically with diversity criteria we see nodes not fully occupied; average fanout seems to be - // about 1/2 maxConn. There is some indexing time penalty for under-allocating, but saves RAM - graph.add(new NeighborArray(Math.max(32, maxConn / 4))); + HnswGraph(int maxConn, int levelOfFirstNode) { this.maxConn = maxConn; + this.numLevels = levelOfFirstNode + 1; + this.graph = new ArrayList<>(numLevels); + this.entryNode = 0; + for (int i = 0; i < numLevels; i++) { + graph.add(new ArrayList<>()); + // Typically with diversity criteria we see nodes not fully occupied; + // average fanout seems to be about 1/2 maxConn. + // There is some indexing time penalty for under-allocating, but saves RAM + graph.get(i).add(new NeighborArray(Math.max(32, maxConn / 4))); + } + + this.nodesByLevel = new ArrayList<>(numLevels); + nodesByLevel.add(null); // we don't need this for 0th level, as it contains all nodes + for (int l = 1; l < numLevels; l++) { + nodesByLevel.add(new int[] {0}); + } } /** - * Searches for the nearest neighbors of a query vector. + * Searches HNSW graph for the nearest neighbors of a query vector. * * @param query search query vector * @param topK the number of nodes to be returned - * @param numSeed the size of the queue maintained while searching, and controls the number of - * random entry points to sample * @param vectors vector values * @param graphValues the graph values. May represent the entire graph, or a level in a * hierarchical graph. * @param acceptOrds {@link Bits} that represents the allowed document ordinals to match, or * {@code null} if they are all allowed to match. - * @param random a source of randomness, used for generating entry points to the graph * @return a priority queue holding the closest neighbors found */ public static NeighborQueue search( float[] query, int topK, - int numSeed, RandomAccessVectorValues vectors, VectorSimilarityFunction similarityFunction, KnnGraphValues graphValues, - Bits acceptOrds, - SplittableRandom random) + Bits acceptOrds) throws IOException { + int size = graphValues.size(); + int queueSize = Math.min(topK, 2 * size); Review comment: Great comment, indeed it is enough just to use `topK`. Addressed in 7bce9f15daa225243c034c22ca9cb3d581e09fc5 -- 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