NGT-store
A vector store with metadata filtering built on NGT. It provides a C++17 library and Python bindings through pybind11. It is designed to be used as a LlamaIndex vector store.
- Choose among NGT's graph / quantized graph index types (Graph / QG / QBG).
- Metadata is stored in SQLite, and filter conditions are applied before the NGT search.
- Distance is selected at creation time from L2 / Cosine / NormalizedCosine / NormalizedL2.
- There is no C API and no generic vector-store abstraction. NGT-specific by design.
Design details and decisions are documented in DESIGN.md (Japanese).
Prerequisites
- A C++17 compiler and CMake 3.16 or newer
- SQLite3
- NGT (v2.8.3 or newer — the filter API is included in official upstream) and
BLAS
- Point
NGT_ROOTat your NGT installation. Do not hardcode paths into the code. WhenNGT_ROOTis unset, NGT is built from thethird_party/ngtsubmodule (the routepip install .takes). - For building NGT, see 「NGT の利用形態」in DESIGN.md (Japanese).
- Point
- For the Python bindings only: Python 3.9 or newer and pybind11
Building
Without NGT (metadata layer only):
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build --parallel
ctest --test-dir build --output-on-failure
With NGT:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Debug \
-DNGTSTORE_WITH_NGT=ON \
-DNGT_ROOT="$PWD/.deps/ngt-filter"
cmake --build build --parallel
ctest --test-dir build --output-on-failure
Including the Python bindings:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Debug \
-DNGTSTORE_WITH_NGT=ON \
-DNGT_ROOT="$PWD/.deps/ngt-filter" \
-DNGTSTORE_BUILD_PYTHON=ON
cmake --build build --parallel
ctest --test-dir build --output-on-failure
From the build tree you can import with PYTHONPATH=build/python:
PYTHONPATH=build/python python3 -c 'import ngtstore; print(ngtstore.__version__)'
Installation
The package can be installed as usual. NGT (v2.8.3) is built from the
third_party/ngt submodule and statically linked, so no system NGT is
required:
git clone --recursive https://github.com/masajiro/NGT-store
pip install .
Building needs a C++17 compiler, CMake 3.16 or newer, and BLAS/LAPACK
(cmake / ninja / pybind11 are fetched by pip automatically). The
NGT_ROOT-based developer flow above instead requires the system NGT shared
library at run time.
For the LlamaIndex integration:
pip install ".[llama-index]"
Distributed wheels and variants
The wheels distributed on PyPI are prebuilt (generated by CI).
- x86_64: a single wheel bundles three extension modules
(
_ngtstore_avx512,_ngtstore_avx2, and the SIMD-disabled generic_ngtstore_base). At import time the CPU is probed and the best supported variant is chosen: AVX-512 only when the CPU covers the whole x86-64-v4 set (AVX-512F/BW/CD/DQ/VL), otherwise AVX2, otherwise the generic build. - aarch64: a single wheel (this NGT logically disables SIMD on ARM64, so one variant suffices).
- Every other environment builds from the sdist, where NGT compiles with the
default
-march=nativeand is therefore optimized for the build machine.
The wheel-build variant is selected with NGTSTORE_SIMD_VARIANT
(single / base / avx2 / avx512, default single). base, avx2,
and avx512 are fat-wheel build options that switch the vendored NGT's (when
NGT_ROOT is unset) compile flags and module name. The avx512 variant
targets the x86-64-v4 baseline and lets the compiler auto-vectorize with
AVX-512 (upstream NGT has no AVX-512 kernels of its own).
Python quick start
from ngtstore import (
DistanceType,
IndexOptions,
IndexType,
NgtStore,
all_of,
eq,
gt,
)
options = IndexOptions()
options.type = IndexType.GRAPH
options.distance_type = DistanceType.COSINE
store = NgtStore.create("./my-store", dimension=4, options=options)
try:
store.add("n1", [1.0, 0.0, 0.0, 0.0], {"category": "book", "price": 1000})
store.add_many(
[
("n2", [0.9, 0.1, 0.0, 0.0], {"category": "book", "price": 2000}),
("n3", [0.0, 1.0, 0.0, 0.0], {"category": "music", "price": 1500}),
]
)
hits = store.search(
[1.0, 0.0, 0.0, 0.0],
5,
filter=all_of([eq("category", "book"), gt("price", 500)]),
)
for hit in hits:
print(hit.node_id, hit.distance, hit.metadata)
store.save()
finally:
store.close()
Reopening:
with NgtStore.open("./my-store", read_only=True) as store:
print(store.dimension, store.index_type, store.distance_type, store.live_count)
Creation options
IndexOptions may only be specified at creation time and is stored in
store_info. open() takes no options.
options = IndexOptions()
options.type = IndexType.QBG
options.distance_type = DistanceType.NORMALIZED_COSINE
options.qbg.number_of_blobs = 16
options.qbg.filter_expansion = 8
Options can also be created from JSON:
from ngtstore import parse_index_options
options = parse_index_options(
'{"type": "qbg", "distance_type": "cosine", '
'"qbg": {"number_of_blobs": 16}}'
)
options.type is one of GRAPH / QG / QBG, and options.distance_type
is one of L2 / COSINE / NORMALIZED_COSINE / NORMALIZED_L2. The
defaults are GRAPH and L2.
Filters
Filters are assembled with Python functions.
from ngtstore import all_of, any_of, contains, eq, ge, in_, is_empty, ne, not_
f = all_of(
[
eq("category", "book"),
in_("language", ["ja", "en"]),
not_(is_empty("tags")),
any_of([contains("tags", "ai"), ne("status", "archived")]),
]
)
The operators are ==, !=, >, >=, <, <=, in, nin, contains,
text_match (case-sensitive), text_match_insensitive (case-insensitive),
and is_empty. Nest them with all_of / any_of / not_.
not_is the complement over the whole set, including rows that lack the key.is_emptymatches missing keys, JSONnull, empty arrays, and empty strings.- Filters are applied before the search, so if fewer than
kobjects match, the result has fewer thankentries.
Restricting the search targets
hits = store.search([1.0, 0.0, 0.0, 0.0], 10, node_ids=["n1", "n2"])
node_ids=[] raises ValueError. To not restrict, pass node_ids=None (or
omit it). Unknown node IDs are ignored.
Search strategies
A filtered search can choose how it runs via strategy.
hits = store.search(query, 10, filter=filters, strategy="auto")
"auto"(default): when the objects matching the filter (plus thenode_idsrestriction) number 10,000 or fewer, compute exact distances by brute force over the matches; otherwise run a filtered graph search. With low selectivity, graph traversal converges poorly and gets slow, so brute force is both faster and exact."inline": always the filtered graph search (bitmap checks during traversal)."exact": always brute force."post": ignores the filter, gatherspost_candidates(default 1000) candidates via graph search, and then matches them against the metadata. Skipping bitmap generation pays off the higher the match ratio is (1M benchmark: at a 15% match ratio, recall 0.996 and about 20x faster; at 1.4%, recall 0.71; at 0.015%, recall 0.015). Exactness is not guaranteed, and if fewer thankof the candidates match, the result has fewer thankentries. Use this only for queries known to have a high match ratio.
Unfiltered searches ignore the strategy and use the graph search. Brute-force and graph search results agree except for the ordering of ties.
Updates and fetches
# Replace an existing node, or add a new one.
store.upsert("n1", [0.0, 0.0, 1.0, 0.0], {"category": "doc"})
# Replace multiple existing nodes. QG rebuilds at most once.
store.upsert_many(
[
("n2", [0.0, 0.0, 0.0, 1.0], {"category": "doc"}),
("n4", [0.5, 0.5, 0.0, 0.0], {"category": "book"}),
]
)
node = store.get_node("n1") # None if unknown
nodes = store.get_nodes(["n2", "n1", "missing"]) # input order, unknowns skipped
page = store.scan(eq("category", "book"), offset=0, limit=10) # node ID order
upsert_many validates everything up front. However, if an insertion after a
removal fails, the pre-replacement data is not restored. Duplicate node IDs
within a batch are an error.
Node has node_id / vector / metadata / metadata_json. vector is
what NGT stores (normalized for normalized distances).
Saving and clearing
store.is_dirty # whether unsaved changes or a pending build exist
store.flush() # alias of save()
store.clear() # delete all objects and persist the empty state
clear() keeps the dimension, index type, distance type, and creation
options.
Removal
store.remove_by_node_id("n1") # unknown IDs are an error
removed = store.remove_by_node_ids(["n2", "n3"]) # unknown IDs are ignored
removed = store.remove_by_filter(eq("category", "book"))
QG rebuilds the whole index on removal; batch removals rebuild it only once.
QBG removals remain as tombstones, so object_count does not decrease.
Check live_count for the number of live entries.
Index types
| Type | Characteristics |
|---|---|
Graph |
The standard NGT graph. The most straightforward for adds, removes, and filtered search. |
QG |
Quantized graph. Unfiltered search is quantized; filtered search uses the base graph. Removals rebuild everything. |
QBG |
Quantized blob graph. For large scales. Filters apply inside the two-step search. Removals are tombstones. |
Distance types
| Value | distance in search results (smaller is closer) |
|---|---|
L2 |
Euclidean distance |
Cosine |
1 - cos(a, b) |
NormalizedCosine |
1 - dot(a, b) (L2-normalized at insert and search) |
NormalizedL2 |
sqrt(2 - 2 * dot(a, b)) (L2-normalized at insert and search) |
Cosine / NormalizedCosine / NormalizedL2 reject zero vectors. QBG's
Cosine is stored internally as NormalizedCosine.
LlamaIndex integration
ngtstore itself does not depend on llama_index. Two optional modules for
LlamaIndex are provided.
ngtstore.llama_index— filter translation and score conversion (nollama_indexrequired)ngtstore.llama_index_store— an adapter implementingBasePydanticVectorStore(requiresllama-index-core)
Using the adapter
from llama_index.core import StorageContext, VectorStoreIndex
from ngtstore import NgtStore
from ngtstore.llama_index_store import NgtVectorStore
store = NgtStore.open("./my-store")
adapter = NgtVectorStore(store)
storage = StorageContext.from_defaults(vector_store=adapter)
index = VectorStoreIndex(nodes, storage_context=storage)
Use StorageContext.from_defaults(vector_store=adapter). The
VectorStoreIndex(..., vector_store=adapter) keyword is ignored.
The adapter stores ref_doc_id as the doc_id metadata key, so
query.doc_ids is searched as an in condition on doc_id, and
delete(ref_doc_id) removes by the same key. The key can be changed with
NgtVectorStore(store, doc_id_key="source_id").
Because stores_text=False, node text is not stored in NGT-store.
Persistence is delegated to adapter.persist() (which calls store.save()).
Using only the helpers
from ngtstore.llama_index import distance_to_score, filters_from_llama_index
filters = filters_from_llama_index(
metadata_filters,
doc_ids=query.doc_ids, # optional; empty means no restriction
)
hits = store.search(query_embedding, top_k, filter=filters)
similarities = [
distance_to_score(hit.distance, store.distance_type) for hit in hits
]
filters_from_llama_index() reads MetadataFilters / MetadataFilter by
duck typing. It supports ==, !=, >, >=, <, <=, in, nin,
contains, text_match, text_match_insensitive, any, all, and
is_empty; unknown operators raise NotImplementedError.
C++ quick start
#include <iostream>
#include "ngtstore/ngt_store.h"
int main() {
using namespace ngtstore;
IndexOptions options;
options.type = IndexType::Graph;
options.distanceType = DistanceType::Cosine;
NgtStore::create("./my-store", 4, options);
NgtStore store;
store.open("./my-store", false);
store.add("n1", {1.0f, 0.0f, 0.0f, 0.0f}, R"({"category":"book","price":1000})");
store.add("n2", {0.0f, 1.0f, 0.0f, 0.0f}, R"({"category":"music","price":1500})");
const Filter filter = allOf({eq("category", Value("book"))});
for (const SearchHit& hit : store.search({1.0f, 0.0f, 0.0f, 0.0f}, 5, filter)) {
std::cout << hit.nodeId << " " << hit.distance << "\n";
}
store.save();
store.close();
}
Using it from CMake:
add_subdirectory(path/to/NGT-store)
target_link_libraries(your_target PRIVATE ngtstore_ngt)
ngtstore is the metadata layer only; ngtstore_ngt includes the NGT
connection layer. Using the connection layer requires
-DNGTSTORE_WITH_NGT=ON -DNGT_ROOT=....
The public API centers on include/ngtstore/ngt_store.h and
include/ngtstore/filter.h. NgtIndex / SqliteMetadataStore are internal.
Tests
ctest --test-dir build --output-on-failure
To run only the Python tests:
PYTHONPATH=build/python python3 -m unittest discover -s tests/python -v
Tests against a real llama-index-core run in an environment with LlamaIndex
installed:
PYTHONPATH=build/python /path/to/venv/bin/python \
-m unittest tests.python.test_llama_index -v
Constraints
- A directory cannot be opened by multiple
NgtStoreinstances at the same time. - Concurrent searches on a single instance are not supported. Different stores can be used concurrently.
- Call
save()explicitly.close()does not save. add()does not return the NGT Object ID. The user-facing key isnode_id.add()errors on a duplicatenode_id. Useupsert()/upsert_many()to replace.- QBG only reranks the candidates found by quantized search, so recall depends on the quantized search.
License
Apache License 2.0 — see LICENSE. The vendored NGT
(third_party/ngt submodule) is also licensed under the Apache License 2.0.
Metadata
Release files for ngtstore 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ngtstore-0.1.0.tar.gz | 2.5 MB | Without provenance | Details for ngtstore-0.1.0.tar.gz |
Built distributions (wheels)
Total release size: 125.5 MB
Release files / ngtstore-0.1.0.tar.gz
| Download URL | ngtstore-0.1.0.tar.gz |
|---|---|
| Size | 2.5 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c05d8b5399291c38956f313154d1eb7b0a59a5ae4dadaf026251f29e0d82900
|
|
BLAKE2b-256 checksum How to use checksums |
bf2f72bdd956de2337e9d90d97442bd9621aea35921cbeb1e58a03ebe773e904
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl
| Download URL | ngtstore-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 17.4 MB |
| Tags | CPython 3.13 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
99a74cae9d0777ccf79dda30666b3c4a2240b85045359549c89fcb58e161f8c1
|
|
BLAKE2b-256 checksum How to use checksums |
b7e1a11c4d78f47d986d329a1fcd4fad0af0662820d57facc955b3db1ee2d3af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp313-cp313-manylinux_2_28_aarch64.whl
| Download URL | ngtstore-0.1.0-cp313-cp313-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.13 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
a0eb85f28d7ff48ecea151b4300790d9f502e7533fa9a4904cdb31e5c8ee4275
|
|
BLAKE2b-256 checksum How to use checksums |
255568a8f051a59a55d4b46759d847bac456b588a2c96ca59df9a3e52eda6c44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl
| Download URL | ngtstore-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 17.4 MB |
| Tags | CPython 3.12 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
a870e745d3230e892e32db06dde8146e62169e3cea09859f064bd4a36fa223f3
|
|
BLAKE2b-256 checksum How to use checksums |
07bab044ee5573c10994140f3af78d2cb9c0f15024d1b4ad318b6ac6dd23973a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp312-cp312-manylinux_2_28_aarch64.whl
| Download URL | ngtstore-0.1.0-cp312-cp312-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.12 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
23dbfdf02b01b440d6454f6afb199cc54ba45b7287a0244e7968febb89d05e17
|
|
BLAKE2b-256 checksum How to use checksums |
4022fac7c939a837d1331ac953abfe65a948a7c9526757757c0d27598b64ed8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl
| Download URL | ngtstore-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 17.4 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
7f7a366f7b09b6090aa08a687642ffc0f34f1cd97ef6cad92c74af7be4017eee
|
|
BLAKE2b-256 checksum How to use checksums |
a9779b0204aa51cd0d126b89d6869617e2b9efc34cf424f6ae91d496b5c58b79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp311-cp311-manylinux_2_28_aarch64.whl
| Download URL | ngtstore-0.1.0-cp311-cp311-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
85f07100c53b69866514e711cc6b3c71ee7fce48cb2d51deb0f510411fa43951
|
|
BLAKE2b-256 checksum How to use checksums |
5868b8853c2671505f7f1f2517712d2005355d40f155023a519f142dd9d19709
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl
| Download URL | ngtstore-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 17.4 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
39eeb1794d732b0d1b2022ad7e142ef3ee3a67d2a2f77fba9c2fd66662ae7330
|
|
BLAKE2b-256 checksum How to use checksums |
154c155693f9d1d93062589bb45bc1cbf76aae748b31fa9276e43e8a1e6807c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp310-cp310-manylinux_2_28_aarch64.whl
| Download URL | ngtstore-0.1.0-cp310-cp310-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
dae195fbdede456fb7cde95dd79a61ef7fa8ea0e75bde27219d7e8388d29e00d
|
|
BLAKE2b-256 checksum How to use checksums |
8b0fac57a50d2e784714444cb36e301568e23f7bd3ba4ed61a0f34352283a646
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl
| Download URL | ngtstore-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 17.4 MB |
| Tags | CPython 3.9 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
52c580c6e05e00b8c7fa68bde60a59d927ebf1c029abeb9313685fa7c17f1478
|
|
BLAKE2b-256 checksum How to use checksums |
a959dac01d29802a5aea8ff9c56fa34eb5074de62e50c688c18f794b17e24f2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ngtstore-0.1.0-cp39-cp39-manylinux_2_28_aarch64.whl
| Download URL | ngtstore-0.1.0-cp39-cp39-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 7.2 MB |
| Tags | CPython 3.9 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
693baf2297aea75c58b546490a0f974bb1a801de25ee70d6b3b77612496f2555
|
|
BLAKE2b-256 checksum How to use checksums |
4bcfcbdac8abd76d65a5f8279d65b37500bc18fff460939beafc6e5d903c3c95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for ngtstore 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.