redis_hnsw
is a Hierarchical Navigable Small World (HNSW) implementation for Redis. Based on the paper Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs. Currently only supports Euclidean distance, Hamming distance forthcoming.
⚠️ requires nightly rust
Build the module - cargo build
Load the module - redis-server --loadmodule ./target/<build_mode>/libredis_hnsw.<dylib|so>
Creating a new index - hnsw.new {index_name} [DIM {data_dim}] [M {m}] [EFCON {ef_construction}]
Add nodes - hnsw.node.add {index_name} {node_name} [DATA {dim} {...data}]
Delete nodes - hnsw.node.del {index_name} {node_name}
Search KNN - hnsw.search {index_name} [K {k}] [DATA {dim} {...data}]
HNSW.NEW {index} [DIM {data_dim}] [M {m}] [EFCON {ef_construction}]
Creates an HNSW index
HNSW.NEW foo DIM 128 M 5 EFCON 200
- index: required, name of the new index.
- DIM: required, dimensionality of the data.
- M: optional, algorithm parameter for the number of neighbors to select for each node.
- EFCON: optional, algorithm parameter for the size of the dynamic candidate list.
O(1)
OK or an error
HNSW.GET {index}
Retrieves an HNSW index
HNSW.GET foo
- index: required, name of the index.
O(1)
Array Reply key-value pairs of index attributes
HNSW.DEL {index}
Deletes an HNSW index
HNSW.DEL foo
- index: required, name of the index.
O(1)
OK or an error
HNSW.NODE.ADD {index} {node} [DATA {dim} {...data}]
Adds an element to the index
HNSW.NODE.ADD foo bar DATA 4 1.0 1.0 1.0 1.0
- index: required, name of the index
- node: required, name of the new node
- DATA: required, dimensionality followed by a space separated vector of data. Total entries must match
DIM
of index
O(log(n)) where n is the number of nodes in the index
OK or an error
HNSW.NODE.GET {index} {node}
Retrieves an element from the index
HNSW.NODE.GET foo bar
- index: required, name of the index
- node: required, name of the node
O(1)
Array Reply key-value pairs of node attributes
HNSW.NODE.DEL {index} {node}
Removes an element from the index
HNSW.NODE.DEL foo bar
- index: required, name of the index
- node: required, name of the node
O(log(n)) where n is the number of nodes in the index
OK or an error
HNSW.SEARCH {index} [K {k}] [QUERY {dim} {...data}]
Search the index for the K nearest elements to the query
HNSW.SEARCH foo K 5 QUERY 4 0.0 0.0 0.0 0.0
- index: required, name of the index
- K: required, number of nearest neighbors to return
- DATA: required, dimensionality followed by space separated vector of query data. Total entries must match
DIM
of index
O(log(n)) where n is the number of nodes in the index
Array Reply where the first element is the number of results, followed by key-value pairs of similiarity and returned node key.