Package org.apache.lucene.util.hnsw
Class HnswGraph
java.lang.Object
org.apache.lucene.util.hnsw.HnswGraph
- Direct Known Subclasses:
OnHeapHnswGraph
Hierarchical Navigable Small World graph. Provides efficient approximate nearest neighbor search
for high dimensional vectors. See Efficient and robust
approximate nearest neighbor search using Hierarchical Navigable Small World graphs [2018]
paper for details.
The nomenclature is a bit different here from what's used in the paper:
Hyperparameters
beamWidthinHnswGraphBuilderhas the same meaning asefConstin the paper. It is the number of nearest neighbor candidates to track while searching the graph for each newly inserted node.maxConnhas the same meaning asMin the paper; it controls how many of theefConstneighbors are connected to the new node
Note: The graph may be searched by multiple threads concurrently, but updates are not thread-safe. The search method optionally takes a set of "accepted nodes", which can be used to exclude deleted documents.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classNodesIterator that accepts nodes as an integer array.static classNodes iterator based on set representation of nodes.static classIterator over the graph nodes on a certain level. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract intReturns graph's entry point on the top level *abstract HnswGraph.NodesIteratorgetNodesOnLevel(int level) Get all nodes on a given level as node 0th ordinals.abstract intmaxConn()returns M, the maximum number of connections for a node.intReturns max node id, inclusive.abstract intabstract intIterates over the neighbor list.abstract intReturns the number of levels of the graphabstract voidseek(int level, int target) Move the pointer to exactly the givenlevel'starget.abstract intsize()Returns the number of nodes in the graph
-
Field Details
-
UNKNOWN_MAX_CONN
public static final int UNKNOWN_MAX_CONN- See Also:
-
EMPTY
Empty graph value
-
-
Constructor Details
-
HnswGraph
protected HnswGraph()Sole constructor
-
-
Method Details
-
seek
Move the pointer to exactly the givenlevel'starget. After this method returns, callnextNeighbor()to return successive (ordered) connected node ordinals.- Parameters:
level- level of the graphtarget- ordinal of a node in the graph, must be ≥ 0 and <KnnVectorValues.size().- Throws:
IOException
-
size
public abstract int size()Returns the number of nodes in the graph -
maxNodeId
public int maxNodeId()Returns max node id, inclusive. Normally this value will be size - 1. -
nextNeighbor
Iterates over the neighbor list. It is illegal to call this method after it returns NO_MORE_DOCS without callingseek(int, int), which resets the iterator.- Returns:
- a node ordinal in the graph, or NO_MORE_DOCS if the iteration is complete.
- Throws:
IOException
-
numLevels
Returns the number of levels of the graph- Throws:
IOException
-
maxConn
public abstract int maxConn()returns M, the maximum number of connections for a node. -
entryNode
Returns graph's entry point on the top level *- Throws:
IOException
-
getNodesOnLevel
Get all nodes on a given level as node 0th ordinals. The nodes are NOT guaranteed to be presented in any particular order.- Parameters:
level- level for which to get all nodes- Returns:
- an iterator over nodes where
nextIntreturns a next node on the level - Throws:
IOException
-
neighborCount
public abstract int neighborCount()
-