> ## 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.

# Vector

> The ParadeDB index is designed for efficient filtered vector search

> [!NOTE]
>
> We strongly recommend **`0.27.0`** or later for substantial vector search
> improvements.

ParadeDB provides `pdb.vector(N)` for single-precision float vectors.

```sql SQL theme={null}
SELECT '[1,2,3]'::pdb.vector;
```

```ts Drizzle theme={null}
import { sql } from "drizzle-orm";

await db.execute(sql`SELECT '[1,2,3]'::pdb.vector`);
```

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

with connection.cursor() as cursor:
    cursor.execute("SELECT '[1,2,3]'::pdb.vector")
```

```python SQLAlchemy theme={null}
from sqlalchemy import text

with engine.connect() as conn:
    conn.execute(text("SELECT '[1,2,3]'::pdb.vector"))
```

```ruby Rails theme={null}
ActiveRecord::Base.connection.execute("SELECT '[1,2,3]'::pdb.vector")
```

```cs EF Core theme={null}
await dbContext.Database.ExecuteSqlRawAsync("SELECT '[1,2,3]'::pdb.vector");
```

To index vectors, simply add them to the ParadeDB index alongside other fields.

```sql SQL theme={null}
CREATE INDEX search_idx ON mock_items
USING paradedb (id, description, category, embedding pdb.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 pdb.vector_l2_ops      -- L2 (default)
embedding pdb.vector_cosine_ops  -- cosine
embedding pdb.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]
>
> 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),
> the `phase` column identifies partition sampling, vector partition training,
> and vector clustering. It is normal for row counters to pause during training
> and clustering, which can be computationally expensive.

## Compatibility with `pgvector` Types

> [!NOTE]
>
> Starting in `0.27.2`, `pgvector` is optional. ParadeDB does not recommend
> using `pgvector`'s `hnsw` or `ivf` indexes because they [do not
> scale](https://www.paradedb.com/blog/filter-aware-vector-search).

If `pgvector` is installed, its standard `vector(N)` type and vector opclasses work
directly with ParadeDB:

```sql theme={null}
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE pgvector_items (id bigint PRIMARY KEY, embedding vector(3));
INSERT INTO pgvector_items VALUES (1, '[1, 2, 3]');

CREATE INDEX pgvector_items_search ON pgvector_items
USING paradedb (id, embedding vector_cosine_ops);
```

Other `pgvector` types like `halfvec`, `sparsevec`, `bit` are not yet supported but are on the roadmap.

## Index Options

We recommend keeping the defaults, which work well for most workloads. The following options are available for development and debugging.

```sql SQL theme={null}
CREATE INDEX search_idx ON mock_items
USING paradedb (id, description, category, embedding pdb.vector_cosine_ops)
WITH (target_leaf_size=400, replication=2);
```

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

indexing
  .paradedbIndex("search_idx", {
    targetLeafSize: 400,
  })
  .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",
            target_leaf_size=400,
        ),
    )
```

```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={'target_leaf_size': 400},
)

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: { target_leaf_size: 400 }
)
```

```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)
    .HasTargetLeafSize(400);
```

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

Fraction of indexed vectors sampled for training whenever a segment is
clustered. `0` chooses the fraction automatically based on available memory.

**target\_leaf\_size** (default: 400)

The target number of indexed vectors in a cluster.

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

By default, cluster centroids are quantized. This makes them more compact and
faster to search.

**replication** (default: 2)

ParadeDB uses boundary replication, which means that vectors sitting close to
two clusters are replicated across both. Replication improves recall. A higher
value replicates to more clusters, at the cost of longer index build times and
more storage.

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

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.