TextSearch:App 侧全文索引
按已声明字段建立局部索引,查询必须给出应用过滤器。
构造参数包含 indexRevision、fields、storedFields,可加 tokenize、boost、combineWith。upsert 按文档 id 增加或替换。search({query,limit,filter}) 返回 {id,score,terms,document}[];document 仅包含 storedFields,不是原文完整副本。query 不能为空,limit 为 1—1000,filter 必须同步返回 boolean。
默认 combineWith='OR';分词默认来自 MiniSearch,中文等语言可显式提供 tokenizer。remove 异步等待 vacuum,clear 清空当前索引。snapshot/restore 检查引擎标识和配置指纹;indexRevision 由 App 表达格式/分词语义版本,修改自定义分词行为时也应更新。快照不是持久化服务,保存、到期、删除和访问策略由 App 负责。
由简到繁的同业务示例
先索引两份接口说明
入门 · Node.js
ts
import { TextSearch } from '@jangts/agent-core/kits/search';
type Document = { id: string; title: string; body: string };
const index = new TextSearch<Document>({ indexRevision: 'manual-v1', fields: ['title', 'body'], storedFields: ['title'] });
index.upsert({ id: 'workflow', title: 'Workflow', body: 'Define reusable agent nodes.' });
index.upsert({ id: 'model', title: 'Model', body: 'Bind a model provider.' });
console.log(index.search({ query: 'agent', limit: 5, filter: () => true }));js
import { TextSearch } from '@jangts/agent-core/kits/search';
const index = new TextSearch({ indexRevision: 'manual-v1', fields: ['title', 'body'], storedFields: ['title'] });
index.upsert({ id: 'workflow', title: 'Workflow', body: 'Define reusable agent nodes.' });
index.upsert({ id: 'model', title: 'Model', body: 'Bind a model provider.' });
console.log(index.search({ query: 'agent', limit: 5, filter: () => true }));相同 search 增加访问过滤、权重和快照
配置 · Node.js
允许用于过滤的元数据须登记为 storedFields;字段过滤必须依赖可信 App 身份,不能让模型自报身份。
ts
import { TextSearch } from '@jangts/agent-core/kits/search';
type Document = { id: string; title: string; body: string; audience: string };
const options = { indexRevision: 'manual-v2', fields: ['title', 'body'] as const, storedFields: ['title', 'audience'] as const, boost: { title: 3 }, combineWith: 'AND' as const };
const index = new TextSearch<Document>(options);
index.upsert({ id: 'workflow', title: 'Agent Workflow', body: 'Define reusable nodes.', audience: 'public' });
index.upsert({ id: 'private', title: 'Agent internals', body: 'Internal notes.', audience: 'owner' });
const restored = TextSearch.restore<Document>(options, index.snapshot());
console.log(restored.search({ query: 'agent', limit: 5, filter: document => document.audience === 'public' }));
await restored.remove('workflow');js
import { TextSearch } from '@jangts/agent-core/kits/search';
const options = { indexRevision: 'manual-v2', fields: ['title', 'body'], storedFields: ['title', 'audience'], boost: { title: 3 }, combineWith: 'AND' };
const index = new TextSearch(options);
index.upsert({ id: 'workflow', title: 'Agent Workflow', body: 'Define reusable nodes.', audience: 'public' });
index.upsert({ id: 'private', title: 'Agent internals', body: 'Internal notes.', audience: 'owner' });
const restored = TextSearch.restore(options, index.snapshot());
console.log(restored.search({ query: 'agent', limit: 5, filter: document => document.audience === 'public' }));
await restored.remove('workflow');