import { type Maybe } from '../value/maybe.type'; /** * Splits an identifier or free text into lowercase words. * * Splits on every non-alphanumeric character (dots, dashes, underscores, whitespace) and on camelCase / * PascalCase boundaries, so `jobWorkerTimesheet.update.reopen` becomes `['job', 'worker', 'timesheet', 'update', 'reopen']`. * * @param input - The identifier or text to split. * @returns The lowercase words, in order, with empty segments removed. * * @example * ```ts * splitSearchIdentifierWords('worker.updateSchedule'); // ['worker', 'update', 'schedule'] * splitSearchIdentifierWords('HTTPServer-v2'); // ['http', 'server', 'v2'] * ``` */ export declare function splitSearchIdentifierWords(input: string): string[]; /** * Tokenizes a search query into its unique lowercase terms. * * Uses {@link splitSearchIdentifierWords}, so a query of `updateSchedule` matches the same words as `update schedule`. * * @param query - The raw search query. * @returns The unique lowercase query tokens, in first-seen order. */ export declare function tokenizeSearchQuery(query: string): string[]; /** * Common English filler words, for the `stopWords` of {@link rankSearchEntries}. * * Only words that never carry meaning in an identifier search are listed, so a sentence-like query such as * "remove my entry from the guestbook" searches for `remove entry guestbook`. */ export declare const RANKED_SEARCH_ENGLISH_STOP_WORDS: ReadonlySet; /** * Maps a lowercase word to the words that can stand in for it in a search. Build one with {@link buildRankedSearchSynonyms}. */ export type RankedSearchSynonyms = ReadonlyMap; /** * Builds a {@link RankedSearchSynonyms} map from groups of interchangeable words. * * Every word of a group stands in for every other word of that group. A word listed in several groups gets the union. * * @param groups - The groups of interchangeable words. * @returns The lowercase synonym map. * * @example * ```ts * const synonyms = buildRankedSearchSynonyms([['delete', 'remove']]); * synonyms.get('remove'); // ['delete'] * ``` */ export declare function buildRankedSearchSynonyms(groups: readonly (readonly string[])[]): RankedSearchSynonyms; /** * Fraction of a field's exact weight that a synonym match scores, so a literal match outranks a synonym match. */ export declare const RANKED_SEARCH_SYNONYM_FACTOR = 0.5; /** * Per-field match weights used by {@link rankSearchEntries}. */ export interface RankedSearchFieldWeights { /** * Score when a token equals one of the field's words (or the whole query equals a whole value). */ readonly exact: number; /** * Score when one of the field's words starts with the token. */ readonly prefix: number; /** * Score when the token appears anywhere inside one of the field's whole values. */ readonly substring: number; } /** * Weights for a primary name/id field. Mirrors the dbx-components catalog name weights. */ export declare const RANKED_SEARCH_NAME_WEIGHTS: RankedSearchFieldWeights; /** * Weights for a secondary slug/alias field. */ export declare const RANKED_SEARCH_SLUG_WEIGHTS: RankedSearchFieldWeights; /** * Weights for a tag/category field. */ export declare const RANKED_SEARCH_TAG_WEIGHTS: RankedSearchFieldWeights; /** * Weights for a free-text field such as a description. */ export declare const RANKED_SEARCH_TEXT_WEIGHTS: RankedSearchFieldWeights; /** * Configures one searchable field of a {@link RankedSearchEntry}. * * @template T - The searched value type. */ export interface RankedSearchFieldConfig { /** * Reads the field's value(s) from an entry. Nullish values are skipped. */ readonly read: (value: T) => Maybe[]>; /** * The weights applied when a query token matches this field. */ readonly weights: RankedSearchFieldWeights; } /** * One pre-tokenized field of a {@link RankedSearchEntry}. */ export interface RankedSearchEntryField { /** * The field's whole values, lowercased. Used for substring and whole-query matches. */ readonly values: readonly string[]; /** * The unique words split from {@link values}. Used for exact and prefix matches. */ readonly words: readonly string[]; readonly weights: RankedSearchFieldWeights; } /** * A value plus its pre-tokenized searchable fields. Build these once with {@link buildRankedSearchEntries} and reuse them per query. * * @template T - The searched value type. */ export interface RankedSearchEntry { readonly value: T; readonly fields: readonly RankedSearchEntryField[]; } /** * Pre-tokenizes values for repeated {@link rankSearchEntries} queries. * * @param values - The values to index. * @param fields - The searchable fields read from each value. * @returns One entry per value, in input order. */ export declare function buildRankedSearchEntries(values: Iterable, fields: readonly RankedSearchFieldConfig[]): RankedSearchEntry[]; /** * Scores one query token against one field: the best of exact word, word prefix, or whole-value substring. * * @param field - The pre-tokenized field. * @param token - The lowercase query token. * @returns The field's score for the token, or `0` when it does not match. */ export declare function scoreRankedSearchFieldToken(field: RankedSearchEntryField, token: string): number; /** * How multiple query tokens combine. * * - `all` — an entry must match every token. * - `any` — an entry must match at least one token. */ export type RankedSearchMatchMode = 'all' | 'any'; /** * Input for {@link rankSearchEntries}. * * @template T - The searched value type. */ export interface RankSearchEntriesInput { readonly entries: Iterable>; readonly query: string; /** * Defaults to `all`. */ readonly mode?: RankedSearchMatchMode; /** * Maximum results to return. Unlimited when unset. */ readonly limit?: Maybe; /** * Optional pre-filter. Entries it rejects are never scored. */ readonly filter?: Maybe<(value: T) => boolean>; /** * Query words dropped before matching, e.g. {@link RANKED_SEARCH_ENGLISH_STOP_WORDS}. A query made only of stop words keeps * them all. */ readonly stopWords?: Maybe>; /** * Words that can stand in for a query word. A synonym counts only as an exact word of a field and scores * {@link RANKED_SEARCH_SYNONYM_FACTOR} of that field's exact weight; a token uses whichever of its literal or synonym score * is higher. */ readonly synonyms?: Maybe; } /** * One ranked match. * * @template T - The searched value type. */ export interface RankedSearchResult { readonly value: T; readonly score: number; } /** * Ranks pre-tokenized entries against a keyword query. * * Each token scores the sum of its best match in every field (see {@link scoreRankedSearchFieldToken}); an entry's score is the * sum over tokens, plus each field's exact weight when the whole query equals one of that field's values. Ties keep input order. * An empty query matches nothing. Optional `stopWords` are dropped from the query first, and optional `synonyms` let a token * match a field through another word. * * @param input - The entries, query, match mode, limit, optional filter, stop words, and synonyms. * @returns The matching entries, best first. * * @example * ```ts * const entries = buildRankedSearchEntries(methods, [{ read: (x) => x.id, weights: RANKED_SEARCH_NAME_WEIGHTS }]); * rankSearchEntries({ entries, query: 'reopen timesheet', limit: 5 }); * ``` */ export declare function rankSearchEntries(input: RankSearchEntriesInput): RankedSearchResult[]; /** * Input for {@link scoreRankedSearchEntry}. * * @template T - The searched value type. */ export interface ScoreRankedSearchEntryInput { readonly entry: RankedSearchEntry; /** * The query tokens from {@link tokenizeSearchQuery}. */ readonly tokens: readonly string[]; /** * The whole query, trimmed and lowercased. */ readonly wholeQuery: string; readonly mode: RankedSearchMatchMode; /** * Words that can stand in for a token. See {@link RankSearchEntriesInput.synonyms}. */ readonly synonyms?: Maybe; } /** * Scores one entry against the query tokens. * * @param input - The entry, tokens, whole query, match mode, and optional synonyms. * @returns The entry's score, or `undefined` when it does not match under the mode. */ export declare function scoreRankedSearchEntry(input: ScoreRankedSearchEntryInput): Maybe;