-
Notifications
You must be signed in to change notification settings - Fork 0
Vector Search
Nodes may carry a vector field; the property graph's
vectors property indexes them for similarity search, and
GraphQuery/PersistedGraphQuery can rank or filter by
similarity.
-
new VectorIndex(onChange?, distanceFn?)— an exact in-memory index.add,addMany,remove,removeMany,clear,has,get,entries,sizemanage vectors.add/addManyinvokeonChangefor each id;hydrate(id, vector)sets a vector without triggeringonChange, for restoring persisted state without marking it dirty again.query(vector, topK, threshold?)returns{ id, score }[], highest first. -
cosineSimilarity(a, b)andeuclideanSimilarity(a, b)are built in. All compared vectors must have identical dimensions, or similarity functions throwRangeError. Zero vectors have cosine similarity0. Added vectors are copied;get()/entries()return detached arrays. -
HNSWIndex(new HNSWIndex(onChange?, distanceFn?, config?, rng?)) is an approximate, update-safe alternative implementing the sameVectorIndexLikesurface.configacceptsM,Mmax0,efConstruction,efSearch(all optional, cosine distance by default — each must be a positive integer or the constructor throwsRangeError).
Swap the engine backing a graph's vectors property via the constructor's
createVectorIndex hook — it accepts anything implementing
VectorIndexLike (VectorIndex, HNSWIndex, or the native engines from
@0xx0lostcause0xx0/polypack-native):
import { PolyGraph, HNSWIndex } from '@0xx0lostcause0xx0/polypack'
const graph = new PolyGraph(undefined, undefined, undefined, undefined, (onChange) => new HNSWIndex(onChange))-
EmbeddingProviderdefinesembed(text)(sync or async, returning a number array,Float32Array, orFloat64Array) and an optionaldimensions. -
FeatureHashEmbeddingis the default, dependency-free provider — deterministic, normalized 384-dimensional lexical vectors, no model download. It captures shared words rather than learned semantic meaning; use a model-backed provider when synonyms and deeper language relationships matter. Its dimensions are configurable in the constructor. -
Pass a custom provider as the third
PolyGraphconstructor argument:const provider = { dimensions: 768, async embed(text: string) { return model.embed(text) }, } const graph = new PolyGraph(adapter, 10_000, provider)
-
embed(text)— generates and validates a detachedFloat64Array. -
addNodeWithEmbedding(node, text)— adds a node using generated text features. -
updateNodeWithEmbedding(id, data, text)/updateNodeSafeWithEmbedding(id, data, text)— regenerate a node vector. -
queryText/queryPersistedText— see Query builder.
Keep one provider and dimensionality per vector index — similarity comparisons reject vectors with mismatched dimensions.
buildEmbeddingText(data, weights?) repeats fields according to weight
(default 1) so the feature-hash bag-of-words embedding treats
higher-weighted fields as more significant:
import { buildEmbeddingText } from '@0xx0lostcause0xx0/polypack'
buildEmbeddingText({ subject: 'Hello', content: 'World' }, { subject: 3 })
// => "Hello Hello Hello World"Back to Home.
polypack
By feature
- Property graph
- Query builder
- Vector search & embeddings
- Persistence
- Database core
- Schema migrations
- Adaptive memory
- Real-time sync
- React integration
Related projects
In the repo