Skip to main content
This tutorial covers every configuration lever in Actian VectorAI DB that affects how accurately similarity search returns relevant results. Getting search to work is straightforward. Getting it to return the right results consistently under real query load requires understanding the trade-offs between recall, precision, speed, and memory — and knowing which knobs to turn for each. Retrieval quality has two dimensions:
  • Recall — the fraction of truly relevant results that the system returns. If there are 10 relevant documents and the system finds 8, recall is 80%.
  • Precision — the fraction of returned results that are actually relevant. If the system returns 10 results and 7 are relevant, precision is 70%.
In approximate nearest-neighbour (ANN) search, there is always a trade-off between quality and speed. A brute-force scan over every vector gives perfect recall but is slow. An HNSW index is fast but may miss some neighbours. By the end of this tutorial, you will know how to:
  • Measure — Establish a ground truth baseline with exact search.
  • Tune HNSW — Adjust m, ef_construct, and hnsw_ef for the recall–speed trade-off.
  • Choose distance — Pick the right metric for your embeddings.
  • Tune search-time parameters — Use hnsw_ef and exact mode.
  • Use multistage prefetch — Widen the candidate pool then rerank.
  • Apply payload filters — Accelerate filtered search.
  • Set score thresholds — Cut noise at the right level.
  • Rebuild and compact — Keep index quality fresh after updates.

Environment setup

Run the following command to install the Python packages required for all code samples in this tutorial.

Step 1: Create a test collection and ingest data

This step sets up the shared imports, constants, and embedding helpers used throughout the tutorial. Running this block loads the all-MiniLM-L6-v2 model, defines two encoding functions, and establishes the server address and collection name that all subsequent steps reference.
The following block defines a 30-document corpus and ingests it into a new collection with cosine distance and default HNSW settings.

Expected output

This block embeds all 30 corpus documents, constructs PointStruct objects pairing each vector with its text and category payload, upserts them into the Retrieval-Quality collection using cosine distance and default HNSW settings (m=16, ef_construct=128), flushes the writes to disk, and queries the server for the total stored vector count.

Before tuning anything, measure ground truth. An exact (brute-force) search scans every vector in the collection and returns the mathematically correct nearest neighbours with 100% recall. Every tuning step in this tutorial should be measured against this baseline. The following block defines three functions: exact_search, which runs a brute-force scan; approx_search, which uses the HNSW index with an optional hnsw_ef override; and compute_recall, which calculates what fraction of the exact top-K results the approximate search also returned. Running the block then queries both functions for the same input and prints a side-by-side comparison. Every tuning step in this tutorial should be measured against this baseline.

Expected output

This block queries the same sentence using both exact_search (brute-force scan) and approx_search (HNSW traversal), then computes recall@10. When both result sets share identical IDs, recall reaches 100%, confirming the HNSW index is producing no approximation error on this query.

Step 3: Tune HNSW index parameters

The HNSW index has two sets of parameters: build-time parameters that affect the quality of the graph structure stored on disk, and search-time parameters that affect how many nodes the query traverses at runtime.

Build-time parameters: m and ef_construct

m and ef_construct are set when creating the collection. Once set, changing them requires recreating the index. The following block creates four collections at different quality levels and measures recall across all of them.
The following table summarizes how each parameter level affects build speed, memory, and the recall ceiling the index can reach. The two parameters control different aspects of graph quality:
  • m — The number of bi-directional links created for each node. Higher values produce a denser graph with more traversal paths, which improves recall at the cost of memory and build time.
  • ef_construct — The search width used during index construction. Higher values produce a better-connected graph. Set this to at least 2 * m.

Measure recall across configurations

Expected output

This block embeds the query, fetches exact ground-truth results from the baseline collection, then runs the same approximate search against each of the four HNSW collections and computes recall@10 for each. The low configuration uses a sparse graph that misses some traversal paths; default and above close that gap entirely.
All configurations return 100% recall on this small 30-doc corpus. Differences become visible at larger scale (1M+ vectors).

Search-time parameter: hnsw_ef

hnsw_ef controls how many candidate nodes the search explores at query time. It can be set per request without rebuilding the index, which makes it the primary knob for trading latency against recall at runtime.

Expected output

This block sweeps six values of hnsw_ef and for each measures recall against the exact baseline and wall-clock latency.
All hnsw_ef values return 100% recall on this small corpus. Latency differences are minimal; at larger scale higher ef values trade measurable latency for better recall.
The following table maps hnsw_ef ranges to their typical recall and latency characteristics.

Step 4: Choose the right distance metric

The distance metric defines what the index considers “similar”. Choosing the wrong metric for your embedding model produces systematically lower recall regardless of any other tuning.
The following table lists common embedding models and the distance metric each is designed to work with. Most pretrained embedding models produce unit-normalized vectors, where cosine similarity equals the dot product. If you are unsure which metric to use, start with cosine.

Expected output

Cosine and Dot return identical rankings and identical scores for all-MiniLM-L6-v2 because it produces unit-normalized vectors (cosine = dot product for unit vectors). Euclid returns distance values — lower is more similar, which inverts the ranking intuition.

Step 5: Use multistage prefetch to widen the candidate pool

A single HNSW traversal only explores one path through the graph. If the most relevant documents sit in a different region of the vector space — for example, in a specific category — that path may never reach them. Multi-stage prefetch runs several candidate-gathering passes in parallel, then reranks the combined pool.
The following table compares prefetch strategies by the size and composition of the candidate pool each one produces. The final limit=5 reranks from the union of all prefetched candidates. Even if one prefetch path misses a relevant result, another path may find it.

Expected output

The RRF fusion promotes id=6 (ml) from rank 3 to rank 2, reflecting that the ML-filtered prefetch stream also returned it as a top candidate — consensus across streams boosts its position.

Step 6: Set the right score threshold

A score threshold rejects any result whose similarity score falls below a minimum value. Setting it too low returns noisy, irrelevant results. Setting it too high discards valid matches. The right threshold depends on the score distribution of your specific embedding model and corpus.

Expected output

This block runs threshold_analysis with the query “machine learning and neural network training” and the known-relevant set {5, 6, 7, 8, 9, 10, 11}. It applies seven score thresholds and prints precision and recall at each level.
Reading this output:
  • At threshold 0.2 — 87.5% precision, 100% recall — good balanced starting point.
  • At threshold 0.3 — 100% precision, 71% recall — trades some recall for clean results.
  • At threshold 0.4+ — fewer than 3 results remain; too aggressive for this corpus.
Run this analysis on multiple representative queries and pick the threshold that balances precision and recall across your query set.

Step 7: Rebuild and compact for sustained quality

After many updates and deletions, index quality degrades over time. Deleted vectors leave tombstones that waste memory and slow search. Segments accumulate and fragment, reducing scan locality. The following block runs a full rebuild, optimization, and compaction sequence.

Expected output

The following table provides a schedule for each maintenance operation based on write and delete activity.

Step 8: Update HNSW config without rebuilding data

collections.update lets you change HNSW parameters on an existing collection without re-ingesting any data. This is useful when you start a project with conservative settings for fast iteration and want to raise quality before going to production.
Starting with low m and ef_construct values keeps build times fast during development. Increasing them before deployment raises the recall ceiling without requiring any data migration.

Retrieval quality checklist

The following tables summarize every lever available in Actian VectorAI DB for optimizing retrieval quality, grouped by when the parameter takes effect.

Collection-level settings (set once, rebuild to change)

These parameters are fixed at collection creation time. Changing them requires recreating the index. These parameters can be tuned on every search request without changing or rebuilding the index.

Operational maintenance (run periodically)

Run these operations on a schedule to keep retrieval quality from degrading as data changes over time.

Next steps