agentic_huge_data_base / wiki
页面 Cognee · 4.1 检索类型总览·DeepWiki 中文全文译文

4.1 · 检索类型总览(Search Types Overview)

记忆管道与知识图谱构建 · 聚焦本章的模块关系、源码依据与实现要点。

项目Cognee 章节4.1 状态全文译文 模块检索、召回与索引、测试、发布与运维、配置治理、系统架构
源码线索
  • cognee/api/v1/search/search.py
  • cognee/modules/observability/__init__.py
  • cognee/modules/observability/trace_context.py
  • cognee/modules/observability/tracing.py
  • cognee/modules/retrieval/coding_rules_retriever.py
  • cognee/modules/search/methods/get_retriever_output.py
  • cognee/modules/search/methods/get_search_type_retriever_instance.py
  • cognee/modules/search/methods/search.py
  • cognee/modules/search/models/SearchResultPayload.py
  • cognee/modules/search/types/SearchType.py
模块标签
  • 检索、召回与索引
  • 测试、发布与运维
  • 配置治理
  • 系统架构
  • 工作流与编排

章节正文

检索类型总览

搜索类型概述

相关源文件

本章引用的主要源码文件:

  • cognee/api/v1/search/search.py
  • cognee/modules/observability/__init__.py
  • cognee/modules/observability/trace_context.py
  • cognee/modules/observability/tracing.py
  • cognee/modules/retrieval/coding_rules_retriever.py
  • cognee/modules/search/methods/get_retriever_output.py
  • cognee/modules/search/methods/get_search_type_retriever_instance.py
  • cognee/modules/search/methods/search.py
  • cognee/modules/search/models/SearchResultPayload.py
  • cognee/modules/search/types/SearchType.py

本文档描述了 Cognee 搜索系统中可用的搜索类型及其技术实现。搜索类型决定了检索策略、底层使用的数据库以及响应生成逻辑。

目的与范围

Cognee 提供了一个多策略检索系统,能够将自然语言查询桥接到结构化和非结构化数据。该系统以 SearchType 枚举 cognee/modules/search/types/SearchType.py:4-20 为核心,指导 search 函数 cognee/modules/search/methods/search.py:37-57 选择合适的 BaseRetriever 实现 cognee/modules/search/methods/get_search_type_retriever_instance.py:36-40

搜索执行流程

下图展示了从自然语言查询到选择特定代码实体(检索器)及其对应数据库引擎的数据流。

搜索策略选择
Cognee · 搜索策略选择 · 图 1
Cognee · 搜索策略选择 · 图 1

来源: cognee/modules/search/methods/get_search_type_retriever_instance.py:73-170cognee/modules/search/methods/search.py:92-111cognee/api/v1/search/search.py:28-52

可用搜索类型

GRAPH_COMPLETION(默认)

这是用于复杂推理的主要搜索模式。它采用混合方法,结合了向量相似度与图遍历。

  1. 检索实例:通过 get_search_type_retriever_instance 使用 SearchType.GRAPH_COMPLETION 创建 cognee/modules/search/methods/get_search_type_retriever_instance.py:99-122
  2. 上下文构建:聚合图三元组和节点属性,为大语言模型(LLM)提供结构化上下文 cognee/api/v1/search/search.py:88-91

关键参数:

  • triplet_distance_penalty:调整关系距离的权重 cognee/api/v1/search/search.py:43
  • neighborhood_depth:控制从种子节点开始遍历的跳数 cognee/api/v1/search/search.py:47
RAG_COMPLETION

一种传统的基于向量的检索增强生成(RAG)。它使用 CompletionRetriever cognee/modules/retrieval/completion_retriever.py 从向量数据库中获取文档片段,并在不利用知识图谱结构的情况下生成响应 cognee/api/v1/search/search.py:93-96

CHUNKS 与 CHUNKS_LEXICAL
  • CHUNKS:使用 ChunksRetriever cognee/modules/retrieval/chunks_retriever.py 基于语义相似度返回原始文本段。
  • CHUNKS_LEXICAL:使用 JaccardChunksRetriever cognee/modules/retrieval/jaccard_retrival.py 基于 Token 匹配(例如 Jaccard 相似度)进行精确词项匹配 cognee/api/v1/search/search.py:123-125
SUMMARIES

使用 SummariesRetriever cognee/modules/retrieval/summaries_retriever.py 获取预先生成的内容分层摘要 cognee/api/v1/search/search.py:103-106

时序

调用 TemporalRetriever cognee/modules/retrieval/temporal_retriever.py 查询时间知识图谱。该类型用于基于时间的查询和分析事件序列 cognee/modules/search/types/SearchType.py:17

代码 / CODING_RULES

专门用于仓库分析。它使用 CodingRulesRetriever cognee/modules/retrieval/coding_rules_retriever.py:11 查找实现模式和架构规则。该检索器专门查找 coding_agent_rules 节点集中的节点 cognee/modules/retrieval/coding_rules_retriever.py:18

CYPHER

提供对图数据库的直接访问。它使用 CypherSearchRetriever cognee/modules/retrieval/cypher_search_retriever.py 对 Neo4j 或 Kuzu 执行原始 Cypher 查询 cognee/api/v1/search/search.py:113-116

AGENTIC_COMPLETION

使用 AgenticRetriever cognee/modules/retrieval/agentic_retriever.py 利用可用技能和智能体逻辑执行多步推理或基于工具的检索 cognee/modules/search/methods/get_search_type_retriever_instance.py:160-169

实现细节:搜索编排

搜索过程由 get_retriever_output 函数编排,该函数管理从检索器实例化到完成生成的搜索请求生命周期。

搜索检索序列
Cognee · 搜索检索序列 · 图 2
Cognee · 搜索检索序列 · 图 2

来源: cognee/modules/search/methods/get_retriever_output.py:19-91cognee/modules/search/methods/search.py:92-111

搜索类型对比表

搜索类型主要引擎代码实体最佳用途
GRAPH_COMPLETION图 + 向量GraphCompletionRetriever复杂推理、关系分析
RAG_COMPLETION向量CompletionRetriever文档中的事实查找
CHUNKS向量ChunksRetriever查找特定段落
SUMMARIES关系型SummariesRetriever高层级概览
TEMPORALTemporalRetriever基于时间的查询
CODING_RULESCodingRulesRetriever仓库规则与模式
CYPHERCypherSearchRetriever自定义图遍历
AGENTIC_COMPLETION图 + 大语言模型(LLM)AgenticRetriever多步智能体推理

来源: cognee/api/v1/search/search.py:86-126cognee/modules/search/methods/get_search_type_retriever_instance.py:73-170

高级配置

邻域搜索

GraphCompletionRetriever 这样的检索器支持 neighborhood_depthneighborhood_seed_top_k 参数。如果提供了这些参数,系统会从向量搜索中找到的 top-k 节点开始执行遍历,以丰富上下文 cognee/modules/search/methods/get_search_type_retriever_instance.py:113-114

反馈影响

feedback_influence 参数(取值范围 0.0 到 1.0)允许搜索优先考虑在之前会话中收到正面用户反馈的图元素 cognee/modules/search/methods/get_search_type_retriever_instance.py:110

可观测性

每个搜索操作都被包装在 OpenTelemetry 跨度中。关键属性如 cognee.search.typecognee.search.querycognee.result.count 会被记录,以提供检索性能的可视性 cognee/modules/search/methods/get_retriever_output.py:35-44

来源: cognee/api/v1/search/search.py:43-48cognee/modules/search/methods/search.py:107-110cognee/modules/observability/tracing.py:37-44