混合搜索会并行运行向量搜索与关键词搜索,并合并结果。它需要启用 关键词搜索。有关搜索模式概览,请参阅 搜索模式。
将 index_method.vector 与 index_method.keyword 均设为 true:
const instance = await env.AI_SEARCH.create({
id: "my-instance",
index_method: {
vector: true,
keyword: true,
},
fusion_method: "rrf",
});要禁用混合搜索,将 index_method.keyword 设为 false。关键词索引将被删除。
对于每种搜索方法,你可以配置以下项以调整检索行为:
fusion_method 字段控制如何合并向量与关键词结果。
| 值 | 默认 | 说明 |
|---|---|---|
rrf |
是 | Reciprocal Rank Fusion。根据两种搜索方法中的排名位置为结果打分。推荐用于大多数用例。 |
max |
否 | 取归一化后的向量分数与关键词分数中的较高者。适用于某一搜索方法始终更相关的场景。 |
融合之后,你可以选择应用重排序,以按语义相关性进一步重新排序结果。重排序使用交叉编码器模型,共同评估查询与每个分块,可在仅依赖融合之外进一步提升精度。
重排序默认禁用。请参阅 重排序 以启用并配置。
使用 ai_search_options.retrieval 可在单个请求上覆盖搜索设置。
| 字段 | 类型 | 说明 |
|---|---|---|
retrieval_type |
"vector"、"keyword" 或 "hybrid" |
强制使用特定搜索模式。必须与 index_method 兼容。 |
fusion_method |
"rrf" 或 "max" |
覆盖融合方法。 |
const instance = env.AI_SEARCH.get("my-instance");
const results = await instance.search({
messages: [{ role: "user", content: "What is Cloudflare?" }],
ai_search_options: {
retrieval: {
retrieval_type: "hybrid",
fusion_method: "rrf",
},
},
});当混合搜索处于活动状态时,每个分块都会包含 scoring_details 对象:
| 字段 | 类型 | 说明 |
|---|---|---|
vector_score |
number | 向量相似度分数(0 到 1)。 |
keyword_score |
number | 原始 BM25 关键词分数。 |
vector_rank |
number | 在向量结果集中的排名位置。 |
keyword_rank |
number | 在关键词结果集中的排名位置。 |
fusion_method |
string | 使用的融合方法(rrf 或 max)。 |
reranking_score |
number | 启用时来自重排序模型的分数。 |
启用关键词搜索的实例在 Workers Paid 套餐上每个实例最多支持 500,000 个文件,而仅向量实例为 1,000,000 个。完整限制列表请参阅 限制与定价。