Skip to main content
Version: V1.4.0

query - Vector query

query() is used to execute vector similarity search to find documents most similar to the query vector.

info

This API can only be used when connected with SeekdbClient. For more information about SeekdbClient, see SeekdbClient.

Prerequisites​

Request parameters​

query()
ParameterTypeRequiredDescriptionExample Value
queryEmbeddingsnumber[] | number[][]YesA 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]
queryTextsstring | string[]NoA 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"]
queryKeyK.EMBEDDING | K.SPARSE_EMBEDDINGNoWhich 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
nResultsnumberYesThe number of similar results to return. Default value is 10`3
whereWhereNoMetadata filter conditions.{"category": {"$eq": "AI"}}
whereDocumentWhereDocumentNoDocument filter conditions.{"$contains": "machine"}
includereadonly ("documents" | "metadatas" | "embeddings" | "distances")[][str]NoA list of fields to include: ["documents", "metadatas", "embeddings"].["documents", "metadatas", "embeddings"]
distanceDistanceMetricNoThe distance metric.
approximatebooleanNoWhether to use approximate search.true
info
  • The embeddingFunction used is associated with the collection (set during createCollection() or getCollection()). You cannot override it for each operation.

  • If queryEmbeddings is provided: The vectors will be used directly without calling embeddingFunction.

  • If queryEmbeddings is not provided but queryTexts is:

    • 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 queryEmbeddings nor queryTexts is 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​

ParameterTypeRequiredDescription
idsreadonly (readonly string[])[]YesThe ID to be added or modified. It can be a single ID or an array of IDs.
embeddings[List[List[List[float]]]]NoVectors. If provided, they will be used directly (ignoring embeddingFunction). If not provided, documents can be provided to automatically generate vectors.
documents[List[List[Dict]]]NoDocuments. If vectors are not provided, documents will be converted to vectors using the collection's embeddingFunction.
metadatas[List[List[Dict]]]NoMetadata.
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
}