indexer

package
v1.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetChunks

func GetChunks(content string, opts ...ChunkOption) ([]*core.Chunk, error)

GetChunks 根据文本内容进行结构化和分块 如果没有指定策略,会根据内容自动选择最佳分块策略 返回完整的分块数组

func GetFileChunks

func GetFileChunks(file string, opts ...ChunkOption) ([]*core.Chunk, error)

GetFileChunks 根据文件路径进行结构化和分块 如果没有指定策略,会根据内容自动选择最佳分块策略 返回完整的分块数组

func NewFulltextIndexer

func NewFulltextIndexer(store core.FullTextStore) (core.Indexer, error)

func NewFulltextIndexerWithFile

func NewFulltextIndexerWithFile(dbPath string) (core.Indexer, error)

NewFulltextIndexer 创建全文索引器

func NewSafeFulltextIndexer

func NewSafeFulltextIndexer(dbPath string) (core.Indexer, error)

NewSafeFulltextIndexer 创建线程安全的全文索引器

func NewSemanticIndexer

func NewSemanticIndexer(db core.VectorStore, embedder core.Embedder) core.Indexer

Types

type ChunkOption

type ChunkOption func(*chunkOption)

ChunkOption 分块选项

func WithChunkStrategy

func WithChunkStrategy(strategy core.ChunkStrategy) ChunkOption

WithChunkStrategy 设置分块策略

type CommunityIndexer

type CommunityIndexer struct {
	// contains filtered or unexported fields
}

CommunityIndexer 社区索引器 负责:社区检测 → 摘要生成 → 存储 → Global Search 查询

func NewCommunityIndexer

func NewCommunityIndexer(store core.GraphStore, client chat.Client, detector ...core.CommunityDetector) *CommunityIndexer

NewCommunityIndexer 创建社区索引器 detector 参数可选,如果为 nil 则使用默认的连通分量检测器

func (*CommunityIndexer) Build

func (ci *CommunityIndexer) Build(ctx context.Context, maxLevel int) error

Build 执行完整的社区构建流程 1. 社区检测(纯图算法) 2. 社区摘要生成(LLM) 3. 摘要存储到图数据库 maxLevel: 层次聚类最大深度(0 = 单层)

func (*CommunityIndexer) SearchGlobal

func (ci *CommunityIndexer) SearchGlobal(ctx context.Context, query string, level int) ([]core.CommunityMatch, error)

SearchGlobal 通过社区摘要回答全局性问题 (Global Search) 流程:获取社区摘要 → LLM Map-Reduce 生成答案 返回匹配的社区信息,不生成最终答案(答案生成由调用方完成)

type GraphIndexer

type GraphIndexer struct {
	// contains filtered or unexported fields
}

GraphIndexer 知识图谱索引器 与语义/全文索引器不同,GraphRAG 的索引是构建实体-关系图,查询是图遍历 支持两种查询模式: 1. 独立 GraphRAG 模式(需要 client):Query → LLM提取实体 → 图遍历 2. 混合模式(通过 Chunk IDs):Query → Semantic搜索 → Chunks → 关联 Nodes/Edges

func NewGraphIndexer

func NewGraphIndexer(store core.GraphStore, opts ...any) *GraphIndexer

NewGraphIndexer 创建知识图谱索引器 client 参数可选:

  • 提供 client:支持独立 GraphRAG 模式(索引 + 查询都用 LLM)
  • 不提供 client:仅支持混合模式(索引时不用 LLM,查询时通过 Chunk IDs)

opts 可选配置,如 WithCache

func (*GraphIndexer) Add

func (g *GraphIndexer) Add(ctx context.Context, content string) ([]*core.Chunk, error)

Add 从内容构建知识图谱(实现 core.Indexer 接口) 流程:分块 → LLM实体关系提取 → 图存储 注意:如果没有 client,会跳过实体提取

func (*GraphIndexer) AddFile

func (g *GraphIndexer) AddFile(ctx context.Context, filePath string) ([]*core.Chunk, error)

AddFile 从文件构建知识图谱(实现 core.Indexer 接口) 注意:如果没有 client,会跳过实体提取

func (*GraphIndexer) Close

func (g *GraphIndexer) Close(ctx context.Context) error

Close 关闭图存储和缓存

func (*GraphIndexer) DB

func (g *GraphIndexer) DB() core.GraphStore

DB 返回图存储数据库

func (*GraphIndexer) IndexChunk

func (g *GraphIndexer) IndexChunk(ctx context.Context, chunk *core.Chunk) error

IndexChunk indexes a pre-generated chunk (implements core.Indexer interface)

func (*GraphIndexer) IndexChunks

func (g *GraphIndexer) IndexChunks(ctx context.Context, chunks []*core.Chunk) error

IndexChunks indexes multiple pre-generated chunks in batch (implements core.ChunkIndexer interface)

三重优化流水线:

  1. 大块优先:将 ParentDoc 子块聚合到父块,减少总 chunk 数
  2. 分级提取:跳过低价值 chunk(纯描述、过短内容),仅对高价值 chunk 调用 LLM
  3. 并发提取:远程 LLM 全并发;本地 LLM(Ollama 等)串行提取但并发写入图数据库

func (*GraphIndexer) Name

func (g *GraphIndexer) Name() string

Name 返回索引器名称

func (*GraphIndexer) NewQuery

func (g *GraphIndexer) NewQuery(terms string) core.Query

NewQuery 创建图查询(实现 core.Indexer 接口)

func (*GraphIndexer) Remove

func (g *GraphIndexer) Remove(ctx context.Context, chunkID string) error

Remove 移除与指定 chunk 关联的所有实体和关系(实现 core.Indexer 接口)

func (*GraphIndexer) Search

func (g *GraphIndexer) Search(ctx context.Context, qry core.Query) ([]core.Hit, error)

Search 执行图搜索(实现 core.Indexer 接口) 独立 GraphRAG 模式:LLM实体提取 → 图遍历 → 节点/边序列化 → Hit 注意:需要 client,否则返回空结果

func (*GraphIndexer) SearchByChunkIDs

func (g *GraphIndexer) SearchByChunkIDs(ctx context.Context, chunkIDs []string, depth, limit int, edgeTypes ...[]string) ([]core.Hit, error)

SearchByChunkIDs 通过 Chunk IDs 查询关联的图结构 混合模式:由 HybridIndexer 调用,无需 LLM 流程:Chunk IDs → 查询关联 Nodes → 多跳遍历(可选边类型过滤) → 路径评分 → Hit

支持选项:

  • depth: 遍历深度(默认 1,即直接邻居)
  • limit: 返回结果数量上限
  • edgeTypes: 关系类型过滤,仅遍历指定类型的边

func (*GraphIndexer) SearchGlobal

func (g *GraphIndexer) SearchGlobal(ctx context.Context, query string, level int) ([]core.Hit, error)

SearchGlobal 通过社区摘要回答全局性问题 (Global Search) 适用于:"数据集主要讨论了哪些主题?" 这类宏观问题 需要 client 和已构建的社区摘要

func (*GraphIndexer) Type

func (g *GraphIndexer) Type() string

Type 返回索引器类型

type GraphOption

type GraphOption func(*GraphIndexer)

GraphOption GraphIndexer 的可选配置

func WithCache

func WithCache(cache core.CacheStore) GraphOption

WithCache 设置实体提取缓存(依赖注入 core.CacheStore 接口)

type GraphSearchResult

type GraphSearchResult struct {
	Query     string       `json:"query,omitempty"`     // 原始查询
	Entities  []*core.Node `json:"entities,omitempty"`  // 匹配的实体节点
	Relations []*core.Edge `json:"relations,omitempty"` // 关联的边
	ChunkIDs  []string     `json:"chunk_ids,omitempty"` // 关联的 chunk ID 列表
	DocIDs    []string     `json:"doc_ids,omitempty"`   // 关联的文档 ID 列表
}

GraphSearchResult 图搜索结果,用于序列化为 Hit.Content

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL