@yoch/frozenminisearch 1.2.4 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -164,11 +164,10 @@ type BinaryCompression = 'auto' | 'raw' | 'zstd' | 'zlib';
164
164
  type SaveBinaryOptions = {
165
165
  /**
166
166
  * Compression codec for the payload.
167
- * - `auto`: one pass; payloads under 64 B stay raw; otherwise zstd when available
168
- * (else zlib on Node < 22.15), kept only when strictly smaller than raw
167
+ * - `auto`: one pass; payloads under 64 B stay raw; otherwise zlib when it shrinks
169
168
  * - `raw`: never compress
170
- * - `zstd`: always zstd-compress, even when larger than raw; requires Node 22.15+ to write
171
- * - `zlib`: always deflate, even when larger than raw; readable on Node 20+
169
+ * - `zstd`: always zstd-compress; requires Node 22.15+ to write
170
+ * - `zlib`: always deflate; readable on Node 20+ and in the browser build
172
171
  */
173
172
  compression?: BinaryCompression;
174
173
  };
@@ -271,6 +270,15 @@ interface RawResultValue {
271
270
  match: MatchInfo;
272
271
  }
273
272
  type RawResult = Map<number, RawResultValue>;
273
+ /** Posting list for one (term, field): docId -> term frequency */
274
+ interface PostingListLike {
275
+ readonly size: number;
276
+ forEachDoc(callback: (docId: number, termFreq: number) => void): void;
277
+ }
278
+ /** term -> fieldId -> posting list */
279
+ interface FieldTermDataLike {
280
+ get(fieldId: number): PostingListLike | undefined;
281
+ }
274
282
  interface FinalizeSearchParams {
275
283
  rawResults: RawResult;
276
284
  getExternalId: (docId: number) => unknown;
@@ -460,6 +468,10 @@ interface FrozenPostingsLayout {
460
468
  sparseOffsets: Uint32Array | null;
461
469
  sparseLengths: Uint32Array | null;
462
470
  }
471
+ /** Single rebindable {@link FieldTermDataLike} per frozen index (O(1) RAM). */
472
+ type FrozenFieldTermFlyweight = FieldTermDataLike & {
473
+ bind(termIndex: number): FrozenFieldTermFlyweight;
474
+ };
463
475
 
464
476
  /** Adaptive-width unsigned column (1/2/4 bytes per element) for field lengths and packed radix columns. */
465
477
  type FieldLengthArray = PackedIndexArray;
@@ -564,6 +576,7 @@ declare class FrozenIndexBuilder<T> {
564
576
  private readonly _avgFieldLength;
565
577
  private readonly _seenIds;
566
578
  private readonly _fieldTermFreqScratch;
579
+ private readonly _rawTokenScratch;
567
580
  private readonly _tokenScratch;
568
581
  private _nextId;
569
582
  private _frozen;
@@ -601,27 +614,25 @@ declare class FrozenIndexBuilder<T> {
601
614
  /** Create an incremental builder for {@link FrozenMiniSearch}. */
602
615
  declare function createFrozenIndexBuilder<T>(options: Options<T>, hints?: FrozenIndexBuilderHints): FrozenIndexBuilder<T>;
603
616
 
604
- declare function frozenMemoryBreakdown(frozen: FrozenMiniSearch): FrozenMemoryBreakdown;
605
- /** Instantiate {@link FrozenMiniSearch} from pre-built flat index parts (full validation). */
606
- declare function assembleFrozen<T>(params: FrozenAssembleParams<T>): FrozenMiniSearch<T>;
607
- declare function buildFrozenFromDocuments<T>(documents: readonly T[], options: Options<T>): FrozenMiniSearch<T>;
608
- /** Finalize a {@link FrozenIndexBuilder} into a read-only index. */
609
- declare function freezeFrozenIndexBuilder<T>(builder: FrozenIndexBuilder<T>): FrozenMiniSearch<T>;
610
- declare class FrozenMiniSearch<T = any> {
611
- private readonly _options;
612
- private readonly _index;
613
- private readonly _documentCount;
614
- private readonly _nextId;
615
- private readonly _externalIds;
616
- private readonly _idLookup;
617
- private readonly _fieldIds;
618
- private readonly _fieldCount;
619
- private readonly _fieldLengthMatrix;
620
- private readonly _avgFieldLength;
621
- private readonly _storedFields;
622
- private readonly _termCount;
623
- private readonly _postings;
624
- private readonly _fieldTermFlyweight;
617
+ declare function frozenMemoryBreakdown(frozen: FrozenMiniSearchCore): FrozenMemoryBreakdown;
618
+ type FrozenMiniSearchCtor<T, I extends FrozenMiniSearchCore<T>> = new (params: FrozenAssembleParams<T>) => I;
619
+ declare class FrozenMiniSearchCore<T = any> {
620
+ protected readonly _options: OptionsWithDefaults<T>;
621
+ protected readonly _index: FrozenTermIndex;
622
+ protected readonly _documentCount: number;
623
+ protected readonly _nextId: number;
624
+ protected readonly _externalIds: unknown[];
625
+ protected readonly _idLookup: IdToShortIdLookup;
626
+ protected readonly _fieldIds: {
627
+ [field: string]: number;
628
+ };
629
+ protected readonly _fieldCount: number;
630
+ protected readonly _fieldLengthMatrix: FieldLengthArray;
631
+ protected readonly _avgFieldLength: Float32Array;
632
+ protected readonly _storedFields: StoredFieldsLayout;
633
+ protected readonly _termCount: number;
634
+ protected readonly _postings: FrozenPostingsLayout;
635
+ protected readonly _fieldTermFlyweight: FrozenFieldTermFlyweight;
625
636
  private readonly _aggregateContext;
626
637
  private readonly _queryEngineParams;
627
638
  private readonly _hasStoredFields;
@@ -638,41 +649,30 @@ declare class FrozenMiniSearch<T = any> {
638
649
  * With a `filter`, uses {@link search} so stored fields are available to the predicate.
639
650
  */
640
651
  autoSuggest(queryString: string, options?: SearchOptions): Suggestion[];
641
- /** Serialize this index as a frozen binary snapshot (synchronous). */
642
- saveBinarySync(saveOptions?: SaveBinaryOptions): Buffer;
643
- /** Non-blocking snapshot serialization with the selected compression codec. */
644
- saveBinaryAsync(saveOptions?: SaveBinaryOptions): Promise<Buffer>;
645
- /** Load a frozen binary snapshot. */
646
- static loadBinarySync<T>(buffer: Buffer, options?: Options<T>): FrozenMiniSearch<T>;
647
- /** Load a frozen binary snapshot with streaming decompression when needed (bounded memory). */
648
- static loadBinaryAsync<T>(buffer: Buffer, options?: Options<T>): Promise<FrozenMiniSearch<T>>;
649
- private static fromBinarySnapshot;
650
652
  /** Build a read-only index in one pass from documents. */
651
- static fromDocuments<T>(documents: readonly T[], options: Options<T>): FrozenMiniSearch<T>;
653
+ static fromDocuments<T, I extends FrozenMiniSearchCore<T>>(this: FrozenMiniSearchCtor<T, I>, documents: readonly T[], options: Options<T>): I;
652
654
  /**
653
655
  * Export this index as a MiniSearch wire snapshot (`serializationVersion: 2`).
654
656
  * Use for migration or interchange with the `minisearch` package (`JSON.stringify` works via this method).
655
- * Not the primary persistence format — prefer {@link saveBinarySync} for production (size and load time).
656
657
  * Term order in `index` may differ from MiniSearch native `toJSON`; search scores stay equivalent.
657
658
  */
658
659
  toJSON(): MiniSearchSnapshot;
659
660
  /**
660
661
  * Build a new frozen index **from** a MiniSearch JSON snapshot string (import / migration).
661
662
  * Accepts the wire format produced by MiniSearch `toJSON` or by {@link toJSON} on this class.
662
- * Distinct from {@link loadBinarySync}: JSON is MiniSearch interchange, not the native frozen binary.
663
663
  * No runtime dependency on the `minisearch` package.
664
664
  */
665
- static fromJson<T>(json: string, options?: Options<T>): FrozenMiniSearch<T>;
665
+ static fromJson<T, I extends FrozenMiniSearchCore<T>>(this: FrozenMiniSearchCtor<T, I>, json: string, options?: Options<T>): I;
666
666
  /**
667
667
  * Same as {@link fromJson} with a pre-parsed snapshot object.
668
668
  * `storedFields` are shallow-copied; callers must not mutate nested values
669
669
  * after load if they intend to keep the index immutable.
670
670
  */
671
- static fromMiniSearchSnapshot<T>(snapshot: MiniSearchSnapshot, options?: Options<T>): FrozenMiniSearch<T>;
671
+ static fromMiniSearchSnapshot<T, I extends FrozenMiniSearchCore<T>>(this: FrozenMiniSearchCtor<T, I>, snapshot: MiniSearchSnapshot, options?: Options<T>): I;
672
672
  /** Accepts any object exposing `toJSON()` in MiniSearch snapshot shape. */
673
- static fromMiniSearch<T>(source: {
673
+ static fromMiniSearch<T, I extends FrozenMiniSearchCore<T>>(this: FrozenMiniSearchCtor<T, I>, source: {
674
674
  toJSON(): MiniSearchSnapshot;
675
- }, options?: Options<T>): FrozenMiniSearch<T>;
675
+ }, options?: Options<T>): I;
676
676
  /**
677
677
  * Build a read-only index from an async stream of documents (e.g. CSV parser).
678
678
  * For sync iterables, use {@link createFrozenIndexBuilder} with `for...of` instead.
@@ -680,10 +680,29 @@ declare class FrozenMiniSearch<T = any> {
680
680
  * @param hints Optional builder hints; `estimatedDocumentCount` pre-allocates
681
681
  * per-document arrays when the final document count is known upfront.
682
682
  */
683
- static fromAsyncIterable<T>(iterable: AsyncIterable<T>, options: Options<T>, hints?: FrozenIndexBuilderHints): Promise<FrozenMiniSearch<T>>;
683
+ static fromAsyncIterable<T, I extends FrozenMiniSearchCore<T>>(this: FrozenMiniSearchCtor<T, I>, iterable: AsyncIterable<T>, options: Options<T>, hints?: FrozenIndexBuilderHints): Promise<I>;
684
684
  private getFieldLength;
685
685
  private executeQuery;
686
686
  }
687
687
 
688
+ /** Build a read-only Node index in one pass from documents. */
689
+ declare function buildFrozenFromDocuments<T>(documents: readonly T[], options: Options<T>): FrozenMiniSearch<T>;
690
+ /** Finalize a {@link FrozenIndexBuilder} into a read-only Node index. */
691
+ declare function freezeFrozenIndexBuilder<T>(builder: FrozenIndexBuilder<T>): FrozenMiniSearch<T>;
692
+ /** Instantiate {@link FrozenMiniSearch} from pre-built flat index parts (full validation). */
693
+ declare function assembleFrozen<T>(params: FrozenAssembleParams<T>): FrozenMiniSearch<T>;
694
+ declare class FrozenMiniSearch<T = any> extends FrozenMiniSearchCore<T> {
695
+ /** Serialize this index as a frozen binary snapshot (synchronous). */
696
+ saveBinarySync(saveOptions?: SaveBinaryOptions): Buffer;
697
+ /** Non-blocking snapshot serialization with the selected compression codec. */
698
+ saveBinaryAsync(saveOptions?: SaveBinaryOptions): Promise<Buffer>;
699
+ private binarySnapshotInput;
700
+ /** Load a frozen binary snapshot. */
701
+ static loadBinarySync<T>(buffer: Buffer, options?: Options<T>): FrozenMiniSearch<T>;
702
+ /** Load a frozen binary snapshot with streaming decompression when needed (bounded memory). */
703
+ static loadBinaryAsync<T>(buffer: Buffer, options?: Options<T>): Promise<FrozenMiniSearch<T>>;
704
+ private static fromBinarySnapshot;
705
+ }
706
+
688
707
  export { AND, AND_NOT, FrozenIndexBuilder, FrozenMiniSearch, OR, assembleFrozen, buildFrozenFromDocuments, createFrozenIndexBuilder, FrozenMiniSearch as default, finalizeRawSearchResults, finalizeSearchResults, freezeFrozenIndexBuilder, frozenMemoryBreakdown, suggestFromRawResults, suggestFromSearchResults };
689
708
  export type { BM25Params, BinaryCompression, CombinationOperator, FrozenAssembleParams, FrozenIndexBuilderHints, FrozenMemoryBreakdown, LogLevel, LowercaseCombinationOperator, MatchInfo, MiniSearchSnapshot, Options, Query, QueryCombination, SaveBinaryOptions, SearchOptions, SearchResult, SerializedIndexEntry, Suggestion, Wildcard };