VelaVela
Cloudflare

Vectorize

Use Cloudflare Vectorize for vector search in your Vela application.

The VectorizeModule provides a managed VectorizeService for storing and querying vector embeddings.

Setup

Import the module and configure the binding name:

import { Module } from '@velajs/vela';
import { VectorizeModule } from '@velajs/cloudflare';

@Module({
  imports: [VectorizeModule.forRoot({ binding: 'MY_INDEX' })],
})
class AppModule {}

Add the Vectorize index to wrangler.toml:

[[vectorize]]
binding = "MY_INDEX"
index_name = "my-index"

Using VectorizeService

Inject VectorizeService to manage and query vectors:

import { Controller, Post, Body } from '@velajs/vela';
import { VectorizeService } from '@velajs/cloudflare';

@Controller('search')
class SearchController {
  constructor(private readonly vectorize: VectorizeService) {}

  @Post('index')
  async index(@Body() body: { id: string; values: number[] }) {
    await this.vectorize.insert([{ id: body.id, values: body.values }]);
    return { indexed: true };
  }

  @Post('query')
  async query(@Body() body: { vector: number[]; topK?: number }) {
    const results = await this.vectorize.query(body.vector, {
      topK: body.topK ?? 10,
    });
    return results;
  }
}

Methods

MethodDescription
query(vector, options?)Find the nearest vectors. Options support topK, filter, and returnValues.
insert(vectors)Insert new vectors. Each vector has an id, values, and optional metadata.
upsert(vectors)Insert or update vectors. Same shape as insert.
getByIds(ids)Retrieve vectors by their IDs.
deleteByIds(ids)Delete vectors by their IDs.
describe()Return index metadata (dimensions, count, configuration).
Edit on GitHub

Last updated on

On this page