search
Searches for vectors similar to a given vector or content string.
search(
data: string | number[],
options?: {
topK?: number
measurement?: Measurement
filters?: Record<string, any>
includeVector?: boolean
includeMetadata?: boolean
}
): Promise<SearchResponse[]>
Reference
import { myVectorStore } from "#elements";
export default async function () {
const results = await myVectorStore.search("content");
console.log(JSON.stringify(results, null, 2));
}
Parameters
data
: The vector or content string to search for.options
: Optional configuration parameters, including:topK
: (optional) The number of results to return, defaults to 10 and capped at 100.measurement
: (optional) The measurement method for semantic similarity, defaults to euclidean.includeVector
: (optional) Whether to include the vector in the result, defaults to false.includeMetadata
: (optional) Whether to include the metadata in the result, defaults to true.filters
: (optional) Filters to apply to the search.
Returns
Promise of search results, each with an ID, a distance to the given content, and optional vector and metadata.
id
: The ID of the result.distance
: The distance to the given content.vector
: (Optional) The vector of the result.metadata
: (Optional) The metadata of the result.
Caveats
- The returned data is sorted by distance in ascending order. The smaller the distance, the more similar the vector.
Examples
In the HTTP Element, search data in the VectorStore Element that matches the key-value pairs included in filters:
import * as Koa from "koa";
import { myVectorStore } from "#elements";
export default async function (
request: Koa.Request,
response: Koa.Response,
ctx: Koa.Context
) {
const result = await myVectorStore.search("what is babel?", {
topK: 3,
filters: {
source: "https://docs.babel.cloud/docs/overview",
},
});
return result;
}