跳到主要内容
版本:Next

实战配方:语义检索

目标。 为每个输入查询返回最近的已存段落,不使用 LLM——这是搜索框、 推荐流与重复检测背后的纯检索模式。

前置条件。 文档已摄入(参见 向量 RAG 流水线);一个 OpenAI 兼容的 embedding endpoint,且使用与摄入时相同的模型。

Qdrant 检索​

- type: embedding
api_base: "http://localhost:9997/v1"
model: "text-embedding-3-small"
api_key: "${env:EMBEDDING_API_KEY:-}"
field: "query"
- type: vector_search
url: "http://localhost:6333"
collection: documents
vector_field: embedding
top_k: 5
score_threshold: 0.75
target_field: matches
  • top_k 限制结果数量;score_threshold 在存储侧丢弃弱匹配,因此空结果 表示「没有足够相似的」,而不是「出错」。
  • 匹配以 JSON 数组文本列到达({id, score, payload}),按相似度降序——可以 直接交给 SQL、JSON 工具或 sink。
  • concurrency(默认 4)限制在途请求数,同时保持行序。

pgvector 检索​

- type: embedding
api_base: "http://localhost:9997/v1"
model: "text-embedding-3-small"
api_key: "${env:EMBEDDING_API_KEY:-}"
field: "query"
- type: pgvector_search
url: "postgres://postgres:${env:PG_PASSWORD:-}@localhost:5432/vectors"
table: documents
metric: cosine
top_k: 5
target_field: matches

距离语义——请基于原始距离过滤,不要当作百分比:

metric算子距离含义
cosine<=>余弦距离;0 = 方向相同,范围 [0, 2]
l2<->欧氏距离;0 = 完全一致
inner_product<#>负内积(越小越相似)

pgvector 没有服务端阈值参数:先取 top_k,再在 SQL 中过滤 (余弦距离截断用 WHERE distance <= 0.25)。

完整可运行示例​

登记的示例 examples/semantic_search_pgvector.yaml (manifest 名称 semantic-search-pgvector)把查询 embedding → pgvector_search → SQL 投影 → stdout 串起来。

如何选择向量库​

存储Processor适用场景
Qdrantvector_search需要服务端分数阈值与 payload 过滤。
PostgreSQL + pgvectorpgvector_search向量与关系数据同库;只运维一个数据库。
Milvusmilvus_search已部署 Milvus 的大规模集合。

验证​

  1. 对极小文档集跑摄入。
  2. 用一个转述某文档的查询跑检索配置;期望的匹配应排第一,且分数明显优于 其余。
  3. 用一个无意义查询;设置了阈值时,期望得到空结果(而非失败)。

失败模式​

症状原因修复
每个查询都返回同一个 top 命中查询未被 embedding,或 field 错误确认 embedding processor 在检索之前运行。
换模型后分数异常查询与摄入使用了不同模型重新摄入或换回;两半绝不混用模型。
批次进入 error_output查询向量为空或 SQL 错误检查输入列;pgvector search 不重试。

相关页面​