TextSearchOptions
KIT · interface · 类型合同
App supplies fields, tokenizer and its own schema/tokenizer revision.
导入
ts
import type { TextSearchOptions } from '@jangts/agent-core/kits/search';1
js
/** @typedef {import('@jangts/agent-core/kits/search').TextSearchOptions} TextSearchOptions */1
其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits、@jangts/agent-core/kits/search。
定义
ts
interface TextSearchOptions<Document>1
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Document | { id: string } | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
indexRevision | string | 必需 | 只读。App-owned revision of the field/tokenizer configuration; a change invalidates snapshots. |
fields | readonly (keyof Document & string)[] | 必需 | 只读。Fields that are indexed and searchable. |
storedFields | readonly (keyof Document & string)[] | 必需 | 只读。Fields returned in a hit; the indexed body is not implicitly returned. |
tokenize | ((text: string, fieldName?: string) => string[]) | undefined | 可省略 | 只读。Tokenizer override; defaults to MiniSearch's default tokenizer. |
boost | Readonly<Partial<Record<keyof Document & string, number>>> | undefined | 可省略 | 只读。Per-field relevance multiplier; omitted fields keep the default weight. |
combineWith | "AND" | "OR" | undefined | 可省略 | 只读。How multiple query terms combine; defaults to the engine default (OR). |
使用示例
- TextSearch: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 }));1
2
3
4
5
6
2
3
4
5
6
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 }));1
2
3
4
5
2
3
4
5
相同 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');1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
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');1
2
3
4
5
6
7
8
2
3
4
5
6
7
8