QuantizedFlatIndex<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.VectorSearch.Indexes

A flat (brute-force) index that stores compressed quantized codes instead of full vectors, trading a small amount of accuracy for large memory savings and faster scanning. This mirrors FAISS IndexPQ/IndexScalarQuantizer and Qdrant’s on-disk quantized collections.

Implements: IVectorIndex<T>

For Beginners

This index keeps a tiny “summary” of each vector instead of the whole thing, so it uses far less memory. You still call Add and Search the same way; just remember to call Train once (on a representative sample) so it can learn how to summarize. Until then it behaves like an ordinary exact index.

How It Works

Vectors added before the underlying quantizer is trained are held in raw form and searched with an exact fallback (equivalent to a FlatIndex). Once Train is called, those vectors are encoded to compact codes, the raw copies are released, and subsequent additions are encoded immediately.

Ranking after training uses the quantizer to score candidates: for a ProductQuantizer paired with a distance metric it uses fast Asymmetric Distance Computation (ADC); otherwise it reconstructs each candidate with Decode and applies the supplied metric.

Constructors

Constructor Summary
QuantizedFlatIndex(ISimilarityMetric<T> metric, IVectorQuantizer<T> quantizer) Initializes a new instance of the QuantizedFlatIndex class.

Properties

Property Summary
IVectorQuantizer<T> Quantizer { get; } Gets the quantizer used to compress vectors.
bool IsTrained { get; } Gets a value indicating whether the underlying quantizer has been trained.
int Count { get; }

Methods

Method Summary
List<ValueTuple<string, T>> Search(Vector<T> query, int k)
bool Remove(string id)
void Add(string id, Vector<T> vector)
void AddBatch(Dictionary<string, Vector<T>> vectors)
void Clear()
void Train(IEnumerable<Vector<T>> vectors) Trains the underlying quantizer, then encodes and compacts any vectors that were added beforehand.