> ## Documentation Index
> Fetch the complete documentation index at: https://www.paradedb.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Indexing Vectors

> Vectors live alongside text and filters in the ParadeDB index

> [!NOTE]
>
> This is a beta feature available in versions `0.25.0` and above. See [How
> Vector Search Works](https://www.paradedb.com/docs/concepts/vector/overview.md) for background.

> [!NOTE]
>
> Make sure the [pgvector](https://github.com/pgvector/pgvector) extension is
> installed first. ParadeDB uses pgvector's vector types, but not its HNSW or
> IVF indexes.

The ParadeDB index can index pgvector's `vector` type alongside your text and other columns. This lets you combine vector search with full text search and filters in a single index,
which can significantly improve latency/recall for selective queries.

## Creating the Index

The `mock_items` table comes with an `embedding` column of type `vector(8)` populated with sample embeddings.
In this example, `embedding` is added to the ParadeDB index with cosine similarity as the distance function.

```sql SQL theme={null}
CREATE INDEX search_idx ON mock_items
USING paradedb (id, description, category, embedding vector_cosine_ops);
```

```ts Drizzle theme={null}
import { indexing } from "@paradedb/drizzle-paradedb";

// In the pgTable definition:
(table) => [
  indexing
    .paradedbIndex("search_idx")
    .on(
      table.id,
      table.description,
      table.category,
      indexing.vectorField(table.embedding, "cosine"),
    ),
];
```

```python Django theme={null}
from django.db import connection
from paradedb.indexes import ParadeDBIndex

with connection.schema_editor() as schema_editor:
    schema_editor.add_index(
        MockItem,
        ParadeDBIndex(
            fields={
                "id": {},
                "description": {},
                "category": {},
                "embedding": {"metric": "cosine"},
            },
            name="search_idx",
        ),
    )
```

```python SQLAlchemy theme={null}
from sqlalchemy import Index
from paradedb.sqlalchemy import indexing

idx = Index(
    "search_idx",
    indexing.ParadeDBField(MockItem.id),
    indexing.ParadeDBField(MockItem.description),
    indexing.ParadeDBField(MockItem.category),
    indexing.VectorField(MockItem.embedding, metric="cosine"),
    postgresql_using="paradedb",
)

with engine.begin() as conn:
    idx.create(conn)
```

```ruby Rails theme={null}
ActiveRecord::Base.connection.add_paradedb_index(
  :mock_items,
  fields: {
    id: {},
    description: {},
    category: {},
    embedding: { metric: :cosine }
  },
  name: :search_idx
)
```

```cs EF Core theme={null}
modelBuilder.Entity<MockItem>()
    .HasParadeDbIndex("search_idx", e => e.Id)
    .HasField(e => e.Description)
    .HasField(e => e.Category)
    .HasField(e => e.Embedding, VectorMetric.Cosine);
```

The desired distance function is encoded into the index definition, and cannot be changed without reindexing.

```sql SQL theme={null}
embedding vector_l2_ops      -- L2 (default)
embedding vector_cosine_ops  -- cosine
embedding vector_ip_ops      -- inner product
```

```ts Drizzle theme={null}
indexing.vectorField(mockItems.embedding, "l2"); // L2 (default)
indexing.vectorField(mockItems.embedding, "cosine"); // cosine
indexing.vectorField(mockItems.embedding, "ip"); // inner product
```

```python Django theme={null}
"embedding": {"metric": "l2"}      # L2 (default)
"embedding": {"metric": "cosine"}  # cosine
"embedding": {"metric": "ip"}      # inner product
```

```python SQLAlchemy theme={null}
indexing.VectorField(MockItem.embedding, metric="l2")      # L2 (default)
indexing.VectorField(MockItem.embedding, metric="cosine")  # cosine
indexing.VectorField(MockItem.embedding, metric="ip")      # inner product
```

```ruby Rails theme={null}
embedding: { metric: :l2 }      # L2 (default)
embedding: { metric: :cosine }  # cosine
embedding: { metric: :ip }      # inner product
```

```cs EF Core theme={null}
.HasField(e => e.Embedding, VectorMetric.L2)            // L2 (default)
.HasField(e => e.Embedding, VectorMetric.Cosine)        // cosine
.HasField(e => e.Embedding, VectorMetric.InnerProduct)  // inner product
```

> [!NOTE]
>
> Only pgvector's `vector` type is supported. The `halfvec`, `sparsevec`, and
> `bit` types are not yet indexable. Each ParadeDB index currently supports one
> vector field, whose dimensions must be declared with `vector(N)`.

> [!NOTE]
>
> If you track index build progress with
> [`pg_stat_progress_create_index`](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING),
> you may notice progress appear to "stop" at intervals. This is expected:
> vectors are clustered with k-means at these points, which is computationally
> expensive.

## Index Options

ParadeDB uses a SPANN-style vector index, which is similar to an IVF index but with additional structures to improve
recall and latency, especially over large datasets. The following `WITH` options control how vectors are clustered and apply to the vector field in the index. They are read each time a segment is clustered: during `CREATE INDEX` and `REINDEX`, and whenever a merge writes a segment with at least `paradedb.vector_clustering_threshold` documents. Changing them with `ALTER INDEX ... SET` affects only segments clustered afterwards; run `REINDEX` to recluster existing segments. An example:

```sql SQL theme={null}
CREATE INDEX search_idx ON mock_items
USING paradedb (id, description, category, embedding vector_cosine_ops)
WITH (training_sample_ratio=0.32, max_leaf_size=200);
```

```ts Drizzle theme={null}
import { indexing } from "@paradedb/drizzle-paradedb";

indexing
  .paradedbIndex("search_idx", { trainingSampleRatio: 0.32, maxLeafSize: 200 })
  .on(
    mockItems.id,
    mockItems.description,
    mockItems.category,
    indexing.vectorField(mockItems.embedding, "cosine"),
  );
```

```python Django theme={null}
from django.db import connection
from paradedb.indexes import ParadeDBIndex

with connection.schema_editor() as schema_editor:
    schema_editor.add_index(
        MockItem,
        ParadeDBIndex(
            fields={'id': {}, 'description': {}, 'category': {}, 'embedding': {'metric': 'cosine'}},
            name="search_idx",
            training_sample_ratio=0.32,
            max_leaf_size=200,
        ),
    )
```

```python SQLAlchemy theme={null}
from sqlalchemy import Index
from paradedb.sqlalchemy import indexing

idx = Index(
    "search_idx",
    indexing.ParadeDBField(MockItem.id),
    indexing.ParadeDBField(MockItem.description),
    indexing.ParadeDBField(MockItem.category),
    indexing.VectorField(MockItem.embedding, metric="cosine"),
    postgresql_using="paradedb",
    postgresql_with={'training_sample_ratio': 0.32, 'max_leaf_size': 200},
)

with engine.begin() as conn:
    idx.create(conn)
```

```ruby Rails theme={null}
ActiveRecord::Base.connection.add_paradedb_index(
  :mock_items,
  fields: {
    id: {},
    description: {},
    category: {},
    embedding: { metric: :cosine },
  },
  name: :search_idx,
  index_options: { training_sample_ratio: 0.32, max_leaf_size: 200 }
)
```

```cs EF Core theme={null}
modelBuilder.Entity<MockItem>()
    .HasParadeDbIndex("search_idx", e => e.Id)
    .HasField(e => e.Description)
    .HasField(e => e.Category)
    .HasField(e => e.Embedding, VectorMetric.Cosine)
    .HasTrainingSampleRatio(0.32)
    .HasMaxLeafSize(200);
```

**training\_sample\_ratio** (default: 0.32)

The fraction of indexed vectors sampled for training whenever a segment is
clustered. Must be between `0.000001` and `1.0`. A value of `1.0` trains on
every vector. The default samples 32% of the vectors. Larger samples can
improve centroid quality at the cost of a slower, more memory-intensive build.

**max\_leaf\_size** (default: 200)

The maximum number of sampled training vectors in a leaf of the hierarchical
clustering tree. Must be an integer between `1` and `2147483647`. Smaller
values generally produce more centroids and smaller clusters. The actual
centroid count depends on the clustering result. This setting is independent
of `training_sample_ratio` and does not cap the number of indexed vectors
assigned to a cluster.

**vector\_router\_quantization** (default: true)

Route each query through one-bit codes of the IVF centroids, then rerank the
shortlisted centroids exactly, instead of scoring every centroid in full
precision. Set it to `false` to keep full-precision routing. Applies to
segments clustered after the option is set; existing segments keep their
router until the index is rebuilt.

> [!NOTE]
>
> These options replace `centroid_ratio` and `training_samples_per_centroid`.
> The old names are no longer accepted. Recreate indexes that explicitly store
> either old option using the new options. `REINDEX` alone does not remove
> obsolete index options. The new defaults sample 32% of vectors and use a
> maximum training leaf size of 200.

**quantization** (default: \[1,1])

A developer setting that we generally do not recommend tuning. ParadeDB uses
two quantization layers by default, each receiving 1 bit per dimension. Each
layer can use up to 4 bits. Set `quantization = 0` to disable quantization
entirely. In our testing, the default `'[1,1]'` delivers the best performance.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.