Query vectors
Querying an index, or vector search, enables you to search an index by providing an input vector and returning the nearest vectors based on the configured distance metric.
Optionally, you can apply metadata filters or a namespace to narrow the vector search space.
To pass a vector as a query to an index, use the query()
method on the index itself.
A query vector is either an array of JavaScript numbers, 32-bit floating point or 64-bit floating point numbers: number[]
, Float32Array
, or Float64Array
. Unlike when inserting vectors, a query vector does not need an ID or metadata.
This would return a set of matches resembling the following, based on a cosine
distance metric:
You can optionally change the number of results returned and/or whether results should include metadata and values:
This would return a set of matches resembling the following, based on a cosine
distance metric:
Refer to Vectorize API for additional examples.
If you are generating embeddings from a Workers AI text embedding model, the response type from env.AI.run()
is an object that includes both the shape
of the response vector - e.g. [1,768]
- and the vector data
as an array of vectors:
When passing the vector to the query()
method of a Vectorize index, pass only the vector embedding itself on the .data
sub-object, and not the top-level response.
For example:
Passing queryVector
or queryVector.data
will cause query()
to return an error.
When using OpenAI’s JavaScript client API and Embeddings API, the response type from embeddings.create
is an object that includes the model, usage information and the requested vector embedding.
Similar to Workers AI, you will need to provide the vector embedding itself (.embedding[0]
) and not the EmbeddingResponse
wrapper when querying a Vectorize index: