Skip to main content

Hybrid Search

vectlite supports dense vector search, sparse keyword search (BM25), and hybrid fusion of both.

Cosine similarity with automatic HNSW indexing. Small collections (<128 records) use exact search.

Python

results = db.search(query_embedding, k=10)

Node.js

const results = db.search(queryEmbedding, { k: 10 })

BM25-scored inverted index for keyword retrieval. Use sparse_terms() to generate term vectors from text.

Python

terms = vectlite.sparse_terms("How to configure SSO authentication")
results = db.search(sparse=terms, k=10)

Node.js

const terms = vectlite.sparseTerms('How to configure SSO authentication')
const results = db.search(null, { sparse: terms, k: 10 })

Hybrid Fusion

Combine dense and sparse search with linear combination or reciprocal rank fusion (RRF).

Linear Combination (default)

results = db.search(
query_embedding,
k=10,
sparse=vectlite.sparse_terms("SSO authentication"),
dense_weight=1.0,
sparse_weight=0.5,
fusion="linear",
)

Reciprocal Rank Fusion (RRF)

results = db.search(
query_embedding,
k=10,
sparse=vectlite.sparse_terms("SSO authentication"),
fusion="rrf",
rrf_k=60,
)
const results = db.search(queryEmbedding, {
k: 10,
sparse: vectlite.sparseTerms('SSO authentication'),
fusion: 'rrf',
rrfK: 60,
})

Inserting Sparse Vectors

Provide sparse terms when inserting to enable keyword search:

Python

db.upsert(
"doc1",
dense_embedding,
{"title": "Auth Setup", "text": "How to configure SSO..."},
sparse=vectlite.sparse_terms("How to configure SSO authentication"),
)

Node.js

db.upsert('doc1', denseEmbedding,
{ title: 'Auth Setup', text: 'How to configure SSO...' },
{ sparse: vectlite.sparseTerms('How to configure SSO authentication') }
)

Text Helpers

For convenience, upsert_text() and search_text() handle embedding generation and sparse term extraction in one call:

Python

def embed(text):
# Your embedding function (e.g., OpenAI, sentence-transformers)
return model.encode(text)

vectlite.upsert_text(db, "doc1", "Auth setup guide for SSO", embed, {"source": "docs"})
results = vectlite.search_text(db, "how to authenticate", embed, k=5)

Node.js

function embed(text) {
// Your embedding function
return model.encode(text)
}

vectlite.upsertText(db, 'doc1', 'Auth setup guide for SSO', embed, { source: 'docs' })
const results = vectlite.searchText(db, 'how to authenticate', embed, { k: 5 })