|
1
|
import { |
|
2
|
assertIndexSupported, |
|
3
|
assertQuerySupported, |
|
4
|
buildFacetQuery, |
|
5
|
buildIndexPlan, |
|
6
|
extractPrimaryKeyFilter, |
|
7
|
keywordTsQuerySql, |
|
8
|
normalizeFacetRequests, |
|
9
|
projectIndexedDocument, |
|
10
|
resolveVectorField, |
|
11
|
sameSchema, |
|
12
|
sourceTextForEmbed, |
|
13
|
tableNameForIndex, |
|
14
|
toWhereClause, |
|
15
|
weightLabel, |
|
16
|
} from "../index" |
|
17
|
import { SearchTypes } from "@medusajs/framework/types" |
|
18
|
|
|
19
|
const baseDefinition = ( |
|
20
|
overrides: Partial<SearchTypes.ResolvedSearchIndexDefinition> = {} |
|
21
|
): SearchTypes.ResolvedSearchIndexDefinition => |
|
22
|
({ |
|
23
|
name: "product", |
|
24
|
entity: "product", |
|
25
|
primary_key: "id", |
|
26
|
provider: "search-postgres", |
|
27
|
physical_name: "product", |
|
28
|
definition_hash: "abc", |
|
29
|
settings: {}, |
|
30
|
seed: async function* () {}, |
|
31
|
fields: { |
|
32
|
id: { type: "keyword", filterable: true }, |
|
33
|
title: { type: "text", searchable: { weight: 3 }, filterable: true }, |
|
34
|
status: { type: "keyword", filterable: true, facetable: true }, |
|
35
|
price: { |
|
36
|
type: "float", |
|
37
|
filterable: true, |
|
38
|
sortable: true, |
|
39
|
facetable: true, |
|
40
|
}, |
|
41
|
tags: { type: "keyword", array: true, filterable: true, facetable: true }, |
|
42
|
sizes: { type: "integer", array: true, filterable: true }, |
|
43
|
deleted_at: { type: "date", filterable: true }, |
|
44
|
variants: { |
|
45
|
type: "object", |
|
46
|
array: true, |
|
47
|
fields: { |
|
48
|
color: { type: "keyword", filterable: true }, |
|
49
|
}, |
|
50
|
}, |
|
51
|
}, |
|
52
|
...overrides, |
|
53
|
} as SearchTypes.ResolvedSearchIndexDefinition) |
|
54
|
|
|
55
|
describe("postgres search utils", () => { |
|
56
|
describe("buildIndexPlan", () => { |
|
57
|
it("flattens nested object arrays into leaf paths", () => { |
|
58
|
const plan = buildIndexPlan(baseDefinition()) |
|
59
|
|
|
60
|
expect(plan.searchable).toEqual(["title"]) |
|
61
|
expect(plan.fields.has("variants.color")).toBe(true) |
|
62
|
expect(plan.fields.get("variants.color")?.is_array).toBe(true) |
|
63
|
expect(plan.fields.get("tags")?.is_array).toBe(true) |
|
64
|
}) |
|
65
|
|
|
66
|
it("reports and ignores vector fields on native", () => { |
|
67
|
const logger = { warn: jest.fn() } as any |
|
68
|
const definition = baseDefinition({ |
|
69
|
fields: { |
|
70
|
id: { type: "keyword", filterable: true }, |
|
71
|
title: { type: "text", searchable: true }, |
|
72
|
embedding: { type: "vector", dimensions: 3 }, |
|
73
|
meta: { |
|
74
|
type: "object", |
|
75
|
fields: { image: { type: "vector", dimensions: 3 } }, |
|
76
|
}, |
|
77
|
}, |
|
78
|
}) |
|
79
|
|
|
80
|
expect(() => |
|
81
|
assertIndexSupported(definition, "native", logger) |
|
82
|
).not.toThrow() |
|
83
|
|
|
84
|
expect(logger.warn).toHaveBeenCalledTimes(1) |
|
85
|
expect(logger.warn.mock.calls[0][0]).toContain( |
|
86
|
'"embedding", "meta.image"' |
|
87
|
) |
|
88
|
|
|
89
|
const plan = buildIndexPlan(definition, "native") |
|
90
|
expect(plan.vectors).toEqual([]) |
|
91
|
expect(plan.fields.has("embedding")).toBe(false) |
|
92
|
expect(plan.fields.has("meta.image")).toBe(false) |
|
93
|
expect(plan.fields.has("title")).toBe(true) |
|
94
|
}) |
|
95
|
|
|
96
|
it("does not fingerprint ignored vector fields into the schema", () => { |
|
97
|
const withVector = buildIndexPlan( |
|
98
|
baseDefinition({ |
|
99
|
fields: { |
|
100
|
id: { type: "keyword", filterable: true }, |
|
101
|
title: { type: "text", searchable: true }, |
|
102
|
embedding: { type: "vector", dimensions: 3 }, |
|
103
|
}, |
|
104
|
}), |
|
105
|
"native" |
|
106
|
) |
|
107
|
const withoutVector = buildIndexPlan( |
|
108
|
baseDefinition({ |
|
109
|
fields: { |
|
110
|
id: { type: "keyword", filterable: true }, |
|
111
|
title: { type: "text", searchable: true }, |
|
112
|
}, |
|
113
|
}), |
|
114
|
"native" |
|
115
|
) |
|
116
|
|
|
117
|
expect(sameSchema(withVector, withoutVector)).toBe(true) |
|
118
|
}) |
|
119
|
|
|
120
|
it("still rejects vector fields on lakebase without dimensions", () => { |
|
121
|
expect(() => |
|
122
|
assertIndexSupported( |
|
123
|
baseDefinition({ |
|
124
|
fields: { |
|
125
|
id: { type: "keyword", filterable: true }, |
|
126
|
embedding: { type: "vector" }, |
|
127
|
}, |
|
128
|
}), |
|
129
|
"lakebase" |
|
130
|
) |
|
131
|
).toThrow(/positive "dimensions"/) |
|
132
|
}) |
|
133
|
|
|
134
|
it("allows vector fields on lakebase when dimensions are set", () => { |
|
135
|
const logger = { warn: jest.fn() } as any |
|
136
|
const definition = baseDefinition({ |
|
137
|
fields: { |
|
138
|
id: { type: "keyword", filterable: true }, |
|
139
|
embedding: { type: "vector", dimensions: 1536 }, |
|
140
|
}, |
|
141
|
}) |
|
142
|
|
|
143
|
expect(() => |
|
144
|
assertIndexSupported(definition, "lakebase", logger) |
|
145
|
).not.toThrow() |
|
146
|
|
|
147
|
expect(logger.warn).not.toHaveBeenCalled() |
|
148
|
expect(buildIndexPlan(definition, "lakebase").vectors).toEqual([ |
|
149
|
"embedding", |
|
150
|
]) |
|
151
|
}) |
|
152
|
|
|
153
|
it("accepts the typo tolerance switch it can honour", () => { |
|
154
|
expect(() => |
|
155
|
assertIndexSupported( |
|
156
|
baseDefinition({ settings: { typo_tolerance: { enabled: false } } }) |
|
157
|
) |
|
158
|
).not.toThrow() |
|
159
|
}) |
|
160
|
|
|
161
|
it("ignores edit-distance thresholds, which only tune matching", () => { |
|
162
|
expect(() => |
|
163
|
assertIndexSupported( |
|
164
|
baseDefinition({ |
|
165
|
settings: { |
|
166
|
typo_tolerance: { |
|
167
|
min_word_size_for_one_typo: 6, |
|
168
|
min_word_size_for_two_typos: 9, |
|
169
|
}, |
|
170
|
}, |
|
171
|
}) |
|
172
|
) |
|
173
|
).not.toThrow() |
|
174
|
}) |
|
175
|
|
|
176
|
it("rejects per-attribute typo tolerance opt-outs", () => { |
|
177
|
expect(() => |
|
178
|
assertIndexSupported( |
|
179
|
baseDefinition({ |
|
180
|
settings: { typo_tolerance: { disabled_on_attributes: ["title"] } }, |
|
181
|
}) |
|
182
|
) |
|
183
|
).toThrow(/all searchable fields at once/) |
|
184
|
}) |
|
185
|
}) |
|
186
|
|
|
187
|
describe("sameSchema", () => { |
|
188
|
it("matches plans with identical fingerprints", () => { |
|
189
|
const a = buildIndexPlan(baseDefinition()) |
|
190
|
const b = buildIndexPlan(baseDefinition()) |
|
191
|
expect(sameSchema(a, b)).toBe(true) |
|
192
|
}) |
|
193
|
|
|
194
|
it("differs when searchability changes", () => { |
|
195
|
const a = buildIndexPlan(baseDefinition()) |
|
196
|
const b = buildIndexPlan( |
|
197
|
baseDefinition({ |
|
198
|
fields: { |
|
199
|
...baseDefinition().fields, |
|
200
|
title: { type: "text", filterable: true }, |
|
201
|
}, |
|
202
|
}) |
|
203
|
) |
|
204
|
expect(sameSchema(a, b)).toBe(false) |
|
205
|
}) |
|
206
|
}) |
|
207
|
|
|
208
|
describe("projectIndexedDocument", () => { |
|
209
|
it("collapses array-of-object leaves and builds weighted text", () => { |
|
210
|
const plan = buildIndexPlan(baseDefinition()) |
|
211
|
const projected = projectIndexedDocument( |
|
212
|
{ |
|
213
|
id: "prod_1", |
|
214
|
title: "Red shoe", |
|
215
|
status: "published", |
|
216
|
price: 49.99, |
|
217
|
tags: ["sale", "new"], |
|
218
|
variants: [{ color: "red" }, { color: "blue" }], |
|
219
|
}, |
|
220
|
plan |
|
221
|
) |
|
222
|
|
|
223
|
expect(projected.id).toBe("prod_1") |
|
224
|
expect(projected.indexed["variants"]).toEqual({ |
|
225
|
color: ["red", "blue"], |
|
226
|
}) |
|
227
|
expect(projected.search_text).toContain("Red shoe") |
|
228
|
expect(projected.weighted_parts[0].weight).toBe("A") |
|
229
|
}) |
|
230
|
|
|
231
|
it("does not store client embeddings for engine-embedded fields", () => { |
|
232
|
const plan = buildIndexPlan( |
|
233
|
baseDefinition({ |
|
234
|
fields: { |
|
235
|
id: { type: "keyword", filterable: true }, |
|
236
|
title: { type: "text", searchable: true }, |
|
237
|
embedding: { |
|
238
|
type: "vector", |
|
239
|
dimensions: 3, |
|
240
|
embed: true, |
|
241
|
}, |
|
242
|
}, |
|
243
|
}), |
|
244
|
"lakebase" |
|
245
|
) |
|
246
|
|
|
247
|
const projected = projectIndexedDocument( |
|
248
|
{ |
|
249
|
id: "prod_1", |
|
250
|
title: "Red shoe", |
|
251
|
embedding: "comfortable red running shoe", |
|
252
|
}, |
|
253
|
plan |
|
254
|
) |
|
255
|
|
|
256
|
expect(projected.vectors).toEqual({}) |
|
257
|
}) |
|
258
|
|
|
259
|
it("reads the text an embedder should encode from the vector field", () => { |
|
260
|
expect( |
|
261
|
sourceTextForEmbed({ id: "prod_1", embedding: "Red shoe" }, "embedding") |
|
262
|
).toBe("Red shoe") |
|
263
|
expect(sourceTextForEmbed({ id: "prod_1" }, "embedding")).toBeUndefined() |
|
264
|
expect(() => |
|
265
|
sourceTextForEmbed( |
|
266
|
{ id: "prod_1", embedding: [0.1, 0.2, 0.3] }, |
|
267
|
"embedding" |
|
268
|
) |
|
269
|
).toThrow(/must be a string/) |
|
270
|
}) |
|
271
|
}) |
|
272
|
|
|
273
|
describe("toWhereClause", () => { |
|
274
|
it("compiles $and / $or / $not and comparisons", () => { |
|
275
|
const plan = buildIndexPlan(baseDefinition()) |
|
276
|
const where = toWhereClause( |
|
277
|
{ |
|
278
|
$and: [ |
|
279
|
{ status: "published" }, |
|
280
|
{ |
|
281
|
$or: [{ price: { $gte: 10 } }, { tags: { $overlaps: ["sale"] } }], |
|
282
|
}, |
|
283
|
{ $not: { status: "draft" } }, |
|
284
|
], |
|
285
|
}, |
|
286
|
plan |
|
287
|
) |
|
288
|
|
|
289
|
expect(where?.sql).toContain("OR") |
|
290
|
expect(where?.sql).toContain("NOT") |
|
291
|
expect(where?.sql).toContain(`"indexed" @>`) |
|
292
|
expect(where?.params).toEqual( |
|
293
|
expect.arrayContaining([ |
|
294
|
JSON.stringify({ status: "published" }), |
|
295
|
10, |
|
296
|
JSON.stringify({ tags: ["sale"] }), |
|
297
|
JSON.stringify({ status: "draft" }), |
|
298
|
]) |
|
299
|
) |
|
300
|
}) |
|
301
|
|
|
302
|
it("treats bare equality on an array field as membership", () => { |
|
303
|
const plan = buildIndexPlan(baseDefinition()) |
|
304
|
const where = toWhereClause({ tags: "sale" }, plan) |
|
305
|
|
|
306
|
expect(where?.sql).toBe(`"indexed" @> ?::jsonb`) |
|
307
|
expect(where?.params).toEqual([JSON.stringify({ tags: ["sale"] })]) |
|
308
|
}) |
|
309
|
|
|
310
|
it("keeps numeric array values numeric in containment", () => { |
|
311
|
const plan = buildIndexPlan(baseDefinition()) |
|
312
|
const where = toWhereClause({ sizes: { $in: [38, 40] } }, plan) |
|
313
|
|
|
314
|
expect(where?.sql).toBe( |
|
315
|
`("indexed" @> ?::jsonb OR "indexed" @> ?::jsonb)` |
|
316
|
) |
|
317
|
expect(where?.params).toEqual([ |
|
318
|
JSON.stringify({ sizes: [38] }), |
|
319
|
JSON.stringify({ sizes: [40] }), |
|
320
|
]) |
|
321
|
}) |
|
322
|
|
|
323
|
it("compiles nested paths into nested containment documents", () => { |
|
324
|
const plan = buildIndexPlan(baseDefinition()) |
|
325
|
const where = toWhereClause({ "variants.color": "red" }, plan) |
|
326
|
|
|
327
|
expect(where?.params).toEqual([ |
|
328
|
JSON.stringify({ variants: { color: ["red"] } }), |
|
329
|
]) |
|
330
|
}) |
|
331
|
|
|
332
|
it("parenthesizes $exists so it cannot escape the conjunction", () => { |
|
333
|
const plan = buildIndexPlan(baseDefinition()) |
|
334
|
const where = toWhereClause( |
|
335
|
{ status: "published", deleted_at: { $exists: false } }, |
|
336
|
plan |
|
337
|
) |
|
338
|
|
|
339
|
const [, existsPart] = where!.sql.split(" AND ") |
|
340
|
expect(existsPart.startsWith("(")).toBe(true) |
|
341
|
expect(existsPart.endsWith(")")).toBe(true) |
|
342
|
expect(existsPart).toContain("IS NULL OR") |
|
343
|
}) |
|
344
|
|
|
345
|
it("rejects range operators on array fields", () => { |
|
346
|
const plan = buildIndexPlan(baseDefinition()) |
|
347
|
expect(() => toWhereClause({ tags: { $gt: "a" } }, plan)).toThrow( |
|
348
|
/array field/ |
|
349
|
) |
|
350
|
}) |
|
351
|
}) |
|
352
|
|
|
353
|
describe("buildFacetQuery", () => { |
|
354
|
it("binds range bounds ahead of the filter params", () => { |
|
355
|
const plan = buildIndexPlan(baseDefinition()) |
|
356
|
const [request] = normalizeFacetRequests( |
|
357
|
[ |
|
358
|
{ |
|
359
|
field: "price", |
|
360
|
type: "range", |
|
361
|
ranges: [ |
|
362
|
{ key: "low", from: 0, to: 50 }, |
|
363
|
{ key: "high", from: 50 }, |
|
364
|
], |
|
365
|
}, |
|
366
|
], |
|
367
|
plan |
|
368
|
) |
|
369
|
|
|
370
|
const query = buildFacetQuery({ |
|
371
|
table: "search_pg_product", |
|
372
|
whereSql: `"indexed" @> ?::jsonb`, |
|
373
|
whereParams: [JSON.stringify({ status: "published" })], |
|
374
|
request, |
|
375
|
plan, |
|
376
|
}) |
|
377
|
|
|
378
|
// SELECT-list placeholders come first in the SQL text. |
|
379
|
expect(query.params).toEqual([ |
|
380
|
0, |
|
381
|
50, |
|
382
|
50, |
|
383
|
JSON.stringify({ status: "published" }), |
|
384
|
]) |
|
385
|
}) |
|
386
|
}) |
|
387
|
|
|
388
|
describe("extractPrimaryKeyFilter", () => { |
|
389
|
it("recognises id membership", () => { |
|
390
|
const plan = buildIndexPlan(baseDefinition()) |
|
391
|
expect(extractPrimaryKeyFilter({ id: ["a", "b"] }, plan)).toEqual([ |
|
392
|
"a", |
|
393
|
"b", |
|
394
|
]) |
|
395
|
expect( |
|
396
|
extractPrimaryKeyFilter({ id: { $in: ["a"] }, status: "x" }, plan) |
|
397
|
).toBeUndefined() |
|
398
|
}) |
|
399
|
}) |
|
400
|
|
|
401
|
describe("resolveVectorField", () => { |
|
402
|
const vectorPlan = buildIndexPlan( |
|
403
|
baseDefinition({ |
|
404
|
fields: { |
|
405
|
id: { type: "keyword", filterable: true }, |
|
406
|
embedding: { type: "vector", dimensions: 3 }, |
|
407
|
}, |
|
408
|
}), |
|
409
|
"lakebase" |
|
410
|
) |
|
411
|
|
|
412
|
it("infers the field when the index has a single vector field", () => { |
|
413
|
expect(resolveVectorField({ value: [0.1, 0.2, 0.3] }, vectorPlan)).toBe( |
|
414
|
"embedding" |
|
415
|
) |
|
416
|
}) |
|
417
|
|
|
418
|
it("requires field when more than one vector is declared", () => { |
|
419
|
const multi = buildIndexPlan( |
|
420
|
baseDefinition({ |
|
421
|
fields: { |
|
422
|
id: { type: "keyword", filterable: true }, |
|
423
|
embedding: { type: "vector", dimensions: 3 }, |
|
424
|
image: { type: "vector", dimensions: 3 }, |
|
425
|
}, |
|
426
|
}), |
|
427
|
"lakebase" |
|
428
|
) |
|
429
|
|
|
430
|
expect(() => |
|
431
|
resolveVectorField({ value: [0.1, 0.2, 0.3] }, multi) |
|
432
|
).toThrow(/more than one vector field/) |
|
433
|
expect( |
|
434
|
resolveVectorField({ field: "image", value: [0.1, 0.2, 0.3] }, multi) |
|
435
|
).toBe("image") |
|
436
|
}) |
|
437
|
}) |
|
438
|
|
|
439
|
describe("helpers", () => { |
|
440
|
it("maps weights and sanitises table names", () => { |
|
441
|
expect(weightLabel({ weight: 3 })).toBe("A") |
|
442
|
expect(weightLabel(true)).toBe("D") |
|
443
|
expect(tableNameForIndex("Product Index")).toBe("search_pg_product_index") |
|
444
|
}) |
|
445
|
|
|
446
|
it("rejects unsupported query options", () => { |
|
447
|
expect(() => |
|
448
|
assertQuerySupported({ |
|
449
|
index: baseDefinition(), |
|
450
|
q: "red", |
|
451
|
attributes_to_retrieve: ["id"], |
|
452
|
search_options: { highlight: { fields: ["title"] } }, |
|
453
|
}) |
|
454
|
).toThrow(/highlight/) |
|
455
|
|
|
456
|
expect(() => |
|
457
|
assertQuerySupported({ |
|
458
|
index: baseDefinition(), |
|
459
|
attributes_to_retrieve: ["id"], |
|
460
|
search_options: { highlight: true, typo_tolerance: true }, |
|
461
|
}) |
|
462
|
).not.toThrow() |
|
463
|
|
|
464
|
expect(() => |
|
465
|
assertQuerySupported( |
|
466
|
{ |
|
467
|
index: baseDefinition(), |
|
468
|
attributes_to_retrieve: ["id"], |
|
469
|
search_options: { |
|
470
|
vector: { field: "embedding", value: [0.1, 0.2] }, |
|
471
|
}, |
|
472
|
}, |
|
473
|
"native" |
|
474
|
) |
|
475
|
).toThrow(/lakebase/) |
|
476
|
|
|
477
|
expect(() => |
|
478
|
assertQuerySupported({ |
|
479
|
index: baseDefinition(), |
|
480
|
attributes_to_retrieve: ["id"], |
|
481
|
search_options: { match_strategy: "last" }, |
|
482
|
}) |
|
483
|
).not.toThrow() |
|
484
|
|
|
485
|
expect(() => |
|
486
|
assertQuerySupported({ |
|
487
|
index: baseDefinition(), |
|
488
|
attributes_to_retrieve: ["id"], |
|
489
|
search_options: { match_strategy: "any" }, |
|
490
|
}) |
|
491
|
).not.toThrow() |
|
492
|
}) |
|
493
|
|
|
494
|
it("builds a prefix tsquery for match_strategy last", () => { |
|
495
|
expect(keywordTsQuerySql("medusa_search_english")).toBe( |
|
496
|
`plainto_tsquery('medusa_search_english', ?)` |
|
497
|
) |
|
498
|
expect(keywordTsQuerySql("medusa_search_english", "any")).toBe( |
|
499
|
`replace(plainto_tsquery('medusa_search_english', ?)::text, ' & ', ' | ')::tsquery` |
|
500
|
) |
|
501
|
expect(keywordTsQuerySql("medusa_search_english", "last")).toBe( |
|
502
|
`regexp_replace(plainto_tsquery('medusa_search_english', ?)::text, '''([^'']+)''$', '''\\1'':*')::tsquery` |
|
503
|
) |
|
504
|
}) |
|
505
|
}) |
|
506
|
}) |