Skip to content
English

TextSearchQuery ​

KIT · interface · 类型合同

Search input: a hard result limit and an App-owned filter, both required on every query.

导入 ​

ts
import type { TextSearchQuery } from '@jangts/agent-core/kits/search';
js
/** @typedef {import('@jangts/agent-core/kits/search').TextSearchQuery} TextSearchQuery */

其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits、@jangts/agent-core/kits/search。

定义 ​

ts
interface TextSearchQuery<Document>

泛型参数 ​

类型参数约束默认类型
Document{ id: string }—

字段结构 ​

字段类型必需说明
limitnumber必需只读。Maximum number of hits returned.
filter(document: Readonly<Partial<Document>> & { id: string; }) => boolean必需只读。Required on every query; the application owns authorization and applicability.

成员目录 ​

成员用途
filterRequired on every query; the application owns authorization and applicability.

成员详情 ​

filter ​

Required on every query; the application owns authorization and applicability.

ts
TextSearchQuery.filter(document: Readonly<Partial<Document>> & { id: string; }): boolean
参数类型传递说明
documentReadonly<Partial<Document>> & { id: string; }必需—
document 的字段
字段类型必需说明
idPartial<Document>["id"] & string必需见类型定义及调用场景。

返回: boolean。

使用示例 ​

先索引两份接口说明 ​

入门 · 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');

相关策略源码 ​

接口参考、使用示例与开发指南。