view

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: 4 Imported by: 0

Documentation

Overview

Package view 提供基于 Cypher 的结构化视图查询能力。

视图查询是「按 Schema 标签的结构化分页列表」语义:

  • 不引入 SQL 引擎、不物化数据;
  • 每次查询通过 Cypher 实时执行,零 LLM 成本;
  • 适合人浏览分页列表(业务系统花名册式),也适合 Agent 精确过滤。

适用场景:

  • AgentHarness 集成:提供 ListLabels / Describe / Query 三类结构化调用;
  • UI 集成:把同标签节点集合以表格方式展示给用户。

与 mindstore/internal/service/view.go 的关系:

  • mindstore 的实现带有 Schema 元数据补全(来自 embed 资源 / 目录级配置);
  • 本包提供无 Schema 依赖的基础实现,可由调用方注入 SchemaProvider 来补全。

Index

Constants

View Source
const (
	ErrInvalidParam       = 40001 // 参数缺失或非法
	ErrUnknownField       = 40002 // 字段名不在 Schema 中
	ErrTypeMismatch       = 40003 // 字段值类型不匹配
	ErrLabelNotFound      = 40401 // 标签不存在
	ErrUnsupportedIndexer = 50101 // 当前索引器不支持视图查询
	ErrGraphNotReady      = 50001 // 图存储未初始化
)

错误码规约

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Name        string `json:"name"`
	Type        string `json:"type"`        // 推断的 JSON 类型
	Description string `json:"description"` // 来自 Schema(若有)
	Required    bool   `json:"required"`
}

Column 描述一个视图列的元数据。

type CypherQuerier

type CypherQuerier interface {
	CypherQuery(ctx context.Context, q string, params map[string]any) ([]map[string]any, error)
}

CypherQuerier 是视图查询所依赖的最小接口。 任何实现此接口的对象都可以作为 ViewService 的后端。 典型实现:gorag/indexer.GraphIndexer.CypherQuery / HyperIndexer.CypherQuery。

type Error

type Error struct {
	Code    int
	Message string
}

Error 携带机器可读的错误码。

func (*Error) Error

func (e *Error) Error() string

type FieldValidator

type FieldValidator interface {
	ValidateField(label, field string) error
}

WithValidator 注入字段名校验器(防御 Cypher 注入)。

type LabelInfo

type LabelInfo struct {
	Label       string `json:"label"`
	Count       int    `json:"count"`
	Category    string `json:"category"`
	Description string `json:"description"`
}

LabelInfo 描述一个可用标签的概览。

type Order

type Order struct {
	Field string // 列名(属性名)
	Desc  bool   // true = DESC,false = ASC
}

Order 描述一个排序规则。

type Range

type Range struct {
	Gte any `json:"gte,omitempty"`
	Lte any `json:"lte,omitempty"`
}

Range 表示一个闭区间 [Gte, Lte];任一端可省略。

type Result

type Result struct {
	Label   string           `json:"label"`
	Columns []Column         `json:"columns"`
	Rows    []map[string]any `json:"rows"`
	Total   int              `json:"total"`
	Page    int              `json:"page"`
	Size    int              `json:"size"`
}

Result 视图查询结果。

type Schema

type Schema struct {
	Label       string   `json:"label"`
	Category    string   `json:"category"`
	Description string   `json:"description"`
	Columns     []Column `json:"columns"`
}

Schema 描述一个标签的完整列结构。

type SchemaProvider

type SchemaProvider interface {
	// Describe 返回标签的列定义。若标签无对应 Schema,返回 (nil, nil)。
	Describe(ctx context.Context, label, category string) ([]Column, string, string, error)
}

WithSchemaProvider 注入 Schema 提供者,用于补全 Describe 的元数据。

type Service

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

Service 结构化视图查询服务。

Service 通过 CypherQuerier 接口直接与底层图存储对话,不依赖 Schema; 调用方可通过 ServiceOption 注入 SchemaProvider / FieldValidator 等可选能力。

func New

func New(cypher CypherQuerier, opts ...ServiceOption) (*Service, error)

New 构造一个 ViewService。 cypher 不能为 nil(必须支持 CypherQuery)。

func (*Service) Describe

func (s *Service) Describe(ctx context.Context, label, category string) (*Schema, error)

Describe 返回指定标签的列结构。

func (*Service) ListLabels

func (s *Service) ListLabels(ctx context.Context) ([]LabelInfo, error)

ListLabels 列出知识库中已存在的所有标签及其计数。

func (*Service) Query

func (s *Service) Query(ctx context.Context, spec Spec) (*Result, error)

Query 执行一次结构化视图查询。 自动归一化 page ≥ 1、size ∈ [1, 200]。

type ServiceOption

type ServiceOption func(*Service)

ServiceOption 是 Service 的配置选项。

func WithSchemaProvider

func WithSchemaProvider(p SchemaProvider) ServiceOption

WithSchemaProvider 设置 Schema 提供者。

func WithValidator

func WithValidator(v FieldValidator) ServiceOption

WithValidator 设置字段校验器。

type Spec

type Spec struct {
	Label     string   // 节点标签,如 "Person"、"Asset"
	Category  string   // 可选,强制指定 Schema 类别("finance" / "general" 等)
	Fields    []string // 可选,列白名单;空 = 返回所有属性
	Where     *Where   // 可选,过滤条件;nil = 不过滤
	OrderBy   []Order  // 可选,排序;空 = 按节点 ID 稳定序
	Page      int      // 1-based;< 1 时按 1 处理
	Size      int      // ≤ 0 时按默认 20 处理;上限 200
	Path      string   // 可选,源文件路径前缀过滤
	WithTotal bool     // 是否统计 total;默认 true
}

Spec 定义一次结构化视图查询。

字段语义参考 mindstore/internal/service/view.go ViewSpec。

type Where

type Where struct {
	Equals   map[string]any    // 等值匹配
	In       map[string][]any  // IN 匹配
	Range    map[string]Range  // 范围匹配
	Contains map[string]string // 字符串包含匹配
}

Where 过滤条件组合,所有子条件之间为 AND 关系。

Jump to

Keyboard shortcuts

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