query

package
v2.0.13 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New added in v2.0.13

func New(text string) core.Query

New 创建一个 core.Query 接口的默认实现。

返回 BaseQuery 实例,预设:

  • raw = text(原始查询字符串)
  • normalized = utils.Clean(text)(清洗后的查询字符串,用于关键词提取)
  • queryType = "semantic"(默认语义查询,由查询前优化阶段识别后通过 SetType 设置)
  • filters = nil(无过滤条件,由调用方通过 AddFilter 添加)
  • embedding = nil(由 indexer 在 Search 前通过 SetEmbedding 注入)

func NewGraphQuery

func NewGraphQuery(terms string) core.Query

NewGraphQuery creates a new graph query from the given natural language text. terms 作为自然语言查询(Text)传递,同时作为原始搜索词保留在 BaseQuery 中。

func NewSemanticQuery

func NewSemanticQuery(terms string, embedder core.Embedder) core.Query

NewSemanticQuery creates a new semantic query from the given terms.

func NewTreeQuery added in v2.0.4

func NewTreeQuery(regionID string, depth int) core.Query

NewTreeQuery 创建树查询。 regionID 为空时返回全局树(所有 Region 为直接子节点)。 depth 控制深度,超出范围时自动裁剪。

func NewWithType added in v2.0.13

func NewWithType(text, queryType string) core.Query

NewWithType 创建一个指定查询类型的 core.Query。

查询类型取值:

  • "semantic":语义查询,走 VectorStore.Search
  • "keyword":关键词查询,走 Metadata 过滤
  • "hybrid":混合查询,同时走语义和关键词
  • "graph":图查询,走 GraphStore.Query(Cypher)

调用方在查询前优化阶段识别查询类型后通过此函数创建 Query, 或通过 New(text).(interface{ SetType(string) core.Query }).SetType(t) 链式调用设置。

func Validate added in v2.0.13

func Validate(q core.Query) error

Validate 校验 Query 的合法性。 当前仅校验 raw 非空;后续可扩展(如校验 queryType 取值)。

Types

type BaseQuery

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

BaseQuery is a default implementation of the core.Query interface.

func (*BaseQuery) AddFilter

func (q *BaseQuery) AddFilter(key string, value any) core.Query

AddFilter adds a filter to the query. The value is copied to prevent external modification.

Parameters:

  • key: The filter key
  • value: The filter value

Returns:

  • core.Query: The query with the added filter

func (*BaseQuery) Embedding added in v2.0.13

func (q *BaseQuery) Embedding() []float32

Embedding 返回查询向量(由 indexer 在 Search 前注入)。

func (*BaseQuery) Filters

func (q *BaseQuery) Filters() map[string]any

Filters returns the filters to apply to the search.

Returns:

  • map[string]any: The filters

func (*BaseQuery) Keywords

func (q *BaseQuery) Keywords() []string

Keywords returns the extracted keywords from the query.

Returns:

  • []string: The extracted keywords

func (*BaseQuery) Raw

func (q *BaseQuery) Raw() string

Raw returns the raw, unprocessed query string.

Returns:

  • string: The raw query string

func (*BaseQuery) SetEmbedding added in v2.0.13

func (q *BaseQuery) SetEmbedding(vec []float32) core.Query

SetEmbedding 设置查询向量,返回 Query 自身支持链式调用。 由 indexer 在 Search 之前调用,将计算好的向量缓存到 Query 中。

func (*BaseQuery) SetType added in v2.0.13

func (q *BaseQuery) SetType(t string) core.Query

SetType 设置查询类型,返回 Query 自身支持链式调用。

func (*BaseQuery) Type added in v2.0.13

func (q *BaseQuery) Type() string

Type 返回查询类型(semantic/keyword/hybrid/graph)。 默认返回 "semantic",由查询前优化阶段识别后通过 SetType 设置。

type GraphQuery

type GraphQuery struct {
	BaseQuery
	Depth     int
	Limit     int
	EdgeTypes []string // 关系类型过滤(可选),空表示不过滤
	// contains filtered or unexported fields
}

GraphQuery 图查询

支持两种查询模式:

  • Text:自然语言描述,由 GraphIndexer 内部 LLM 自动生成 Cypher 执行
  • Raw:直接提供原生 Cypher 语句,GraphIndexer 不做转换直接执行

如果 Text 和 Raw 都为空,GraphIndexer 走默认的语义向量检索 + 图融合路径。

func (*GraphQuery) RawCypher

func (q *GraphQuery) RawCypher() string

RawCypher 返回原生 Cypher 查询语句。 非空时,GraphIndexer 直接将其交给 graphDB 执行。

func (*GraphQuery) SetDepth

func (q *GraphQuery) SetDepth(depth int)

SetDepth 设置图遍历深度

func (*GraphQuery) SetEdgeTypes

func (q *GraphQuery) SetEdgeTypes(types []string)

SetEdgeTypes 设置关系类型过滤(仅遍历指定类型的边)

func (*GraphQuery) SetLimit

func (q *GraphQuery) SetLimit(limit int)

SetLimit 设置返回结果数量限制

func (*GraphQuery) SetRawCypher

func (q *GraphQuery) SetRawCypher(cypher string)

SetRawCypher 设置原生 Cypher 查询语句。 GraphIndexer 会在 Search 时直接执行此语句。

func (*GraphQuery) SetTextQuery

func (q *GraphQuery) SetTextQuery(text string)

SetTextQuery 设置自然语言查询文本。 GraphIndexer 会在 Search 时使用内部 LLM 将其转换为 Cypher。

func (*GraphQuery) TextQuery

func (q *GraphQuery) TextQuery() string

TextQuery 返回自然语言查询文本。 非空时,GraphIndexer 会使用内部 LLM 将其转换为 Cypher 再执行。

type SemanticQuery

type SemanticQuery struct {
	BaseQuery
	Embedder core.Embedder // 向量编码器,用于语义相似度计算
	// contains filtered or unexported fields
}

SemanticQuery 语义查询

func (*SemanticQuery) Expansion

func (q *SemanticQuery) Expansion(client chat.Client) error

Expansion 查询扩展:生成多个相关查询变体,扩大检索范围 适用于查询词过于狭窄或模糊的场景

func (*SemanticQuery) HyDe

func (q *SemanticQuery) HyDe(client chat.Client) error

HyDe 假设式文档查询:生成一个假设性的文档来回答查询 适用于需要语义匹配但缺乏精确关键词的场景

func (*SemanticQuery) Rewriting

func (q *SemanticQuery) Rewriting(client chat.Client) error

Rewriting 查询重写:将模糊或不明确的查询改写为更清晰的形式 适用于口语化、模糊或不完整的查询

func (*SemanticQuery) Vector

func (q *SemanticQuery) Vector() *core.Vector

Vector returns the vector representations of the query.

type Text2Cypher added in v2.0.13

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

Text2Cypher 自然语言转 Cypher 预实现类。

承载自然语言 → Cypher 的转换能力,作为 query 包的预实现能力保留。 当前可能未在主流程使用,但作为后续扩展的基础设施不删除。 内部依赖 LLM 客户端完成转换;构造函数接收 chat.Client 和 model 作为必传参数,返回 error。

使用方式:

t2c, err := query.NewText2Cypher(chatClient, "gpt-4", logger)
if err != nil { ... }
cypher, err := t2c.Translate(ctx, "查询所有 Person 类型的实体")
if err != nil { ... }
// 用 cypher 调用 graphDB.Query(ctx, cypher, params)

func NewText2Cypher added in v2.0.13

func NewText2Cypher(chatClient chat.Client, model string, logger logging.Logger) (*Text2Cypher, error)

NewText2Cypher 创建 Text2Cypher 实例。

必传参数:

  • chatClient: LLM 客户端(不能为 nil)
  • model: 模型名称(不能为空)
  • logger: 日志实例(可为 nil,使用 NoopLogger)

返回 error 而非 panic。

func (*Text2Cypher) Translate added in v2.0.13

func (t *Text2Cypher) Translate(ctx context.Context, naturalLanguage string) (string, error)

Translate 将自然语言查询转换为 Cypher 查询语句。

流程:

  1. 构建 Cypher 生成 Prompt(含 gograph 数据模型说明)
  2. 调用 LLM(最多 3 次重试,间隔 2s/4s)
  3. 清理响应(移除 markdown 代码块标记)

失败时返回 error,包含详细的重试信息。

type TreeQuery added in v2.0.4

type TreeQuery struct {
	BaseQuery

	// RegionID 为空字符串表示全局树,非空表示限定到指定 Region。
	RegionID string

	// Depth 控制树深度:
	//   0/1 = Region → Document(默认)
	//   2   = Region → Document → Entity
	Depth int
}

TreeQuery 树状知识结构查询。

限定范围(Region 或全局)返回 ChunkNode 树, 供 UI 导航展示,避免全量加载力导向图。

Jump to

Keyboard shortcuts

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