import { DocumentOptions, Document, DocumentSearchResult } from 'flexsearch'; import { d as IndexedDocument } from './types-DZE2UBNA.js'; /** * `@icjia/pdf-search-index/flexsearch` — alternative search-engine * adapter for [FlexSearch](https://github.com/nextapps-de/flexsearch). * * **When to use this instead of `/fuse`.** FlexSearch hits its stride * around the corpus size where Fuse starts to slow down. Pick by * corpus: * * < 2,500 docs → Fuse.js (better typo tolerance, snippet * positions, simpler API). * 2,500 – 10,000 docs → **FlexSearch** (sub-millisecond queries, * denser index, built-in WorkerIndex). * 10,000+ docs → Pagefind (chunked on-demand index — see the * `/pagefind-emit` entry). * * **Tradeoffs vs Fuse:** * * - **No native typo tolerance.** FlexSearch's `tolerant: true` is * n-gram-based; it works but needs tuning. If "applicent" → "applicant" * is core to your UX, stay on Fuse. * - **No native match-position output.** FlexSearch returns matched * document IDs, not character-range indices. Snippet highlighting * becomes a substring-search problem; we ship * `snippetHTMLForFlexMatch` to handle the common case. * - **Encoded index format.** FlexSearch's `index.export()` produces a * denser-than-JSON serialization. We use this directly in the demo's * "Inspect the prebuilt index" panel so you can compare it side-by- * side with the Fuse equivalent. * * Added in 1.3. */ /** * Default FlexSearch Document options tuned for the `IndexedDocument` * shape. Mirrors the spirit of `DEFAULT_FUSE_OPTIONS` from `/fuse` — * sensible defaults that work for the common case (search across title * + text, store url/title/format/pages for result rendering). * * Override per call via the `options.flexOptions` argument. */ declare const DEFAULT_FLEX_OPTIONS: { readonly document: { readonly id: "id"; readonly index: readonly [{ readonly field: "title"; readonly tokenize: "forward"; }, { readonly field: "text"; readonly tokenize: "forward"; }]; readonly store: readonly ["url", "title", "format", "pages"]; }; readonly encode: "icase"; }; /** * Build a FlexSearch Document index from `IndexedDocument[]` rows. * * Pure-Node usage: * * ```ts * import { createFlexSearchIndex } from '@icjia/pdf-search-index/flexsearch'; * * const rows = await indexDocuments(urls); * const index = await createFlexSearchIndex(rows); * * // FlexSearch search API * const results = await index.search('stigma', { enrich: true }); * ``` * * Browser usage: build the index in the consumer's bundle the same way. * Or use FlexSearch's built-in `WorkerIndex` for off-main-thread search; * see https://github.com/nextapps-de/flexsearch/blob/master/doc/worker.md. * * @param rows Documents to index. * @param options Optional FlexSearch DocumentOptions override; merged * onto `DEFAULT_FLEX_OPTIONS`. */ declare function createFlexSearchIndex(rows: readonly IndexedDocument[], options?: { flexOptions?: DocumentOptions; }): Promise>; /** * Render an HTML snippet around the first occurrence of `query` in the * matched document's text. FlexSearch returns matched docs but not * character positions, so we do our own case-insensitive substring * search to locate the highlight. * * Output shape matches `snippetHTMLFor` from `/snippet` — same * ``-wrapped highlight, same `…` leading/trailing markers, same * HTML escaping for source text. * * @param row The matched IndexedDocument (from FlexSearch's * enriched result or your own row lookup). * @param query The user's search query. * @param options contextChars (default 80); matchKey (default 'text'); * collapseWhitespace (default true). */ declare function snippetHTMLForFlexMatch(row: IndexedDocument, query: string, options?: { contextChars?: number; matchKey?: 'title' | 'text'; collapseWhitespace?: boolean; }): string; /** * Helper: pull `IndexedDocument` rows out of a FlexSearch enriched * search result. FlexSearch's result shape is: * * ``` * [ { field: 'text', result: [{ id, doc }, ...] }, ... ] * ``` * * — one entry per searched field with a per-field result array. This * helper flattens + dedupes by `id` and returns the underlying rows in * the order they first appear. */ declare function flattenFlexResults(results: DocumentSearchResult): T[]; export { DEFAULT_FLEX_OPTIONS, IndexedDocument, createFlexSearchIndex, flattenFlexResults, snippetHTMLForFlexMatch };