@delali/narsil 0.3.1-next.73 → 0.3.1-next.75

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.
Files changed (54) hide show
  1. package/dist/{capabilities-CYeAWYxQ.d.ts → capabilities-BHNksicw.d.ts} +16 -2
  2. package/dist/{chunk-PU43WDY4.mjs → chunk-2E35UM3G.mjs} +1 -13
  3. package/dist/{chunk-AK3BLGCI.mjs → chunk-356UOTK2.mjs} +15 -48
  4. package/dist/chunk-7ODNRREZ.mjs +2050 -0
  5. package/dist/chunk-AIUOAN7Y.mjs +658 -0
  6. package/dist/{chunk-WVDFG45M.mjs → chunk-AMGDG4ZJ.mjs} +3 -2
  7. package/dist/{chunk-AFKH4ZK3.mjs → chunk-BT62MUM5.mjs} +1 -1
  8. package/dist/{chunk-LPBWCHBE.mjs → chunk-BT6BI4GV.mjs} +42 -4
  9. package/dist/{chunk-LJYTW7GT.mjs → chunk-IBR6634K.mjs} +1 -1
  10. package/dist/chunk-KEHK2O6P.mjs +13 -0
  11. package/dist/{chunk-CVFAFZSN.mjs → chunk-LOARLB4F.mjs} +46 -34
  12. package/dist/{chunk-3YKWSO54.mjs → chunk-NAMGDK45.mjs} +228 -649
  13. package/dist/{chunk-2WLD2NFW.mjs → chunk-PXDY5VRT.mjs} +3 -3
  14. package/dist/chunk-UOJIV2NG.mjs +593 -0
  15. package/dist/chunk-V3B24XR7.mjs +54 -0
  16. package/dist/{chunk-ZULSEZCE.mjs → chunk-WL2GDSCV.mjs} +100 -19
  17. package/dist/{chunk-THMSFOJO.mjs → chunk-YOVS4EV5.mjs} +582 -598
  18. package/dist/{client-D698E5ge.d.ts → client-D4sXFS9f.d.ts} +3 -3
  19. package/dist/client.d.ts +8 -8
  20. package/dist/{config-Bv8etwUZ.d.ts → config-B-GRonqw.d.ts} +2 -2
  21. package/dist/{config-QXU3HMpq.d.ts → config-C1kbq1X2.d.ts} +2 -2
  22. package/dist/direct-executor-AVNWB73C.mjs +16 -0
  23. package/dist/distribution/coordinator/etcd.d.ts +2 -2
  24. package/dist/distribution/coordinator/etcd.mjs +1 -1
  25. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  26. package/dist/distribution/coordinator/in-memory.mjs +1 -1
  27. package/dist/distribution.d.ts +8 -8
  28. package/dist/distribution.mjs +23 -13
  29. package/dist/{engine-D_OzKZ-6.d.ts → engine-DcGSd5vh.d.ts} +2 -2
  30. package/dist/index.browser.mjs +1272 -834
  31. package/dist/index.d.ts +27 -8
  32. package/dist/index.mjs +14 -11
  33. package/dist/invalidation/filesystem.mjs +1 -1
  34. package/dist/{memory-B4jMmygw.d.ts → memory-M9kPmYP8.d.ts} +1257 -1178
  35. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  36. package/dist/partial-words.d.ts +22 -0
  37. package/dist/partial-words.mjs +11 -0
  38. package/dist/pattern.mjs +2 -2
  39. package/dist/persistence/durability/checkpoint-worker.d.ts +33 -13
  40. package/dist/persistence/durability/checkpoint-worker.mjs +14 -11
  41. package/dist/react.d.ts +7 -7
  42. package/dist/{schema-BBOCdhND.d.ts → schema-DgyDijhf.d.ts} +6 -2
  43. package/dist/server.d.ts +10 -10
  44. package/dist/server.mjs +36 -11
  45. package/dist/{tasks-DpgCpW1x.d.ts → tasks-A8se0J98.d.ts} +1 -1
  46. package/dist/{thread-3LNVA43V.mjs → thread-BSIKT6ED.mjs} +16 -9
  47. package/dist/{types-BiUmaiWT.d.ts → types-ChrVy4lH.d.ts} +1 -1
  48. package/dist/vector/search-worker.d.ts +1 -1
  49. package/dist/vector/search-worker.mjs +4 -3
  50. package/dist/workers/entry.mjs +3 -3
  51. package/package.json +16 -7
  52. package/dist/chunk-XW25X6AU.mjs +0 -614
  53. package/dist/chunk-ZKRV5M2P.mjs +0 -75
  54. package/dist/direct-executor-5SRDFZAS.mjs +0 -13
@@ -1,462 +1,45 @@
1
- import { isWordIndexedBase, flattenSchema, MAX_LATITUDE_DEGREES, MAX_LONGITUDE_DEGREES, MIN_POLYGON_POINTS, parsedTypeOf, isStringValuedBase, withSortableOption, isGeopointOrVectorType, MAX_SORT_FIELDS, MAX_SORT_FIELD_NAME_LENGTH, MAX_CURSOR_LENGTH, MAX_CURSOR_ANCHOR_CODE_POINTS, RESULT_WINDOW, DEFAULT_FUZZY_PREFIX_LENGTH, EARTH_RADIUS_METERS, RADIUS_BOX_MARGIN_RATIO, RADIUS_BOX_MARGIN_RADIANS, FULL_TURN_DEGREES, ANTIMERIDIAN_SPAN_DEGREES, DEFAULT_PAGE_SIZE, PROTOTYPE_POLLUTION_KEYS, DEG_TO_RAD, LOCATION_CODE_BITS_PER_AXIS, isLatitude, isLongitude, isWordIndexedFieldType, PREFIX_MAX_EXPANSIONS, MAX_COVERING_CELLS } from './chunk-AK3BLGCI.mjs';
2
- import { DEFAULT_PATTERN_WORK_CAP, PATTERN_TEST_OPERATORS, bitsetIsEmpty, MAX_PATTERN_TEXT_CODE_POINTS, MAX_PATTERN_POSITIONS, createBitSet, bitsetSet, PATTERN_WORK_FLUSH_UNITS, MATCHER_STATE_BUDGET_BYTES } from './chunk-ZKRV5M2P.mjs';
3
- import { compareCodePoints, sortModeOf, defaultSortMode, SORT_VALUE_MAX_CODE_POINTS, toReducedSortValue, readSortField, compareComparableKeys, toComparableSortValue, exceedsCodePoints, holdsUnpairedSurrogate, isSortMode, SORT_MODES, compareSortValues, foldedCodePointCount } from './chunk-LJYTW7GT.mjs';
4
- import { createNarsilError, ErrorCodes, NarsilError } from './chunk-I64LXKO3.mjs';
1
+ import { MAX_SORT_FIELDS, MAX_SORT_FIELD_NAME_LENGTH, MAX_CURSOR_LENGTH, MAX_CURSOR_ANCHOR_CODE_POINTS, RESULT_WINDOW, DEFAULT_FUZZY_PREFIX_LENGTH, MAX_FILTER_DEPTH, DEFAULT_PAGE_SIZE, MAX_FILTER_ARRAY_SIZE, GEO_UNIT_METRES, MAX_FILTER_TESTS, PREFIX_MAX_EXPANSIONS, PARTIAL_WORD_MIN_CODE_POINTS, PARTIAL_WORDS_ADDED_PER_QUERY } from './chunk-V3B24XR7.mjs';
2
+ import { DEFAULT_PATTERN_WORK_CAP, patternSearchModule, PATTERN_TEST_OPERATORS, bitsetIsEmpty, PATTERN_EXPRESSION_OPERATORS, MAX_PATTERN_POSITIONS, createBitSet, bitsetSet, MAX_PATTERN_TEXT_CODE_POINTS, PATTERN_WORK_FLUSH_UNITS, MATCHER_STATE_BUDGET_BYTES } from './chunk-7ODNRREZ.mjs';
3
+ import { getFlatSchema, tokenize, getFieldValueByInternalId, getFieldValueForDoc, fuzzyTermMatches } from './chunk-UOJIV2NG.mjs';
4
+ import { schemaFieldsOf, MAX_LATITUDE_DEGREES, MAX_LONGITUDE_DEGREES, MIN_POLYGON_POINTS, flattenSchema, parsedTypeOf, isStringValuedBase, withSortableOption, isGeopointOrVectorType, EARTH_RADIUS_METERS, RADIUS_BOX_MARGIN_RATIO, RADIUS_BOX_MARGIN_RADIANS, FULL_TURN_DEGREES, ANTIMERIDIAN_SPAN_DEGREES, PROTOTYPE_POLLUTION_KEYS, DEG_TO_RAD, LOCATION_CODE_BITS_PER_AXIS, isLatitude, isLongitude, isWordIndexedFieldType, partialFieldsOf, MAX_COVERING_CELLS } from './chunk-356UOTK2.mjs';
5
+ import { appendFoldedCodePoint, compareCodePoints, sortModeOf, defaultSortMode, SORT_VALUE_MAX_CODE_POINTS, toReducedSortValue, readSortField, compareComparableKeys, toComparableSortValue, exceedsCodePoints, singleFoldTable, holdsUnpairedSurrogate, isSortMode, SORT_MODES, compareSortValues, foldedCodePointCount, codePointCount } from './chunk-IBR6634K.mjs';
6
+ import { NarsilError, ErrorCodes } from './chunk-I64LXKO3.mjs';
5
7
 
6
- // src/core/tokenizer/constants.ts
7
- var TOKEN_CACHE_SIZE_FLOOR = 5e4;
8
- var TOKEN_CACHE_SIZE_CEILING = 2e6;
9
- var TOKEN_CACHE_BYTES_PER_ENTRY = 200;
10
- var NODE_TOKEN_CACHE_ENTRIES = 1e6;
11
- var UNKNOWN_MEMORY_TOKEN_CACHE_ENTRIES = 2e5;
12
- var CONSTRAINED_MEMORY_TOKEN_CACHE_FRACTION = 0.05;
13
- var SMALL_DEVICE_MEMORY_GB = 1;
14
- var MEDIUM_DEVICE_MEMORY_GB = 4;
15
- var SMALL_DEVICE_TOKEN_CACHE_ENTRIES = 1e5;
16
- var MEDIUM_DEVICE_TOKEN_CACHE_ENTRIES = 25e4;
17
- var LARGE_DEVICE_TOKEN_CACHE_ENTRIES = 5e5;
18
- var DEFAULT_MIN_TOKEN_LENGTH = 1;
19
-
20
- // src/core/tokenizer/normalize.ts
21
- var DOTTED_CAPITAL_I = "\u0130";
22
- var COMBINING_DOT_ABOVE = "\u0307";
23
- var ARMENIAN_MARKS = /[\u055B-\u055F]/;
24
- var FOLDED_FORMS = /\u0307|[\u0130\u055B-\u055F\u2018\u2019\u02BC\u02BB]/g;
25
- var WIDTH_FORMS = /[\uFF01-\uFF5E\uFF61-\uFF9F]/;
26
- var WIDTH_FORMS_GLOBAL = /[\uFF01-\uFF5E\uFF61-\uFF9F]/g;
27
- var FULLWIDTH_ASCII_END = 65374;
28
- var FULLWIDTH_ASCII_OFFSET = 65248;
29
- var HALFWIDTH_KANA_START = 65377;
30
- var HALFWIDTH_VOICED_MARK = 65438;
31
- var HALFWIDTH_SEMI_VOICED_MARK = 65439;
32
- var COMBINING_VOICED_MARK = "\u3099";
33
- var COMBINING_SEMI_VOICED_MARK = "\u309A";
34
- var HALFWIDTH_KANA_TARGETS = "\u3002\u300C\u300D\u3001\u30FB\u30F2\u30A1\u30A3\u30A5\u30A7\u30A9\u30E3\u30E5\u30E7\u30C3\u30FC\u30A2\u30A4\u30A6\u30A8\u30AA\u30AB\u30AD\u30AF\u30B1\u30B3\u30B5\u30B7\u30B9\u30BB\u30BD\u30BF\u30C1\u30C4\u30C6\u30C8\u30CA\u30CB\u30CC\u30CD\u30CE\u30CF\u30D2\u30D5\u30D8\u30DB\u30DE\u30DF\u30E0\u30E1\u30E2\u30E4\u30E6\u30E8\u30E9\u30EA\u30EB\u30EC\u30ED\u30EF\u30F3";
35
- function foldForm(match) {
36
- if (match === DOTTED_CAPITAL_I) return "i";
37
- if (match === COMBINING_DOT_ABOVE || ARMENIAN_MARKS.test(match)) return "";
38
- return "'";
39
- }
40
- function foldWidth(match) {
41
- const codePoint = match.charCodeAt(0);
42
- if (codePoint <= FULLWIDTH_ASCII_END) return String.fromCharCode(codePoint - FULLWIDTH_ASCII_OFFSET);
43
- if (codePoint === HALFWIDTH_VOICED_MARK) return COMBINING_VOICED_MARK;
44
- if (codePoint === HALFWIDTH_SEMI_VOICED_MARK) return COMBINING_SEMI_VOICED_MARK;
45
- return HALFWIDTH_KANA_TARGETS[codePoint - HALFWIDTH_KANA_START];
46
- }
47
- function isAscii(text) {
48
- for (let i = 0; i < text.length; i++) {
49
- if (text.charCodeAt(i) > 127) return false;
50
- }
51
- return true;
52
- }
53
- function foldWidthForms(text) {
54
- if (!WIDTH_FORMS.test(text)) return text;
55
- return text.replace(WIDTH_FORMS_GLOBAL, foldWidth);
56
- }
57
- function normalizeForSplitting(text) {
58
- if (isAscii(text)) return text.toLowerCase();
59
- return foldWidthForms(text).normalize("NFC").replace(FOLDED_FORMS, foldForm).toLowerCase();
60
- }
61
- function stripDiacritics(text) {
62
- return text.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
8
+ // src/core/pattern-index/platform.ts
9
+ function platformPatternSearch() {
10
+ return patternSearchModule;
63
11
  }
64
12
 
65
- // src/core/tokenizer/cache.ts
66
- function clampCacheSize(entries) {
67
- return Math.max(TOKEN_CACHE_SIZE_FLOOR, Math.min(entries, TOKEN_CACHE_SIZE_CEILING));
13
+ // src/core/pattern-index/registry.ts
14
+ var registered = null;
15
+ function isPatternSearchModule(value) {
16
+ return value !== null && typeof value === "object" && value.name === "pattern" && "createIndex" in value && typeof value.createIndex === "function" && "readArrays" in value && typeof value.readArrays === "function" && "mergeArrays" in value && typeof value.mergeArrays === "function" && "matchInto" in value && typeof value.matchInto === "function" && "checkExpression" in value && typeof value.checkExpression === "function" && "expressionFootprint" in value && typeof value.expressionFootprint === "function";
68
17
  }
69
- function computeDefaultCacheSize() {
70
- try {
71
- if (typeof process !== "undefined" && typeof process.versions?.node === "string" && typeof window === "undefined") {
72
- const constrainedMemory = typeof process.constrainedMemory === "function" ? process.constrainedMemory() : 0;
73
- if (constrainedMemory > 0 && constrainedMemory < Number.MAX_SAFE_INTEGER) {
74
- const budget = constrainedMemory * CONSTRAINED_MEMORY_TOKEN_CACHE_FRACTION;
75
- return clampCacheSize(Math.floor(budget / TOKEN_CACHE_BYTES_PER_ENTRY));
76
- }
77
- return clampCacheSize(NODE_TOKEN_CACHE_ENTRIES);
78
- }
79
- if (typeof navigator !== "undefined") {
80
- const mem = navigator.deviceMemory;
81
- if (typeof mem === "number" && mem > 0) {
82
- let entries;
83
- if (mem <= SMALL_DEVICE_MEMORY_GB) entries = SMALL_DEVICE_TOKEN_CACHE_ENTRIES;
84
- else if (mem <= MEDIUM_DEVICE_MEMORY_GB) entries = MEDIUM_DEVICE_TOKEN_CACHE_ENTRIES;
85
- else entries = LARGE_DEVICE_TOKEN_CACHE_ENTRIES;
86
- return clampCacheSize(entries);
87
- }
88
- }
89
- } catch {
90
- return clampCacheSize(UNKNOWN_MEMORY_TOKEN_CACHE_ENTRIES);
91
- }
92
- return clampCacheSize(UNKNOWN_MEMORY_TOKEN_CACHE_ENTRIES);
93
- }
94
- var normalizationCache = /* @__PURE__ */ new Map();
95
- var normalizationCacheSize = 0;
96
- var maxCacheSize = computeDefaultCacheSize();
97
- var cachedLangName = "";
98
- var cachedFlags = "";
99
- var cachedBucket = null;
100
- function configureNormalizationCache(maxSize) {
101
- if (!Number.isFinite(maxSize)) {
102
- throw createNarsilError(ErrorCodes.CONFIG_INVALID, "tokenizerCacheSize must be a finite number", {
103
- received: maxSize
104
- });
105
- }
106
- if (maxSize < 0) {
107
- throw createNarsilError(ErrorCodes.CONFIG_INVALID, "tokenizerCacheSize must not be negative", {
108
- received: maxSize
109
- });
110
- }
111
- if (maxSize === 0) {
112
- throw createNarsilError(
18
+ function registerPatternSearch(module) {
19
+ if (!isPatternSearchModule(module)) {
20
+ throw new NarsilError(
113
21
  ErrorCodes.CONFIG_INVALID,
114
- "tokenizerCacheSize must be greater than zero; the normalization cache cannot be disabled",
115
- {
116
- received: maxSize
117
- }
22
+ 'registerPatternSearch takes the patternSearch object that "@delali/narsil/pattern" exports'
118
23
  );
119
24
  }
120
- maxCacheSize = Math.max(TOKEN_CACHE_SIZE_FLOOR, Math.min(Math.floor(maxSize), TOKEN_CACHE_SIZE_CEILING));
121
- if (normalizationCacheSize > maxCacheSize) {
122
- evictOldestEntries(normalizationCacheSize - maxCacheSize);
123
- }
124
- }
125
- function getCacheBucket(language, flags) {
126
- const langKey = `${language.name}@${language.revision}`;
127
- if (cachedBucket && langKey === cachedLangName && flags === cachedFlags) return cachedBucket;
128
- const key = `${langKey}:${flags}`;
129
- let bucket = normalizationCache.get(key);
130
- if (!bucket) {
131
- bucket = /* @__PURE__ */ new Map();
132
- normalizationCache.set(key, bucket);
133
- }
134
- cachedLangName = langKey;
135
- cachedFlags = flags;
136
- cachedBucket = bucket;
137
- return bucket;
138
- }
139
- function evictOldestEntries(count) {
140
- let remaining = count;
141
- for (const [key, bucket] of normalizationCache) {
142
- if (remaining <= 0) break;
143
- if (bucket.size <= remaining) {
144
- remaining -= bucket.size;
145
- normalizationCacheSize -= bucket.size;
146
- normalizationCache.delete(key);
147
- if (bucket === cachedBucket) cachedBucket = null;
148
- } else {
149
- let deleted = 0;
150
- for (const raw of bucket.keys()) {
151
- if (deleted >= remaining) break;
152
- bucket.delete(raw);
153
- deleted++;
154
- }
155
- normalizationCacheSize -= deleted;
156
- remaining = 0;
157
- }
158
- }
159
- }
160
- function transformToken(raw, language, stem, removeDiacritics) {
161
- const flags = (stem ? "s" : "") + (removeDiacritics ? "d" : "");
162
- let bucket = getCacheBucket(language, flags);
163
- const cached = bucket.get(raw);
164
- if (cached !== void 0) return cached;
165
- let normalized = raw;
166
- if (language.normalizer) {
167
- normalized = language.normalizer(normalized);
168
- }
169
- if (removeDiacritics) {
170
- normalized = stripDiacritics(normalized);
171
- }
172
- if (stem && language.stemmer) {
173
- normalized = language.stemmer(normalized);
174
- }
175
- if (normalizationCacheSize >= maxCacheSize) {
176
- evictOldestEntries(Math.max(1, maxCacheSize >>> 2));
177
- bucket = getCacheBucket(language, flags);
178
- }
179
- bucket.set(raw, normalized);
180
- normalizationCacheSize++;
181
- return normalized;
25
+ registered = module;
182
26
  }
183
- function clearNormalizationCache() {
184
- normalizationCache.clear();
185
- normalizationCacheSize = 0;
186
- cachedBucket = null;
27
+ function availablePatternSearch() {
28
+ return registered ?? platformPatternSearch();
187
29
  }
188
- function resetNormalizationCache() {
189
- clearNormalizationCache();
190
- maxCacheSize = computeDefaultCacheSize();
191
- }
192
- function getNormalizationCacheSize() {
193
- return normalizationCacheSize;
194
- }
195
-
196
- // src/core/tokenizer/ngrams.ts
197
- var OTHER_SCRIPT = 0;
198
- var HAN_SCRIPT = 1;
199
- var HIRAGANA_SCRIPT = 2;
200
- var KATAKANA_SCRIPT = 3;
201
- var HANGUL_SCRIPT = 4;
202
- var THAI_SCRIPT = 5;
203
- var LAO_SCRIPT = 6;
204
- var KHMER_SCRIPT = 7;
205
- var MYANMAR_SCRIPT = 8;
206
- var PROLONGED_SOUND_MARK = 12540;
207
- var IDEOGRAPHIC_ITERATION_MARK = 12293;
208
- var LOWEST_NGRAM_CODE_POINT = 3584;
209
- var LOWEST_MARK_CODE_POINT = 768;
210
- var THAI_BLOCK_END = 3711;
211
- var LAO_BLOCK_END = 3839;
212
- var MYANMAR_BLOCK_START = 4096;
213
- var MYANMAR_BLOCK_END = 4255;
214
- var KHMER_BLOCK_START = 6016;
215
- var KHMER_BLOCK_END = 6655;
216
- var SOUTHEAST_ASIAN_BLOCKS_END = 6656;
217
- var HIRAGANA_BLOCK_START = 12352;
218
- var HIRAGANA_BLOCK_END = 12447;
219
- var COMBINING_VOICED_MARK2 = 12441;
220
- var COMBINING_SEMI_VOICED_MARK2 = 12442;
221
- var KATAKANA_BLOCK_START = 12448;
222
- var KATAKANA_BLOCK_END = 12543;
223
- var HAN_EXTENSION_A_START = 13312;
224
- var HAN_EXTENSION_A_END = 19903;
225
- var HAN_UNIFIED_START = 19968;
226
- var HAN_UNIFIED_END = 40959;
227
- var HANGUL_SYLLABLE_START = 44032;
228
- var HANGUL_SYLLABLE_END = 55203;
229
- var HAN_COMPATIBILITY_START = 63744;
230
- var HAN_COMPATIBILITY_END = 64255;
231
- var SUPPLEMENTARY_PLANE_START = 65536;
232
- var MARK = /\p{M}/uy;
233
- var HAN = /\p{Script=Han}/uy;
234
- var HIRAGANA = /\p{Script=Hiragana}/uy;
235
- var KATAKANA = /\p{Script=Katakana}/uy;
236
- var HANGUL = /\p{Script=Hangul}/uy;
237
- var THAI = /\p{Script=Thai}/uy;
238
- var LAO = /\p{Script=Lao}/uy;
239
- var KHMER = /\p{Script=Khmer}/uy;
240
- var MYANMAR = /\p{Script=Myanmar}/uy;
241
- function matchesAt(pattern, part, index) {
242
- pattern.lastIndex = index;
243
- return pattern.test(part);
244
- }
245
- function isMark(part, index, codePoint) {
246
- if (codePoint < LOWEST_MARK_CODE_POINT) return false;
247
- if (codePoint >= HAN_UNIFIED_START && codePoint <= HAN_UNIFIED_END) return false;
248
- if (codePoint >= HIRAGANA_BLOCK_START && codePoint <= KATAKANA_BLOCK_END) {
249
- return codePoint === COMBINING_VOICED_MARK2 || codePoint === COMBINING_SEMI_VOICED_MARK2;
250
- }
251
- if (codePoint >= HANGUL_SYLLABLE_START && codePoint <= HANGUL_SYLLABLE_END) return false;
252
- if (codePoint >= HAN_EXTENSION_A_START && codePoint <= HAN_EXTENSION_A_END) return false;
253
- return matchesAt(MARK, part, index);
254
- }
255
- function classifyOutsideCommonBlocks(part, index) {
256
- if (matchesAt(HAN, part, index)) return HAN_SCRIPT;
257
- if (matchesAt(HIRAGANA, part, index)) return HIRAGANA_SCRIPT;
258
- if (matchesAt(KATAKANA, part, index)) return KATAKANA_SCRIPT;
259
- if (matchesAt(HANGUL, part, index)) return HANGUL_SCRIPT;
260
- if (matchesAt(THAI, part, index)) return THAI_SCRIPT;
261
- if (matchesAt(LAO, part, index)) return LAO_SCRIPT;
262
- if (matchesAt(KHMER, part, index)) return KHMER_SCRIPT;
263
- if (matchesAt(MYANMAR, part, index)) return MYANMAR_SCRIPT;
264
- return OTHER_SCRIPT;
265
- }
266
- function classifySoutheastAsian(part, index, codePoint) {
267
- if (codePoint <= THAI_BLOCK_END) return matchesAt(THAI, part, index) ? THAI_SCRIPT : OTHER_SCRIPT;
268
- if (codePoint <= LAO_BLOCK_END) return matchesAt(LAO, part, index) ? LAO_SCRIPT : OTHER_SCRIPT;
269
- if (codePoint >= MYANMAR_BLOCK_START && codePoint <= MYANMAR_BLOCK_END) {
270
- return matchesAt(MYANMAR, part, index) ? MYANMAR_SCRIPT : OTHER_SCRIPT;
271
- }
272
- if (codePoint >= KHMER_BLOCK_START && codePoint <= KHMER_BLOCK_END) {
273
- return matchesAt(KHMER, part, index) ? KHMER_SCRIPT : OTHER_SCRIPT;
274
- }
275
- return classifyOutsideCommonBlocks(part, index);
276
- }
277
- function classify(part, index, codePoint, current) {
278
- if (codePoint < LOWEST_NGRAM_CODE_POINT) return OTHER_SCRIPT;
279
- if (codePoint < SOUTHEAST_ASIAN_BLOCKS_END) return classifySoutheastAsian(part, index, codePoint);
280
- if (codePoint === PROLONGED_SOUND_MARK) {
281
- return current === HIRAGANA_SCRIPT ? HIRAGANA_SCRIPT : KATAKANA_SCRIPT;
282
- }
283
- if (codePoint >= HIRAGANA_BLOCK_START && codePoint <= HIRAGANA_BLOCK_END) return HIRAGANA_SCRIPT;
284
- if (codePoint >= KATAKANA_BLOCK_START && codePoint <= KATAKANA_BLOCK_END) return KATAKANA_SCRIPT;
285
- if (codePoint >= HAN_UNIFIED_START && codePoint <= HAN_UNIFIED_END) return HAN_SCRIPT;
286
- if (codePoint >= HAN_EXTENSION_A_START && codePoint <= HAN_EXTENSION_A_END) return HAN_SCRIPT;
287
- if (codePoint >= HANGUL_SYLLABLE_START && codePoint <= HANGUL_SYLLABLE_END) return HANGUL_SCRIPT;
288
- if (codePoint >= HAN_COMPATIBILITY_START && codePoint <= HAN_COMPATIBILITY_END) return HAN_SCRIPT;
289
- if (codePoint === IDEOGRAPHIC_ITERATION_MARK) return HAN_SCRIPT;
290
- return classifyOutsideCommonBlocks(part, index);
291
- }
292
- function emitRun(part, offsets, kind, size, out) {
293
- const count = offsets.length - 1;
294
- if (count < 1) return;
295
- if (kind === OTHER_SCRIPT || count <= size) {
296
- out.push(part.slice(offsets[0], offsets[count]));
297
- return;
30
+ function requirePatternSearchFor(schema) {
31
+ for (const { path, pattern } of schemaFieldsOf(schema)) {
32
+ if (pattern) requirePatternSearch(path);
298
33
  }
299
- for (let start = 0; start + size <= count; start++) {
300
- out.push(part.slice(offsets[start], offsets[start + size]));
301
- }
302
- }
303
- function expandNgrams(part, size, out) {
304
- const length = part.length;
305
- if (length === 0) return;
306
- const offsets = [];
307
- let kind = OTHER_SCRIPT;
308
- let index = 0;
309
- while (index < length) {
310
- const codePoint = part.codePointAt(index);
311
- if (codePoint === void 0) break;
312
- const width = codePoint >= SUPPLEMENTARY_PLANE_START ? 2 : 1;
313
- if (offsets.length > 0 && isMark(part, index, codePoint)) {
314
- index += width;
315
- continue;
316
- }
317
- const next = classify(part, index, codePoint, kind);
318
- if (offsets.length > 0 && next !== kind) {
319
- offsets.push(index);
320
- emitRun(part, offsets, kind, size, out);
321
- offsets.length = 0;
322
- }
323
- kind = next;
324
- offsets.push(index);
325
- index += width;
326
- }
327
- offsets.push(length);
328
- emitRun(part, offsets, kind, size, out);
329
34
  }
330
-
331
- // src/core/tokenizer/tokenize.ts
332
- function producesSurfaceForms(language, options) {
333
- if (!options?.collectSurfaces || options.customTokenizer) return false;
334
- return (options.stem ?? true) && language.stemmer !== null;
335
- }
336
- var DEFAULT_SPLIT_PATTERN = /[^\p{L}\p{M}\p{N}_'-]+/u;
337
- function resolveStopWords(language, override) {
338
- if (!override) return language.stopWords;
339
- if (override instanceof Set) return override;
340
- return override(language.stopWords);
341
- }
342
- function splitText(text, language) {
343
- const pattern = language.tokenizer?.splitPattern ?? DEFAULT_SPLIT_PATTERN;
344
- const parts = text.split(pattern);
345
- const ngramSize = language.tokenizer?.ngramSize;
346
- if (ngramSize === void 0 || ngramSize < 1) return parts;
347
- const size = Math.floor(ngramSize);
348
- const expanded = [];
349
- for (const part of parts) {
350
- expandNgrams(part, size, expanded);
351
- }
352
- return expanded;
353
- }
354
- var CHAR_APOSTROPHE = 39;
355
- var CHAR_S = 115;
356
- function stripPossessive(token) {
357
- const len = token.length;
358
- if (len >= 2 && token.charCodeAt(len - 1) === CHAR_S && token.charCodeAt(len - 2) === CHAR_APOSTROPHE) {
359
- return token.slice(0, -2);
360
- }
361
- if (len >= 1 && token.charCodeAt(len - 1) === CHAR_APOSTROPHE) {
362
- return token.slice(0, -1);
363
- }
364
- return token;
365
- }
366
- function visitTokens(text, language, options, visit) {
367
- const {
368
- stem = true,
369
- removeStopWords = true,
370
- removeDiacritics = false,
371
- collectSurfaces = false,
372
- customTokenizer,
373
- stopWordOverride
374
- } = options ?? {};
375
- if (customTokenizer) {
376
- for (const entry of customTokenizer.tokenize(text)) visit(entry.token, entry.position, void 0, entry.token);
377
- return;
378
- }
379
- const rawParts = splitText(normalizeForSplitting(text), language);
380
- const minLength = language.tokenizer?.minTokenLength ?? DEFAULT_MIN_TOKEN_LENGTH;
381
- const effectiveDiacritics = removeDiacritics || (language.tokenizer?.normalizeDiacritics ?? false);
382
- const wantSurfaces = collectSurfaces && stem && language.stemmer !== null;
383
- const stopWords = removeStopWords ? resolveStopWords(language, stopWordOverride) : null;
384
- const stripPossessives = language.tokenizer?.stripPossessive ?? false;
385
- for (let position = 0; position < rawParts.length; position++) {
386
- const part = rawParts[position];
387
- const candidate = stripPossessives ? stripPossessive(part) : part;
388
- if (candidate.length < minLength || stopWords?.has(candidate)) continue;
389
- const processed = transformToken(candidate, language, stem, effectiveDiacritics);
390
- if (processed.length === 0) continue;
391
- if (!wantSurfaces) {
392
- visit(processed, position, void 0, part);
393
- continue;
394
- }
395
- const surface = effectiveDiacritics ? transformToken(candidate, language, false, true) : candidate;
396
- visit(processed, position, surface === processed ? void 0 : surface, part);
397
- }
398
- }
399
- function tokenize(text, language, options) {
400
- if (options?.customTokenizer) {
401
- const customResult = options.customTokenizer.tokenize(text);
402
- return { tokens: customResult, originalTokens: customResult.map((entry) => entry.token) };
403
- }
404
- const tokens = [];
405
- const originalTokens = [];
406
- const surfaces = producesSurfaceForms(language, options) ? [] : void 0;
407
- visitTokens(text, language, options, (token, position, surface, original) => {
408
- tokens.push({ token, position });
409
- originalTokens.push(original);
410
- surfaces?.push(surface);
411
- });
412
- return { tokens, originalTokens, surfaces };
413
- }
414
- function* tokenizeIterator(text, language, options) {
415
- const {
416
- stem = true,
417
- removeStopWords = true,
418
- removeDiacritics = false,
419
- collectSurfaces = false,
420
- customTokenizer,
421
- stopWordOverride
422
- } = options ?? {};
423
- if (customTokenizer) {
424
- const customResult = customTokenizer.tokenize(text);
425
- for (const entry of customResult) {
426
- yield entry;
427
- }
428
- return;
429
- }
430
- const normalized = normalizeForSplitting(text);
431
- const rawParts = splitText(normalized, language);
432
- const minLength = language.tokenizer?.minTokenLength ?? DEFAULT_MIN_TOKEN_LENGTH;
433
- const effectiveDiacritics = removeDiacritics || (language.tokenizer?.normalizeDiacritics ?? false);
434
- const stemsTokens = stem && language.stemmer !== null;
435
- const wantSurfaces = collectSurfaces && stemsTokens;
436
- const stopWords = removeStopWords ? resolveStopWords(language, stopWordOverride) : /* @__PURE__ */ new Set();
437
- const stripPossessives = language.tokenizer?.stripPossessive ?? false;
438
- let position = 0;
439
- for (const part of rawParts) {
440
- const candidate = stripPossessives ? stripPossessive(part) : part;
441
- if (candidate.length < minLength) {
442
- position++;
443
- continue;
444
- }
445
- if (stopWords.has(candidate)) {
446
- position++;
447
- continue;
448
- }
449
- const processed = transformToken(candidate, language, stem, effectiveDiacritics);
450
- if (processed.length > 0) {
451
- if (wantSurfaces) {
452
- const surface = effectiveDiacritics ? transformToken(candidate, language, false, true) : candidate;
453
- yield { token: processed, position, surface: surface === processed ? void 0 : surface };
454
- } else {
455
- yield { token: processed, position };
456
- }
457
- }
458
- position++;
459
- }
35
+ function requirePatternSearch(fieldPath) {
36
+ const module = availablePatternSearch();
37
+ if (module !== null) return module;
38
+ throw new NarsilError(
39
+ ErrorCodes.CONFIG_INVALID,
40
+ `Field "${fieldPath}" is a pattern field, but this bundle contains no pattern search code. Add import { patternSearch } from '@delali/narsil/pattern', then call registerPatternSearch(patternSearch) before you create or load the index`,
41
+ { field: fieldPath }
42
+ );
460
43
  }
461
44
 
462
45
  // src/core/pattern-index/work-meter.ts
@@ -768,78 +351,6 @@ function heapPop(heap) {
768
351
  return top;
769
352
  }
770
353
 
771
- // src/core/partition/utils.ts
772
- function getOrCreateFieldNameIndex(table, fieldName) {
773
- const existing = table.indexMap.get(fieldName);
774
- if (existing !== void 0) return existing;
775
- const idx = table.names.length;
776
- table.names.push(fieldName);
777
- table.indexMap.set(fieldName, idx);
778
- return idx;
779
- }
780
- var VECTOR_PATTERN = /^vector\[(\d+)]$/;
781
- function getNestedValue(obj, path) {
782
- const parts = path.split(".");
783
- let current = obj;
784
- for (let i = 0; i < parts.length; i++) {
785
- if (current === null || current === void 0 || typeof current !== "object") return void 0;
786
- if (!Object.hasOwn(current, parts[i])) return void 0;
787
- current = current[parts[i]];
788
- }
789
- return current;
790
- }
791
- function stringArraysEqual(a, b) {
792
- if (a === b) return true;
793
- if (!a || !b) return false;
794
- if (a.length !== b.length) return false;
795
- for (let i = 0; i < a.length; i++) {
796
- if (a[i] !== b[i]) return false;
797
- }
798
- return true;
799
- }
800
- function textFieldsChanged(oldDoc, newDoc, fields) {
801
- for (const { path, base } of fields) {
802
- if (!isWordIndexedBase(base)) continue;
803
- const oldVal = getNestedValue(oldDoc, path);
804
- const newVal = getNestedValue(newDoc, path);
805
- if (base === "string") {
806
- if (oldVal !== newVal) return true;
807
- } else {
808
- if (!stringArraysEqual(oldVal, newVal)) return true;
809
- }
810
- }
811
- return false;
812
- }
813
- function parseVectorDimension(fieldType) {
814
- const match = VECTOR_PATTERN.exec(fieldType);
815
- return match ? Number.parseInt(match[1], 10) : null;
816
- }
817
- function tokenizeOptions(options) {
818
- return {
819
- stem: true,
820
- removeStopWords: true,
821
- collectSurfaces: options?.collectSurfaces === true,
822
- stopWordOverride: options?.stopWordOverride,
823
- customTokenizer: options?.customTokenizer
824
- };
825
- }
826
- function getFlatSchema(state, schema) {
827
- if (state.lastSchemaRef === schema && state.flatSchemaCache) return state.flatSchemaCache;
828
- state.flatSchemaCache = flattenSchema(schema);
829
- state.lastSchemaRef = schema;
830
- return state.flatSchemaCache;
831
- }
832
- function getFieldValueForDoc(docStore, docId, fieldPath) {
833
- const stored = docStore.get(docId);
834
- if (!stored) return void 0;
835
- return getNestedValue(stored.fields, fieldPath);
836
- }
837
- function getFieldValueByInternalId(docStore, internalId, fieldPath) {
838
- const externalId = docStore.getExternalId(internalId);
839
- if (externalId === void 0) return void 0;
840
- return getFieldValueForDoc(docStore, externalId, fieldPath);
841
- }
842
-
843
354
  // src/core/partition/facets.ts
844
355
  var UNDECLARED_FIELD_TYPE = "";
845
356
  function keptFacetValues(limit) {
@@ -1039,67 +550,6 @@ function mergeFacets(partitionFacets, config) {
1039
550
  return result;
1040
551
  }
1041
552
 
1042
- // src/core/fuzzy.ts
1043
- var FIRST_SURROGATE = 55296;
1044
- var LAST_SURROGATE = 57343;
1045
- var LAST_BASIC_PLANE_CODE_POINT = 65535;
1046
- function hasSurrogate(text) {
1047
- for (let index = 0; index < text.length; index++) {
1048
- const unit = text.charCodeAt(index);
1049
- if (unit >= FIRST_SURROGATE && unit <= LAST_SURROGATE) return true;
1050
- }
1051
- return false;
1052
- }
1053
- function fuzzyPrefixOf(queryTerm, prefixLength) {
1054
- let end = 0;
1055
- for (let counted = 0; counted < prefixLength && end < queryTerm.length; counted++) {
1056
- const codePoint = queryTerm.codePointAt(end);
1057
- end += codePoint !== void 0 && codePoint > LAST_BASIC_PLANE_CODE_POINT ? 2 : 1;
1058
- }
1059
- return queryTerm.slice(0, end);
1060
- }
1061
- function fuzzyTermMatches(queryTerm, indexTerm, tolerance, prefixLength) {
1062
- if (queryTerm === indexTerm) return true;
1063
- if (tolerance <= 0) return false;
1064
- if (!indexTerm.startsWith(fuzzyPrefixOf(queryTerm, prefixLength))) return false;
1065
- return boundedLevenshtein(queryTerm, indexTerm, tolerance).withinTolerance;
1066
- }
1067
- function boundedLevenshtein(a, b, tolerance) {
1068
- if (tolerance < 0) return { distance: -1, withinTolerance: false };
1069
- if (a === b) return { distance: 0, withinTolerance: true };
1070
- if (hasSurrogate(a) || hasSurrogate(b)) return bandedDistance(Array.from(a), Array.from(b), tolerance);
1071
- return bandedDistance(a, b, tolerance);
1072
- }
1073
- function bandedDistance(a, b, tolerance) {
1074
- const m = a.length;
1075
- const n = b.length;
1076
- const beyond = tolerance + 1;
1077
- if (Math.abs(m - n) > tolerance) return { distance: beyond, withinTolerance: false };
1078
- let previous = new Uint32Array(n + 1).fill(beyond);
1079
- let current = new Uint32Array(n + 1).fill(beyond);
1080
- const firstRowEnd = Math.min(n, tolerance);
1081
- for (let j = 0; j <= firstRowEnd; j++) previous[j] = j;
1082
- for (let i = 1; i <= m; i++) {
1083
- const low = Math.max(1, i - tolerance);
1084
- const high = Math.min(n, i + tolerance);
1085
- current[low - 1] = low === 1 ? i : beyond;
1086
- let rowMin = current[low - 1];
1087
- const character = a[i - 1];
1088
- for (let j = low; j <= high; j++) {
1089
- const substitution = previous[j - 1] + (character === b[j - 1] ? 0 : 1);
1090
- const cell = Math.min(previous[j] + 1, current[j - 1] + 1, substitution, beyond);
1091
- current[j] = cell;
1092
- if (cell < rowMin) rowMin = cell;
1093
- }
1094
- if (rowMin > tolerance) return { distance: beyond, withinTolerance: false };
1095
- const finished = previous;
1096
- previous = current;
1097
- current = finished;
1098
- }
1099
- const distance = previous[n];
1100
- return { distance, withinTolerance: distance <= tolerance };
1101
- }
1102
-
1103
553
  // src/search/cursor-codec.ts
1104
554
  function encodeCursorText(text) {
1105
555
  if (typeof Buffer !== "undefined") return Buffer.from(text, "utf-8").toString("base64");
@@ -1116,12 +566,6 @@ function decodeCursorText(encoded) {
1116
566
  return new TextDecoder().decode(bytes);
1117
567
  }
1118
568
 
1119
- // src/filters/constants.ts
1120
- var MAX_FILTER_DEPTH = 30;
1121
- var MAX_FILTER_TESTS = 1024;
1122
- var MAX_FILTER_ARRAY_SIZE = 65536;
1123
- var GEO_UNIT_METRES = { km: 1e3, mi: 1609.344, m: 1 };
1124
-
1125
569
  // src/filters/operands/checks.ts
1126
570
  function operandKindOf(type) {
1127
571
  if (type === void 0) return void 0;
@@ -1542,6 +986,34 @@ function requireSizeOperands(fieldPath, filter, type) {
1542
986
  }
1543
987
  }
1544
988
 
989
+ // src/core/pattern-index/host.ts
990
+ var NO_SOURCES = [];
991
+ var foldSources = null;
992
+ function sourcesByFold() {
993
+ if (foldSources !== null) return foldSources;
994
+ const sources = /* @__PURE__ */ new Map();
995
+ const add = (source, folded) => {
996
+ const known = sources.get(folded);
997
+ if (known === void 0) sources.set(folded, [source]);
998
+ else known.push(source);
999
+ };
1000
+ for (const [source, folded] of singleFoldTable()) add(source, folded);
1001
+ foldSources = sources;
1002
+ return sources;
1003
+ }
1004
+ function singleFoldSources(folded) {
1005
+ return sourcesByFold().get(folded) ?? NO_SOURCES;
1006
+ }
1007
+ var PATTERN_HOST = { appendFoldedCodePoint, singleFoldSources };
1008
+ function patternIndexOf(indexes, fieldPath) {
1009
+ let index = indexes.get(fieldPath);
1010
+ if (index === void 0) {
1011
+ index = requirePatternSearch(fieldPath).createIndex(PATTERN_HOST);
1012
+ indexes.set(fieldPath, index);
1013
+ }
1014
+ return index;
1015
+ }
1016
+
1545
1017
  // src/filters/operands/pattern.ts
1546
1018
  function requirePatternField(fieldPath, operator, type) {
1547
1019
  if (parsedTypeOf(type)?.pattern === true) return;
@@ -1552,14 +1024,16 @@ function requirePatternField(fieldPath, operator, type) {
1552
1024
  operator
1553
1025
  );
1554
1026
  }
1027
+ function requireTextLength(fieldPath, operator, text) {
1028
+ if (!exceedsCodePoints(text, MAX_PATTERN_TEXT_CODE_POINTS)) return;
1029
+ invalid(
1030
+ `The text of "${operator}" on pattern field "${fieldPath}" holds more than ${MAX_PATTERN_TEXT_CODE_POINTS} code points`,
1031
+ fieldPath,
1032
+ operator
1033
+ );
1034
+ }
1555
1035
  function requirePatternText(fieldPath, operator, text, caseFold) {
1556
- if (exceedsCodePoints(text, MAX_PATTERN_TEXT_CODE_POINTS)) {
1557
- invalid(
1558
- `The text of "${operator}" on pattern field "${fieldPath}" holds more than ${MAX_PATTERN_TEXT_CODE_POINTS} code points`,
1559
- fieldPath,
1560
- operator
1561
- );
1562
- }
1036
+ requireTextLength(fieldPath, operator, text);
1563
1037
  if (caseFold && foldedCodePointCount(text) > MAX_PATTERN_POSITIONS) {
1564
1038
  invalid(
1565
1039
  `The text of "${operator}" on pattern field "${fieldPath}" has a case fold of more than ${MAX_PATTERN_POSITIONS} code points, while a pattern holds at most ${MAX_PATTERN_POSITIONS} positions`,
@@ -1569,6 +1043,23 @@ function requirePatternText(fieldPath, operator, text, caseFold) {
1569
1043
  }
1570
1044
  }
1571
1045
  var ELEMENT_OPERATORS2 = ["matchesAny", "containsAll"];
1046
+ function requireExpressions(fieldPath, filter, type) {
1047
+ for (const operator of PATTERN_EXPRESSION_OPERATORS) {
1048
+ const pattern = filter[operator];
1049
+ if (pattern === void 0) continue;
1050
+ if (typeof pattern !== "string")
1051
+ invalid(`"${operator}" on field "${fieldPath}" must be a string`, fieldPath, operator);
1052
+ requirePatternField(fieldPath, operator, type);
1053
+ requireTextLength(fieldPath, operator, pattern);
1054
+ requirePatternSearch(fieldPath).checkExpression(
1055
+ operator,
1056
+ pattern,
1057
+ filter.caseFold === true,
1058
+ fieldPath,
1059
+ PATTERN_HOST
1060
+ );
1061
+ }
1062
+ }
1572
1063
  function requirePatternOperands(fieldPath, filter, type) {
1573
1064
  if (filter.contains !== void 0) {
1574
1065
  if (typeof filter.contains !== "string") {
@@ -1582,6 +1073,7 @@ function requirePatternOperands(fieldPath, filter, type) {
1582
1073
  }
1583
1074
  requirePatternField(fieldPath, "caseFold", type);
1584
1075
  }
1076
+ requireExpressions(fieldPath, filter, type);
1585
1077
  if (parsedTypeOf(type)?.pattern !== true) return;
1586
1078
  const caseFold = filter.caseFold === true;
1587
1079
  for (const operator of PATTERN_TEST_OPERATORS) {
@@ -2064,36 +1556,120 @@ function applyPagination(results, limit, offset, binding, cursor, sort, pinnedId
2064
1556
  return { paginated: sliced, nextCursor };
2065
1557
  }
2066
1558
 
2067
- // src/search/fulltext.ts
2068
- function resolvePrefixExpansion(partition, params, language, options, queryTokens, lastToken) {
2069
- const term = params.term;
2070
- if (term === void 0) return void 0;
1559
+ // src/search/term-groups.ts
1560
+ function writtenWordsByPosition(term, language, analysis) {
2071
1561
  const unstemmed = tokenize(term, language, {
2072
1562
  stem: false,
2073
1563
  removeStopWords: true,
2074
- customTokenizer: options?.customTokenizer,
2075
- stopWordOverride: options?.stopWords
1564
+ customTokenizer: analysis?.customTokenizer,
1565
+ stopWordOverride: analysis?.stopWords
2076
1566
  });
2077
- let surfacePrefix;
2078
- for (const t of unstemmed.tokens) {
2079
- if (t.position === lastToken.position) {
2080
- surfacePrefix = t.token;
2081
- break;
1567
+ const written = /* @__PURE__ */ new Map();
1568
+ for (const token of unstemmed.tokens) {
1569
+ if (!written.has(token.position)) written.set(token.position, token.token);
1570
+ }
1571
+ return written;
1572
+ }
1573
+ function groupFor(groups, token, typoMatches) {
1574
+ let group = groups.get(token);
1575
+ if (group === void 0) {
1576
+ group = { token, members: [{ token, factor: 1, partialFactor: 1 }], typoMatches };
1577
+ groups.set(token, group);
1578
+ }
1579
+ return group;
1580
+ }
1581
+ function addCompletions(groups, partition, lastToken, typedPrefix) {
1582
+ if (typedPrefix === void 0 || typedPrefix.length === 0) return;
1583
+ const group = groupFor(groups, lastToken.token, false);
1584
+ const ownLength = codePointCount(lastToken.token);
1585
+ for (const completion of partition.expandTermPrefix(typedPrefix, lastToken.token, PREFIX_MAX_EXPANSIONS)) {
1586
+ if (completion === lastToken.token) continue;
1587
+ const factor = Math.min(1, ownLength / codePointCount(completion));
1588
+ group.members.push({ token: completion, factor, partialFactor: factor });
1589
+ }
1590
+ }
1591
+ function addPartialWords(group, added) {
1592
+ const membersByToken = /* @__PURE__ */ new Map();
1593
+ for (const member of group.members) membersByToken.set(member.token, member);
1594
+ for (const word of added) {
1595
+ const member = membersByToken.get(word.token);
1596
+ if (member === void 0) {
1597
+ group.members.push({ token: word.token, factor: 0, partialFactor: word.factor });
1598
+ } else if (word.factor > member.partialFactor) {
1599
+ member.partialFactor = word.factor;
2082
1600
  }
2083
1601
  }
2084
- if (surfacePrefix === void 0 || surfacePrefix.length === 0) return void 0;
2085
- const expansions = partition.expandTermPrefix(surfacePrefix, lastToken.token, PREFIX_MAX_EXPANSIONS);
2086
- const terms = [];
2087
- for (const expanded of expansions) {
2088
- if (expanded !== lastToken.token) terms.push(expanded);
1602
+ }
1603
+ function partialWordAnalysisOf(fields, language, analysis) {
1604
+ return {
1605
+ fields,
1606
+ language,
1607
+ options: {
1608
+ stopWordOverride: analysis?.stopWords,
1609
+ customTokenizer: analysis?.customTokenizer,
1610
+ collectSurfaces: true
1611
+ }
1612
+ };
1613
+ }
1614
+ function searchesAPartialField(params, partialFields) {
1615
+ if (params.fields === void 0) return true;
1616
+ for (const field of params.fields) {
1617
+ if (partialFields.includes(field)) return true;
2089
1618
  }
2090
- for (const qt of queryTokens) {
2091
- if (qt.token === lastToken.token) {
2092
- return { token: qt.token, terms };
1619
+ return false;
1620
+ }
1621
+ function partialWordRequests(params, queryTokens, written, prefixToken) {
1622
+ const eligible = [];
1623
+ for (const queryToken of queryTokens) {
1624
+ const word = written.get(queryToken.position);
1625
+ if (word === void 0 || codePointCount(word) < PARTIAL_WORD_MIN_CODE_POINTS) continue;
1626
+ eligible.push({ token: queryToken.token, written: word });
1627
+ if (eligible.length === PARTIAL_WORDS_ADDED_PER_QUERY) break;
1628
+ }
1629
+ const count = eligible.length;
1630
+ if (count === 0) return [];
1631
+ const evenShare = Math.floor(PARTIAL_WORDS_ADDED_PER_QUERY / count);
1632
+ const remainder = PARTIAL_WORDS_ADDED_PER_QUERY % count;
1633
+ return eligible.map((entry, order) => ({
1634
+ token: entry.token,
1635
+ request: {
1636
+ written: entry.written,
1637
+ ownToken: entry.token,
1638
+ share: evenShare + (order < remainder ? 1 : 0),
1639
+ tolerance: entry.token === prefixToken ? 0 : params.tolerance ?? 0,
1640
+ prefixLength: params.prefixLength ?? DEFAULT_FUZZY_PREFIX_LENGTH
1641
+ }
1642
+ }));
1643
+ }
1644
+ function resolveTermGroups(partition, params, language, schema, queryTokens, lastToken, analysis) {
1645
+ const term = params.term;
1646
+ if (term === void 0 || params.exact === true) return {};
1647
+ const usesPrefix = params.prefix === true;
1648
+ const partialFields = params.partialWords === true ? partialFieldsOf(schema) : [];
1649
+ const usesPartialWords = partialFields.length > 0 && searchesAPartialField(params, partialFields);
1650
+ if (!usesPrefix && !usesPartialWords) return {};
1651
+ const written = writtenWordsByPosition(term, language, analysis);
1652
+ const groups = /* @__PURE__ */ new Map();
1653
+ if (usesPrefix) addCompletions(groups, partition, lastToken, written.get(lastToken.position));
1654
+ const prefixToken = groups.has(lastToken.token) ? lastToken.token : void 0;
1655
+ if (usesPartialWords) {
1656
+ const requests = partialWordRequests(params, queryTokens, written, prefixToken);
1657
+ const added = partition.expandPartialWords(
1658
+ partialWordAnalysisOf(partialFields, language, analysis),
1659
+ requests.map((entry) => entry.request)
1660
+ );
1661
+ for (let at = 0; at < requests.length; at++) {
1662
+ if (added[at].length === 0) continue;
1663
+ const token = requests[at].token;
1664
+ addPartialWords(groupFor(groups, token, token !== prefixToken), added[at]);
2093
1665
  }
2094
1666
  }
2095
- return void 0;
1667
+ const termGroups = [...groups.values()];
1668
+ if (termGroups.length === 0) return {};
1669
+ return usesPartialWords ? { termGroups, partialFields: [...partialFields] } : { termGroups };
2096
1670
  }
1671
+
1672
+ // src/search/fulltext.ts
2097
1673
  function prepareFulltextQuery(partition, params, language, schema, options) {
2098
1674
  if (!params.term || params.term.trim().length === 0) return null;
2099
1675
  if (params.fields && params.fields.length === 0) return null;
@@ -2109,11 +1685,6 @@ function prepareFulltextQuery(partition, params, language, schema, options) {
2109
1685
  });
2110
1686
  if (queryTokenResult.tokens.length === 0) return null;
2111
1687
  const queryTokens = deduplicateTokens(queryTokenResult.tokens);
2112
- let prefixExpansion;
2113
- if (params.prefix === true && params.exact !== true) {
2114
- const lastToken = queryTokenResult.tokens[queryTokenResult.tokens.length - 1];
2115
- prefixExpansion = resolvePrefixExpansion(partition, params, language, options, queryTokens, lastToken);
2116
- }
2117
1688
  let filterBitset;
2118
1689
  if (params.filters) {
2119
1690
  filterBitset = partition.applyFiltersBitset(
@@ -2123,14 +1694,17 @@ function prepareFulltextQuery(partition, params, language, schema, options) {
2123
1694
  );
2124
1695
  if (bitsetIsEmpty(filterBitset)) return null;
2125
1696
  }
2126
- return { queryTokens, prefixExpansion, filterBitset };
1697
+ const lastToken = queryTokenResult.tokens[queryTokenResult.tokens.length - 1];
1698
+ const groups = resolveTermGroups(partition, params, language, schema, queryTokens, lastToken, options);
1699
+ return { queryTokens, ...groups, filterBitset };
2127
1700
  }
2128
1701
  function fulltextMatches(partition, params, language, schema, options) {
2129
1702
  const prepared = prepareFulltextQuery(partition, params, language, schema, options);
2130
1703
  if (prepared === null) return null;
2131
1704
  return partition.searchFulltextMatches({
2132
1705
  queryTokens: prepared.queryTokens,
2133
- prefixExpansion: prepared.prefixExpansion,
1706
+ termGroups: prepared.termGroups,
1707
+ partialFields: prepared.partialFields,
2134
1708
  fields: params.fields,
2135
1709
  tolerance: params.tolerance ?? 0,
2136
1710
  prefixLength: params.prefixLength ?? DEFAULT_FUZZY_PREFIX_LENGTH,
@@ -2143,14 +1717,15 @@ function fulltextSearch(partition, params, language, schema, options) {
2143
1717
  if (prepared === null) {
2144
1718
  return { scored: [], totalMatched: 0 };
2145
1719
  }
2146
- const { queryTokens, prefixExpansion, filterBitset } = prepared;
1720
+ const { queryTokens, termGroups, partialFields, filterBitset } = prepared;
2147
1721
  const needsAllResults = params.minScore !== void 0 || params.termMatch !== void 0 && params.termMatch !== "any" || params.sort !== void 0 || params.group !== void 0 || params.pinned !== void 0 || params.searchAfter !== void 0;
2148
1722
  const maxResults = needsAllResults ? void 0 : clampRowCount(params.limit, DEFAULT_PAGE_SIZE) + clampRowCount(params.offset, 0) + 1;
2149
1723
  const collectMatchedSet = params.facets !== void 0 && !needsAllResults ? "ordinals" : void 0;
2150
1724
  const collectComponents = params.includeScoreComponents === true || params.termMatch !== void 0 && params.termMatch !== "any";
2151
1725
  const rawResult = partition.searchFulltext({
2152
1726
  queryTokens,
2153
- prefixExpansion,
1727
+ termGroups,
1728
+ partialFields,
2154
1729
  fields: params.fields,
2155
1730
  boost: params.boost,
2156
1731
  tolerance: params.tolerance ?? 0,
@@ -2174,7 +1749,8 @@ function fulltextSearch(partition, params, language, schema, options) {
2174
1749
  params.tolerance ?? 0,
2175
1750
  params.prefixLength ?? DEFAULT_FUZZY_PREFIX_LENGTH,
2176
1751
  params.exact ?? false,
2177
- prefixExpansion
1752
+ termGroups,
1753
+ partialFields ?? []
2178
1754
  );
2179
1755
  }
2180
1756
  if (params.minScore !== void 0 && params.minScore > 0) {
@@ -2216,45 +1792,48 @@ function deduplicateTokens(tokens) {
2216
1792
  }
2217
1793
  return result;
2218
1794
  }
2219
- function filterByTermCoverage(scored, queryTokens, policy, tolerance, prefixLength, exact, prefixExpansion) {
1795
+ function filterByTermCoverage(scored, queryTokens, policy, tolerance, prefixLength, exact, termGroups, partialFields) {
2220
1796
  const requiredCount = policy === "all" ? queryTokens.length : policy;
2221
1797
  if (requiredCount <= 0) return scored;
2222
1798
  if (requiredCount > queryTokens.length) return [];
2223
- const expansionTerms = prefixExpansion ? new Set(prefixExpansion.terms) : void 0;
1799
+ const groups = /* @__PURE__ */ new Map();
1800
+ for (const group of termGroups ?? []) {
1801
+ const partialOnly = /* @__PURE__ */ new Set();
1802
+ for (const member of group.members) {
1803
+ if (member.factor === 0) partialOnly.add(member.token);
1804
+ }
1805
+ groups.set(group.token, {
1806
+ members: new Set(group.members.map((member) => member.token)),
1807
+ partialOnly,
1808
+ typoMatches: group.typoMatches
1809
+ });
1810
+ }
2224
1811
  return scored.filter((doc) => {
2225
- const matched = countDocTermMatches(
2226
- doc,
2227
- queryTokens,
2228
- tolerance,
2229
- prefixLength,
2230
- exact,
2231
- prefixExpansion?.token,
2232
- expansionTerms
2233
- );
1812
+ const matched = countDocTermMatches(doc, queryTokens, tolerance, prefixLength, exact, groups, partialFields);
2234
1813
  return matched >= requiredCount;
2235
1814
  });
2236
1815
  }
2237
- function countDocTermMatches(doc, queryTokens, tolerance, prefixLength, exact, prefixToken, expansionTerms) {
1816
+ function countDocTermMatches(doc, queryTokens, tolerance, prefixLength, exact, groups, partialFields) {
2238
1817
  const indexTokens = Object.keys(doc.idf);
2239
1818
  if (indexTokens.length === 0) return 0;
2240
1819
  let count = 0;
2241
1820
  for (const qt of queryTokens) {
2242
- if (prefixToken !== void 0 && qt.token === prefixToken) {
2243
- if (prefixTokenSatisfied(qt.token, indexTokens, expansionTerms)) {
2244
- count++;
2245
- }
2246
- continue;
2247
- }
2248
- if (queryTermSatisfied(qt.token, indexTokens, tolerance, prefixLength, exact)) {
2249
- count++;
2250
- }
1821
+ const group = groups.get(qt.token);
1822
+ const satisfied = group === void 0 ? queryTermSatisfied(qt.token, indexTokens, tolerance, prefixLength, exact) : groupSatisfied(group, doc, indexTokens, partialFields) || group.typoMatches && queryTermSatisfied(qt.token, indexTokens, tolerance, prefixLength, exact);
1823
+ if (satisfied) count++;
2251
1824
  }
2252
1825
  return count;
2253
1826
  }
2254
- function prefixTokenSatisfied(prefixToken, indexTokens, expansionTerms) {
1827
+ function matchedInPartialField(doc, token, partialFields) {
1828
+ for (const field of partialFields) {
1829
+ if (doc.termFrequencies[`${field}:${token}`] !== void 0) return true;
1830
+ }
1831
+ return false;
1832
+ }
1833
+ function groupSatisfied(group, doc, indexTokens, partialFields) {
2255
1834
  for (const indexToken of indexTokens) {
2256
- if (indexToken === prefixToken) return true;
2257
- if (expansionTerms?.has(indexToken)) return true;
1835
+ if (!group.members.has(indexToken)) continue;
1836
+ if (!group.partialOnly.has(indexToken) || matchedInPartialField(doc, indexToken, partialFields)) return true;
2258
1837
  }
2259
1838
  return false;
2260
1839
  }
@@ -2578,4 +2157,4 @@ function setNestedValue(obj, path, value) {
2578
2157
  current[segments[segments.length - 1]] = value;
2579
2158
  }
2580
2159
 
2581
- export { GEO_UNIT_METRES, MAX_FILTER_ARRAY_SIZE, MAX_FILTER_DEPTH, MAX_FILTER_TESTS, applyPagination, applyProjection, applySorting, boundedLevenshtein, clampRowCount, clearNormalizationCache, cloneProjected, collectQueryTermStats, computeFacets, configureNormalizationCache, countsWithoutScores, coveringRanges, createPatternWorkMeter, decodePageCursor, encodePageCursor, everyValueFacetConfig, fanOutMatchCount, fanOutQuery, filterTestCount, fnv1a, fnv1aBytes, fnv1aUint32s, fulltextMatches, fuzzyPrefixOf, fuzzyTermMatches, getFieldValueByInternalId, getFlatSchema, getNestedValue, getNormalizationCacheSize, getOrCreateFieldNameIndex, joinPatternWorkMeter, kWayMerge, locationCode, mergeFacets, mergePartitionStats, normalizeSort, oversampledFacetConfig, oversampledShardSize, parseVectorDimension, partitionsIn, polygonBoxes, polygonContainment, projectionKeepsField, pruneStatsToQueryTerms, radiusBoxes, readSortValues, requireMatchingCursor, requireSortableFields, requireValidFilter, requireValidOperands, requireWithinResultWindow, resetNormalizationCache, resolveProjection, sanitizeGlobalStats, setNestedValue, sortModesOf, sortSignatureEntry, sortSignatureOf, textFieldsChanged, tokenize, tokenizeIterator, tokenizeOptions, undeclaredFieldOnStrictIndex, undeclaredFilterField, visitTokens };
2160
+ export { PATTERN_HOST, applyPagination, applyProjection, applySorting, availablePatternSearch, clampRowCount, cloneProjected, collectQueryTermStats, computeFacets, countsWithoutScores, coveringRanges, createPatternWorkMeter, decodePageCursor, encodePageCursor, everyValueFacetConfig, fanOutMatchCount, fanOutQuery, filterTestCount, fnv1a, fnv1aBytes, fnv1aUint32s, fulltextMatches, joinPatternWorkMeter, kWayMerge, locationCode, mergeFacets, mergePartitionStats, normalizeSort, oversampledFacetConfig, oversampledShardSize, partitionsIn, patternIndexOf, polygonBoxes, polygonContainment, projectionKeepsField, pruneStatsToQueryTerms, radiusBoxes, readSortValues, registerPatternSearch, requireMatchingCursor, requirePatternSearch, requirePatternSearchFor, requireSortableFields, requireValidFilter, requireValidOperands, requireWithinResultWindow, resolveProjection, sanitizeGlobalStats, setNestedValue, sortModesOf, sortSignatureEntry, sortSignatureOf, undeclaredFieldOnStrictIndex, undeclaredFilterField };