跳到主要内容
版本:V1.1.0

query - 向量查询

query() 用于执行向量相似性搜索,以找到与查询向量最相似的 documents。

信息

仅支持在使用 SeekdbClient 连接时,才能使用该接口。关于 SeekdbClient 的详细介绍,参见 SeekdbClient

前提条件

请求参数

query()
参数取值类型是否必选描述取值示例
queryEmbeddingsnumber[] | number[][]必选用于批量查询的单个向量或向量列表;如果提供,则直接使用(忽略embeddingFunction);如果没有提供,则必须提供 queryTextcollection 必须具有 embeddingFunction[1.0, 2.0, 3.0]
queryTextsstring | string[]可选单个 vectors 或 vectors 列表;如果提供,则直接使用(忽略 embeddingFunction);如果没有提供,则必须提供 documents,同时 collection 必须具有 embeddingFunction["my query text"]
nResultsnumber必须返回相似的结果数,默认值为 10`3
whereWhere可选Metadata 筛选条件。{"category": {"$eq": "AI"}}
whereDocumentWhereDocument可选Document 筛选条件。{"$contains": "machine"}
includereadonly ("documents" | "metadatas" | "embeddings" | "distances")[][str]可选要包含的字段列表:["documents", "metadatas", "embeddings"]["documents", "metadatas", "embeddings"]
distanceDistanceMetric可选距离度量方式。
approximateboolean可选是否使用近似搜索。true
信息
  • 使用的 embeddingFunction 是与 collection 相关联的(在 createCollection()getCollection() 期间设置)。您不能每次操作都覆盖它。

  • 如果提供了 queryEmbeddings: 直接使用向量,不调用 embeddingFunction

  • 如果未提供 queryEmbeddings 但提供了 queryTexts:

    • 如果集合有 embeddingFunction,将自动从文本生成查询向量。

    • 如果集合没有 embeddingFunction,将抛出 ValueError。

  • 如果既未提供 queryEmbeddings 也未提供 queryTexts: 将抛出 ValueError。

请求示例

import { SeekdbClient } from "seekdb";

// 1. Connect
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);

返回参数

参数取值类型是否必选描述
idsreadonly (readonly string[])[]必选需要新增或者修改的 ID。可以是单个,也可以是数组。
embeddings[List[List[List[float]]]]可选Vectors。如果提供,直接使用(忽略 embeddingFunction),如果不提供,可以提供 documents 来自动生成 vectors。
documents[List[List[Dict]]]可选Documents。如果没有提供 vectorsdocuments 将使用 collection 的 embeddingFunction 转换为 vectors。
metadatas[List[List[Dict]]]可选metadata。
distances[List[List[Dict]]]可选

返回示例

{
ids: [ [ '1', '2' ] ],
distances: [ [ 0.24884326749104657, 0.9389671014508124 ] ],
documents: [ [ 'Hello world', 'seekdb is fast' ] ],
metadatas: [ [ null, null ] ],
embeddings: undefined
}

相关操作