Skip to content
中文

TextSearch ​

KIT · class · Runtime value

Optional App-side full-text index. No storage, identity, model calls or Core dependencies.

Only declared fields are indexed and only storedFields come back with a hit, so a document never leaks extra properties into search results.

Import ​

ts
import { TextSearch } from '@jangts/agent-core/kits/search';
js
import { TextSearch } from '@jangts/agent-core/kits/search';

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits, @jangts/agent-core/kits/search.

Definition ​

ts
class TextSearch<Document>

Type parameters ​

Type parameterConstraintDefault type
Document{ id: string }—

Constructor ​

ts
new TextSearch<Document extends { id: string; }>(registration: TextSearchOptions<Document>): TextSearch<Document>

Compile the index configuration and validate it before any document is added.

ParameterTypePassingDescription
registrationTextSearchOptions<Document>Required—
Fields of registration
FieldTypeRequiredDescription
indexRevisionstringRequiredRead-only. App-owned revision of the field/tokenizer configuration; a change invalidates snapshots.
fieldsreadonly (keyof Document & string)[]RequiredRead-only. Fields that are indexed and searchable.
storedFieldsreadonly (keyof Document & string)[]RequiredRead-only. Fields returned in a hit; the indexed body is not implicitly returned.
tokenize((text: string, fieldName?: string) => string[]) | undefinedOptionalRead-only. Tokenizer override; defaults to MiniSearch's default tokenizer.
boostReadonly<Partial<Record<keyof Document & string, number>>> | undefinedOptionalRead-only. Per-field relevance multiplier; omitted fields keep the default weight.
combineWith"AND" | "OR" | undefinedOptionalRead-only. How multiple query terms combine; defaults to the engine default (OR).

Returns: TextSearch<Document>.

Member index ​

MemberPurpose
restoreStatic. Rebuild an index from a snapshot.
upsertInsert or replace one document.
searchSearch the index and return at most limit hits.
removeRemove one document. Vacuum before returning so deleted terms do not survive the next serialized checkpoint.
clearDrop every document while keeping the compiled configuration.
snapshotSerialize the index together with the configuration used to build it.

Member details ​

restore ​

Static member.

Rebuild an index from a snapshot.

ts
TextSearch.restore<Document extends { id: string; }>(definition: TextSearchOptions<Document>, snapshot: TextSearchSnapshot): TextSearch<Document>

Rebuild an index from a snapshot.

Type parameterConstraintDefault type
Document{ id: string; }—
ParameterTypePassingDescription
definitionTextSearchOptions<Document>Required—
snapshotTextSearchSnapshotRequired—
Fields of definition
FieldTypeRequiredDescription
indexRevisionstringRequiredRead-only. App-owned revision of the field/tokenizer configuration; a change invalidates snapshots.
fieldsreadonly (keyof Document & string)[]RequiredRead-only. Fields that are indexed and searchable.
storedFieldsreadonly (keyof Document & string)[]RequiredRead-only. Fields returned in a hit; the indexed body is not implicitly returned.
tokenize((text: string, fieldName?: string) => string[]) | undefinedOptionalRead-only. Tokenizer override; defaults to MiniSearch's default tokenizer.
boostReadonly<Partial<Record<keyof Document & string, number>>> | undefinedOptionalRead-only. Per-field relevance multiplier; omitted fields keep the default weight.
combineWith"AND" | "OR" | undefinedOptionalRead-only. How multiple query terms combine; defaults to the engine default (OR).
Fields of snapshot
FieldTypeRequiredDescription
version2RequiredRead-only. Snapshot format version; other versions are not loaded.
engine"minisearch-7"RequiredRead-only. Engine that produced the snapshot; a different engine cannot restore it.
configurationFingerprintstringRequiredRead-only. Fingerprint of the field and tokenizer configuration that built the index.
serializedIndexstringRequiredRead-only. Serialized MiniSearch index.

Returns: TextSearch<Document>.

upsert ​

Instance method.

Insert or replace one document.

ts
upsert(document: Document): void

Insert or replace one document.

ParameterTypePassingDescription
documentDocumentRequired—

Returns: void.

Instance method.

Search the index and return at most limit hits.

ts
search(options: TextSearchQuery<Document> & { query: string; }): TextSearchHit<Document>[]

Search the index and return at most limit hits.

ParameterTypePassingDescription
optionsTextSearchQuery<Document> & { query: string; }Required—
Fields of options
FieldTypeRequiredDescription
limitnumberRequiredRead-only. Maximum number of hits returned.
filter(document: Readonly<Partial<Document>> & { id: string; }) => booleanRequiredRead-only. Required on every query; the application owns authorization and applicability.
querystringRequiredSee the type definition and usage context.

Returns: TextSearchHit<Document>[].

remove ​

Instance method.

Remove one document.

Vacuum before returning so deleted terms do not survive the next serialized checkpoint.

ts
remove(id: string): Promise<boolean>

Remove one document.

Vacuum before returning so deleted terms do not survive the next serialized checkpoint.

ParameterTypePassingDescription
idstringRequired—

Returns: Promise<boolean>.

Awaited value: boolean.

clear ​

Instance method.

Drop every document while keeping the compiled configuration.

ts
clear(): void

Drop every document while keeping the compiled configuration.

No call parameters.

Returns: void.

snapshot ​

Instance method.

Serialize the index together with the configuration used to build it.

ts
snapshot(): TextSearchSnapshot

Serialize the index together with the configuration used to build it.

No call parameters.

Returns: TextSearchSnapshot.

Return value structure
FieldTypeRequiredDescription
version2RequiredRead-only. Snapshot format version; other versions are not loaded.
engine"minisearch-7"RequiredRead-only. Engine that produced the snapshot; a different engine cannot restore it.
configurationFingerprintstringRequiredRead-only. Fingerprint of the field and tokenizer configuration that built the index.
serializedIndexstringRequiredRead-only. Serialized MiniSearch index.

Examples ​

First index two interface documents ​

Beginner · 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 }));

Configuration · Node.js

Metadata allowed for filtering must be registered as storedFields; field filtering must rely on a trusted App identity and must not trust an identity the model claims.

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');

API reference, usage examples and developer guides.