query - Vector query
query() is used to execute vector similarity search to find documents most similar to the query vector.
This API can only be used when connected with SeekdbClient. For more information about SeekdbClient, see SeekdbClient.
Prerequisites
-
You have installed seekdb-js. For more information, see Quick start.
-
If you use seekdb in server mode, make sure seekdb server mode is installed. For more information, see Deploy seekdb with package managers.
-
You have connected to the database. For more information, see SeekdbClient.
-
You have created a collection and inserted corresponding data. For more information, see createCollection - Create a collection and add - Insert data.
Request parameters
query()
| Parameter | Type | Required | Description | Example Value |
|---|---|---|---|---|
queryEmbeddings | number[] | number[][] | Yes | A single vector or a list of vectors for batch queries. If provided, they are used directly (ignoring embeddingFunction). If not provided, queryTexts must be provided, and collection must have an embeddingFunction. | [1.0, 2.0, 3.0] |
queryTexts | string | string[] | No | A single query string or a list of query strings. If provided, they are used directly (ignoring embeddingFunction). If not provided, documents must be provided, and collection must have an embeddingFunction. | ["my query text"] |
queryKey | K.EMBEDDING | K.SPARSE_EMBEDDING | No | Which index to query. If omitted, the dense vector index is used. If set, K.EMBEDDING selects the dense vector index and K.SPARSE_EMBEDDING selects the sparse vector index. | K.EMBEDDING |
nResults | number | Yes | The number of similar results to return. Default value is 10 | `3 |
where | Where | No | Metadata filter conditions. | {"category": {"$eq": "AI"}} |
whereDocument | WhereDocument | No | Document filter conditions. | {"$contains": "machine"} |
include | readonly ("documents" | "metadatas" | "embeddings" | "distances")[][str] | No | A list of fields to include: ["documents", "metadatas", "embeddings"]. | ["documents", "metadatas", "embeddings"] |
distance | DistanceMetric | No | The distance metric. | |
approximate | boolean | No | Whether to use approximate search. | true |
-
The
embeddingFunctionused is associated with the collection (set duringcreateCollection()orgetCollection()). You cannot override it for each operation. -
If
queryEmbeddingsis provided: The vectors will be used directly without callingembeddingFunction. -
If
queryEmbeddingsis not provided butqueryTextsis:-
If the collection has an
embeddingFunction, the query vector will be automatically generated from the text. -
If the collection does not have an
embeddingFunction, a ValueError will be thrown.
-
-
If neither
queryEmbeddingsnorqueryTextsis provided, a ValueError will be thrown.
Request example
import { SeekdbClient } from "seekdb";
// 1. Connect
// Embedded mode - Database management (seekdb Embedded)
// const client = new SeekdbClient({
// path: "./seekdb.db",
// database: "test",
// });
// Remote server mode - Database management (seekdb Server)
const client = new SeekdbClient({
host: "127.0.0.1",
port: 2881,
user: "root",
password: "",
database: "test",
});
// 2. Create collection
const collection = await client.createCollection({ name: "my_collection" });
// 3. Add data (auto-vectorized)
await collection.add({
ids: ["1", "2"],
documents: ["Hello world", "seekdb is fast"],
});
// 4. Search
const results = await collection.query({
queryTexts: "Hello",
nResults: 5,
});
console.log(results);
Response parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ids | readonly (readonly string[])[] | Yes | The ID to be added or modified. It can be a single ID or an array of IDs. |
embeddings | [List[List[List[float]]]] | No | Vectors. If provided, they will be used directly (ignoring embeddingFunction). If not provided, documents can be provided to automatically generate vectors. |
documents | [List[List[Dict]]] | No | Documents. If vectors are not provided, documents will be converted to vectors using the collection's embeddingFunction. |
metadatas | [List[List[Dict]]] | No | Metadata. |
distances | [List[List[Dict]]] | No |
Response example
{
ids: [ [ '1', '2' ] ],
distances: [ [ 0.24884326749104657, 0.9389671014508124 ] ],
documents: [ [ 'Hello world', 'seekdb is fast' ] ],
metadatas: [ [ null, null ] ],
embeddings: undefined
}