KaisarCode

hnsw.c

hnsw.c - HNSW Vector Search

A minimalist C library and CLI for fixed-dimension vector indexing with Approximate Nearest Neighbor search using a Hierarchical Navigable Small World (HNSW) graph.


CLI

Run a nearest neighbor search over a vector dataset.

Dataset format

Each line in the input file must contain:

<id> <v1> <v2> ... <vN>

Example (3D vectors):

item_1 1.0 0.0 0.0
item_2 0.0 1.0 0.0
item_3 0.0 0.0 1.0

Examples

Basic search:

./bin/x86_64/linux/hnsw --dim 3 --input vectors.txt --query "1 0 0"

Using a different metric and limiting results:

./bin/x86_64/linux/hnsw --dim 3 --input vectors.txt --query "1 0 0" --metric cosine --top 5

Applying a threshold:

./bin/x86_64/linux/hnsw --dim 3 --input vectors.txt --query "1 0 0" --threshold 0.8

Pipe query vector through standard input:

echo "1 0 0" | ./bin/x86_64/linux/hnsw --dim 3 --input vectors.txt

Parameters

FlagDescription
--dim, -dVector dimension
--input, -iInput dataset file
--query, -qQuery vector
--metric, -mMetric (l2, cosine, ip)
--top, -kNumber of results
--threshold, -tThreshold filter
--help, -hShow help
--version, -vShow version

Output

Results are printed as:

<id>: <score>

Metrics

Available metrics:

  • l2: squared Euclidean distance
  • cosine: cosine distance
  • ip: inner product similarity

l2 uses squared Euclidean distance:

d(a, b) = sum((a[i] - b[i])^2)

Note: no square root is applied. Rankings are identical to Euclidean distance.


Public API

#include "hnsw.h"

kc_hnsw_options_t opts = kc_hnsw_options_default();
opts.dimension = dimension;
opts.metric = KC_HNSW_METRIC_COSINE;

kc_hnsw_t *hnsw = kc_hnsw_open(&opts);
kc_hnsw_add(hnsw, "id_1", values);
kc_hnsw_build(hnsw);
kc_hnsw_search(hnsw, query, limit, threshold, results);
kc_hnsw_close(hnsw);

Lifecycle

  • kc_hnsw_options_default() returns default index options.
  • kc_hnsw_options_load_env() applies KC_HNSW_* environment overrides.
  • kc_hnsw_options_free() releases option resources.
  • kc_hnsw_open() allocates a new index from options.
  • kc_hnsw_add() inserts vectors.
  • kc_hnsw_build() constructs the HNSW graph.
  • kc_hnsw_search() queries the index.
  • kc_hnsw_close() releases all resources.

Build

Compiled artifacts are generated under bin/{arch}/{platform}/ for the host architecture running the build.

make clean && make

Multiarch Builds

The project is prepared to build artifacts for multiple architectures under bin/{arch}/{platform}/. A plain make builds only the current host architecture, while the targets below build the full matrix or a specific target.

make all
make x86_64/linux
make x86_64/windows
make i686/linux
make i686/windows
make aarch64/linux
make aarch64/android
make armv7/linux
make armv7/android
make armv7hf/linux
make riscv64/linux
make powerpc64le/linux
make mips/linux
make mipsel/linux
make mips64el/linux
make s390x/linux
make loongarch64/linux

Beta Notice

This is a beta project tested only on Debian x86_64. It was created out of a personal need for these libraries, but no guarantees are provided regarding its stability or future support. You are free to test it, use it, and modify it as you please.

If you'd like to reach out, you can send an email to [email protected]. Please note that I do not accept pull requests; the goal is to avoid long-term dependency on platforms like GitHub, and I do not maintain fixed infrastructure to guarantee long-term stability for these projects.


Repo

You can download the repository and read the most up-to-date documentation directly from its official source.

GitHub: kaisarcode/hnsw.c

License

GPLv3

This project is distributed under the GNU General Public License version 3 (GPLv3).