---
title: "Build an IVF vector search index in C#"
url: https://recursiveai.org/oneindex/cookbook/build-clustered-ivf-vector-index/
description: "Train an IVF index on local vectors with OneIndex. Open it in C#, query the nearest clusters, and see what changes when you probe more lists."
version: "0.4.0-preview.2"
---

# Build an IVF vector search index in C#

**OneIndex** is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneIndex) · [GitHub](https://github.com/recursive-ai-oss/oneindex-csharp) · [Docs](https://recursiveai.org/oneindex/docs/) · [Download example (.zip)](https://recursiveai.org/oneindex/cookbook/build-clustered-ivf-vector-index/build-clustered-ivf-vector-index.zip)

An inverted-file (IVF) index groups vectors into clusters so a query can scan just the nearest lists.

```shell
dotnet add package RecursiveAI.OneIndex --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneIndex.Storage;
using OneIndex.Vectors;

// Four vectors, two values per row.
float[] rows = [1, 0, 1, .1f, 0, 1, .1f, 1];
var storage = new MemoryStorage();
IvfBuilder.Write(storage, "demo.ivf", rows,
    dimensions: 2,
    options: new IvfOptions { Lists = 2 });
using var index = IvfIndex.Open(
    storage.OpenRead("demo.ivf"));
var hits = index.Search([1, 0],
    k: 3, nprobe: 1);
foreach (var hit in hits)
    Console.WriteLine($"Row {hit.Id}");
```

```text
Row 0
Row 1
```

Unzip [the example](https://recursiveai.org/oneindex/cookbook/build-clustered-ivf-vector-index/build-clustered-ivf-vector-index.zip), then `dotnet run`.

## The full program

The program uses OneIndex 0.4.0-preview.2. It indexes rows `(1, 0)`, `(1, 0.1)`, `(0, 1)` and `(0.1, 1)` in memory. Asking for three neighbors of `(1, 0)` returns only two rows with one list probed. With both lists probed, it also returns row 3.

```csharp
using OneIndex.Storage;
using OneIndex.Vectors;

float[] rows = [1, 0, 1, .1f, 0, 1, .1f, 1];
var storage = new MemoryStorage();
IvfBuilder.Write(storage, "demo.ivf", rows, 2,
    new IvfOptions { Lists = 2 });
using var index = IvfIndex.Open(
    storage.OpenRead("demo.ivf"));

float[] query = [1, 0];
foreach (int probes in new[] { 1, 2 })
{
    var hits = index.Search(query, k: 3,
        nprobe: probes);
    Console.WriteLine($"Lists probed: {probes}");
    foreach (var hit in hits)
        Console.WriteLine(FormattableString.Invariant(
            $"Row {hit.Id}: {hit.Score:F3}"));
}
```

```text
Lists probed: 1
Row 0: 1.000
Row 1: 0.995
Lists probed: 2
Row 0: 1.000
Row 1: 0.995
Row 3: 0.100
```

`IvfBuilder.Write` rejects a flat input that doesn’t divide evenly into rows of the specified dimension.

## Variations

**Probe both lists.** Change `nprobe` to `2` in the first snippet to also look in the other cluster:

```csharp
var hits = index.Search([1, 0],
    k: 3, nprobe: 2);
```

## Related recipes

[Vector search in .NET](https://recursiveai.org/oneindex/cookbook/vector-search-dotnet/) · [Publish a read-only static search index](https://recursiveai.org/oneindex/cookbook/publish-read-only-static-search-index/)

---

## The short answer

Flatten equal-length vectors into one `float[]`, then call `IvfBuilder.Write` with the dimension and number of lists. The write call trains the clusters and writes the index in one step. Open it with `IvfIndex.Open` and pass a query vector of the same dimension to `Search`. Each hit’s `Id` is the zero-based input row number, so keep your own mapping if you need to recover a catalog entry or other record.

## What is IVF in vector search?

IVF stands for inverted file. Each list holds the vectors assigned to one cluster center. Given a query, the search finds the closest centers, then scores the vectors in their lists. `nprobe` sets how many lists to scan; `k` sets how many hits to return at most. They solve different problems: increasing `k` alone won’t retrieve rows from a list that wasn’t searched.

In the example, row 3 is the third neighbor when you search both lists. It can’t appear when you probe only the first list, even though there’s a spare result slot. This is the tradeoff to check on your own data: if neighbors you care about fall in other lists, raise `nprobe` and compare the results against a full search. With full-precision storage and all lists probed, every row is considered and scored at full precision.

## How does IVF indexing work?

OneIndex uses k-means to train the centers from a sample of your input, then assigns every row to its nearest center. With cosine similarity, the default metric, the training uses spherical k-means, so the centers have unit length. The arrays in the example are already vectors; the builder does not train a text embedding model. When using text embeddings, generate the indexed and query vectors with compatible models and keep the dimensions consistent.

`IvfOptions.Lists` controls how many clusters the builder trains. If you omit it, OneIndex chooses about the square root of the number of rows, bounded from 1 to 4096. `IvfOptions.Storage` defaults to full precision for the standalone builder. The `EightBit` option scores compact codes first and re-scores the best candidates with full-precision vectors kept in the file. OneIndex also uses IVF for the vector layout in its static index, where lists can be read as contiguous byte ranges. See the [IVF reference](https://recursiveai.org/oneindex/docs/ivf/) for the storage modes and the separate per-field IVF option.
