Sparse vector index
The sparse vector index (SparseVectorIndexConfig) strengthens keyword-oriented retrieval. Unlike dense vectors (for example BERT or OpenAI embeddings), which emphasize semantic similarity, sparse vectors (for example BM25 or SPLADE) emphasize exact keyword matching.
Use cases and benefits
- Exact keyword matching: For proper nouns, abbreviations, serial numbers, or domain terms, sparse vectors often match more accurately than dense vectors.
- Interpretability: Each dimension usually maps to a vocabulary token, so results are easier to explain.
- Out-of-domain generalization: Without domain-tuned dense models, statistical sparse models such as BM25 are often more robust.
- Hybrid search: Combining dense vectors for semantics and sparse vectors for exact matching often improves recall at K (Recall@K).
Field reference
SparseVectorIndexConfig configures sparse vector indexes such as BM25 and SPLADE and supports hybrid retrieval with dense vectors. Fields:
| Field | Type | Default | Description and constraints | Required |
|---|---|---|---|---|
| embeddingFunction | SparseEmbeddingFunction | None | Must support persistence (name, get_config, build_from_config) | No |
| sourceKey | SourceKey (Key | string | null) | — | Data source for sparse vectors. Default K.DOCUMENT ("#document"); or a metadata field name (such as "title"). | No |
| lib | string | "vsag" | Only vsag is supported | No |
| distance | string | "inner_product" | Sparse vectors support inner_product only | No |
| type | string | "sindi" | Only sindi is supported | No |
| prune | boolean | False | Whether pruning is enabled | No |
| refine | boolean | False | Whether refine is enabled | No |
| drop_ratio_build | number | 0.0 | Ratio of small weights ignored at index build time (0.0–0.9) | No |
| drop_ratio_search | number | 0.0 | Ratio of small weights ignored at search time (0.0–0.9); larger values are faster but may reduce accuracy | No |
| refine_k | number | 4.0 | Rerank candidate multiplier (1.0–1000.0) | No |
Constraints and behavior:
- At most one sparse vector index per collection; sparse vectors are stored in the
sparse_embeddingcolumn. sourceKeyofK.DOCUMENTor"#document"means vectors are built from document content; a plain string means vectors are built frommetadata[sourceKey].
Usage
To use a sparse vector index in seekdb-js, configure it through Schema when you create a collection.
Prerequisites
Install seekdb-js V1.2.0 or later.
npm install seekdb
For BM25, install the dependency.
npm install @seekdb/bm25
Usage example
-
The following example configures a sparse vector index with BM25 from
@seekdb/bm25.import { SeekdbClient, SparseVectorIndexConfig, K } from "seekdb";
import { Bm25EmbeddingFunction } from "@seekdb/bm25";
// 1. Initialize the client (embedded example; adjust for your backend)
const client = new SeekdbClient({
path: "./seekdb.db",
database: "test",
});
// 2. Define schema: BM25 sparse index, document content (K.DOCUMENT) as source
const sparseConfig = new SparseVectorIndexConfig({
embeddingFunction: new Bm25EmbeddingFunction({ k: 1.2, b: 0.75 }),
sourceKey: K.DOCUMENT, // or a metadata field such as "title"
drop_ratio_search: 0.2, // prune ratio at search time for speed
});
const schema = {
sparseVectorIndex: sparseConfig,
};
// 3. Create collection
const collectionName = "demo_sparse_collection";
const collections = await client.listCollections();
if (collections.includes(collectionName)) {
await client.deleteCollection(collectionName);
}
const collection = await client.createCollection({
name: collectionName,
schema,
});
// 4. Add data (seekdb-js generates sparse vectors with the configured BM25 function)
await collection.add({
ids: ["1", "2", "3"],
documents: [
"Machine learning is fascinating.",
"Python is a great programming language.",
"Artificial Intelligence and machine learning are related.",
],
});
// 5. Sparse vector query (queryKey: K.SPARSE_EMBEDDING selects the sparse index)
const results = await collection.query({
queryTexts: ["machine learning"],
queryKey: K.SPARSE_EMBEDDING,
nResults: 2,
});
console.log("Sparse Search Results:", results); -
Custom sparse vectors (for example SPLADE)
seekdb-js does not ship a Hugging Face or SPLADE implementation. For SPLADE or other deep sparse representations, implement the
SparseEmbeddingFunctioninterface yourself and pass it asembeddingFunctiononSparseVectorIndexConfig.