|
1
|
import { Table, Prerequisites, CodeTabs, CodeTab } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Search Index Fields`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you'll learn about the field types and modifiers you can use in a [search index definition](../page.mdx). |
|
10
|
|
|
11
|
<Prerequisites |
|
12
|
items={[ |
|
13
|
{ |
|
14
|
text: "Medusa v2.21.1+", |
|
15
|
link: "https://github.com/medusajs/medusa/releases/tag/v2.21.1" |
|
16
|
} |
|
17
|
]} |
|
18
|
/> |
|
19
|
|
|
20
|
<Note> |
|
21
|
|
|
22
|
Refer to the [Search Index Field Modifiers guide](../modifiers/page.mdx) for details on what each modifier does and which types it applies to. |
|
23
|
|
|
24
|
</Note> |
|
25
|
|
|
26
|
<Note title="Tip"> |
|
27
|
|
|
28
|
The product indexes in this guide's snippets are simplified to show one feature at a time. For examples of indexing prices in multiple currencies, option values, categories, and other product data, refer to the [Product Index Examples guide](../../product-index-examples/page.mdx). |
|
29
|
|
|
30
|
</Note> |
|
31
|
|
|
32
|
## Define Index Fields |
|
33
|
|
|
34
|
A search index definition's `fields` property declares what the search engine holds. Use `search.define` to create a schema of field types and modifiers. |
|
35
|
|
|
36
|
For example: |
|
37
|
|
|
38
|
```ts title="src/search/product.ts" |
|
39
|
import { |
|
40
|
defineSearchIndex, |
|
41
|
search, |
|
42
|
} from "@medusajs/framework/utils" |
|
43
|
|
|
44
|
export const productIndex = defineSearchIndex({ |
|
45
|
fields: search.define({ |
|
46
|
title: search.text().searchable({ weight: 3 }), |
|
47
|
min_price: search.float().sortable().facetable(), |
|
48
|
// ... |
|
49
|
}), |
|
50
|
// ... |
|
51
|
}) |
|
52
|
``` |
|
53
|
|
|
54
|
Each field has a type, such as `search.text()`, and any number of chained modifiers, such as `.searchable()`. A type sets what the field holds, and a modifier declares what the engine can do with it. |
|
55
|
|
|
56
|
--- |
|
57
|
|
|
58
|
## Modifiers and Facet Types by Field Type |
|
59
|
|
|
60
|
A modifier that doesn't apply to a field type isn't available on it, so an invalid combination fails to compile. The following table shows which [modifiers](../modifiers/page.mdx) apply to which field types, and which [facet types](../modifiers/page.mdx#facet-types) each type allows. |
|
61
|
|
|
62
|
<Table> |
|
63
|
<Table.Header> |
|
64
|
<Table.Row> |
|
65
|
<Table.HeaderCell> |
|
66
|
Field Type |
|
67
|
</Table.HeaderCell> |
|
68
|
<Table.HeaderCell> |
|
69
|
Modifiers it Accepts |
|
70
|
</Table.HeaderCell> |
|
71
|
<Table.HeaderCell> |
|
72
|
Facet Types it Allows |
|
73
|
</Table.HeaderCell> |
|
74
|
</Table.Row> |
|
75
|
</Table.Header> |
|
76
|
<Table.Body> |
|
77
|
<Table.Row> |
|
78
|
<Table.Cell> |
|
79
|
`keyword`, `text` |
|
80
|
</Table.Cell> |
|
81
|
<Table.Cell> |
|
82
|
`searchable`, `filterable`, `sortable`, `facetable`, `array`, `retrievable`, `providerOptions` |
|
83
|
</Table.Cell> |
|
84
|
<Table.Cell> |
|
85
|
`value` |
|
86
|
</Table.Cell> |
|
87
|
</Table.Row> |
|
88
|
<Table.Row> |
|
89
|
<Table.Cell> |
|
90
|
`integer`, `float`, `date` |
|
91
|
</Table.Cell> |
|
92
|
<Table.Cell> |
|
93
|
`filterable`, `sortable`, `facetable`, `array`, `retrievable`, `providerOptions` |
|
94
|
</Table.Cell> |
|
95
|
<Table.Cell> |
|
96
|
`value`, `range`, `stats` |
|
97
|
</Table.Cell> |
|
98
|
</Table.Row> |
|
99
|
<Table.Row> |
|
100
|
<Table.Cell> |
|
101
|
`boolean`, `geo` |
|
102
|
</Table.Cell> |
|
103
|
<Table.Cell> |
|
104
|
`filterable`, `sortable`, `facetable`, `array`, `retrievable`, `providerOptions` |
|
105
|
</Table.Cell> |
|
106
|
<Table.Cell> |
|
107
|
`value` |
|
108
|
</Table.Cell> |
|
109
|
</Table.Row> |
|
110
|
<Table.Row> |
|
111
|
<Table.Cell> |
|
112
|
`object` |
|
113
|
</Table.Cell> |
|
114
|
<Table.Cell> |
|
115
|
`filterable`, `sortable`, `facetable`, `array`, `retrievable`, `providerOptions` |
|
116
|
</Table.Cell> |
|
117
|
<Table.Cell> |
|
118
|
`value` |
|
119
|
</Table.Cell> |
|
120
|
</Table.Row> |
|
121
|
<Table.Row> |
|
122
|
<Table.Cell> |
|
123
|
`vector` |
|
124
|
</Table.Cell> |
|
125
|
<Table.Cell> |
|
126
|
`embed`, `retrievable`, `providerOptions` |
|
127
|
</Table.Cell> |
|
128
|
<Table.Cell> |
|
129
|
None |
|
130
|
</Table.Cell> |
|
131
|
</Table.Row> |
|
132
|
</Table.Body> |
|
133
|
</Table> |
|
134
|
|
|
135
|
Only `integer`, `float`, and `date` accept a `types` option on `facetable`. Every other type takes `facetable()` as a boolean and allows `value` facets. Refer to [Facet Types](../modifiers/page.mdx#facet-types) for what each kind returns. |
|
136
|
|
|
137
|
<Note> |
|
138
|
|
|
139
|
Adding, removing, or changing a field changes the definition's hash, which the Search Module treats as a schema change. Refer to the [Reindexing and Migrations guide](../../reindexing/page.mdx) to learn how it rebuilds the index. |
|
140
|
|
|
141
|
</Note> |
|
142
|
|
|
143
|
--- |
|
144
|
|
|
145
|
## search.keyword() |
|
146
|
|
|
147
|
The `keyword` identifier indicates a string that is treated as a whole value. Use it for IDs, handles, statuses, and any value you filter or facet on. |
|
148
|
|
|
149
|
```ts title="src/search/product.ts" |
|
150
|
import { |
|
151
|
defineSearchIndex, |
|
152
|
search, |
|
153
|
} from "@medusajs/framework/utils" |
|
154
|
|
|
155
|
export const productIndex = defineSearchIndex({ |
|
156
|
fields: search.define({ |
|
157
|
status: search.keyword().filterable(), |
|
158
|
// ... |
|
159
|
}), |
|
160
|
// ... |
|
161
|
}) |
|
162
|
``` |
|
163
|
|
|
164
|
In this example, `status` is a `keyword` field, so a query can filter on it with `{ status: "published" }`. |
|
165
|
|
|
166
|
--- |
|
167
|
|
|
168
|
## search.text() |
|
169
|
|
|
170
|
The `text` identifier indicates a string analyzed for free-text matching. Use it for titles, descriptions, and other prose. |
|
171
|
|
|
172
|
```ts title="src/search/product.ts" |
|
173
|
import { |
|
174
|
defineSearchIndex, |
|
175
|
search, |
|
176
|
} from "@medusajs/framework/utils" |
|
177
|
|
|
178
|
export const productIndex = defineSearchIndex({ |
|
179
|
fields: search.define({ |
|
180
|
title: search.text().searchable({ weight: 3 }), |
|
181
|
description: search.text().searchable(), |
|
182
|
// ... |
|
183
|
}), |
|
184
|
// ... |
|
185
|
}) |
|
186
|
``` |
|
187
|
|
|
188
|
A `text` field only participates in free-text matching once you add the [searchable](../modifiers/page.mdx#searchable) modifier. Here `title` carries a higher weight than `description`, so a product matching a query in its title outranks one matching only in its description. |
|
189
|
|
|
190
|
### Search Text in Multiple Locales |
|
191
|
|
|
192
|
A `text` field holds a single string, and no field type stores translations of a value. An index only holds what its `seed` and `consume` functions write to it, so to match a free-text query in more than one locale, declare a field for each locale. |
|
193
|
|
|
194
|
How you declare those fields depends on the provider: |
|
195
|
|
|
196
|
<CodeTabs group="search-provider"> |
|
197
|
<CodeTab label="Medusa Search" value="medusa-search"> |
|
198
|
|
|
199
|
```ts title="src/search/product.ts" |
|
200
|
export const productIndex = defineSearchIndex({ |
|
201
|
name: "product", |
|
202
|
entity: "product", |
|
203
|
fields: search.define({ |
|
204
|
id: search.keyword().filterable(), |
|
205
|
title_en: search.text().searchable() |
|
206
|
.providerOptions({ |
|
207
|
"search-medusa": { |
|
208
|
full_text_search: { |
|
209
|
language: "english", |
|
210
|
stemming: true, |
|
211
|
}, |
|
212
|
}, |
|
213
|
}), |
|
214
|
title_fr: search.text().searchable() |
|
215
|
.providerOptions({ |
|
216
|
"search-medusa": { |
|
217
|
full_text_search: { |
|
218
|
language: "french", |
|
219
|
stemming: true, |
|
220
|
}, |
|
221
|
}, |
|
222
|
}), |
|
223
|
}), |
|
224
|
// ... |
|
225
|
}) |
|
226
|
``` |
|
227
|
|
|
228
|
</CodeTab> |
|
229
|
<CodeTab label="Other Providers" value="other-providers"> |
|
230
|
|
|
231
|
```ts title="src/search/product.ts" |
|
232
|
export const productIndex = defineSearchIndex({ |
|
233
|
name: "product", |
|
234
|
entity: "product", |
|
235
|
fields: search.define({ |
|
236
|
id: search.keyword().filterable(), |
|
237
|
title_en: search.text().searchable(), |
|
238
|
title_fr: search.text().searchable(), |
|
239
|
}), |
|
240
|
// ... |
|
241
|
}) |
|
242
|
``` |
|
243
|
|
|
244
|
</CodeTab> |
|
245
|
</CodeTabs> |
|
246
|
|
|
247
|
[Medusa Search](!cloud!/search) matches a searchable field term by term, and it stems those terms only when the field's `full_text_search` provider option asks for it. So its tab names a language per locale field and enables `stemming`, which gives each field the stemmer of its own language. Refer to [Set a Field's Language](!cloud!/search/settings#set-a-fields-language) for the languages and options it accepts. |
|
248
|
|
|
249
|
Other providers set the language for the whole index rather than per field, such as the `language` option of the [PostgreSQL Search Module Provider](../../providers/postgres/page.mdx#postgresql-search-module-provider-options), so a locale field needs no option of its own. |
|
250
|
|
|
251
|
Then, write each locale's value to its own field in the index's [`seed` function](../page.mdx#custom-seed-implementation). If you use the [Translation Module](../../../../commerce-modules/translation/page.mdx), pass the `locale` option to Query to retrieve the translated values of a locale: |
|
252
|
|
|
253
|
export const localeSeedHighlights = [ |
|
254
|
["9", "cursor", "Page by the primary key, so an interrupted run resumes."], |
|
255
|
["31", "withDeleted", "Include deleted products in the catch-up pass."], |
|
256
|
["39", "gone", "The products the index must lose."], |
|
257
|
["46", "locale", "Read the same products again in the other locale."], |
|
258
|
["70", "delete", "Remove the documents of the deleted products."], |
|
259
|
] |
|
260
|
|
|
261
|
```ts title="src/search/product.ts" highlights={localeSeedHighlights} |
|
262
|
export const productIndex = defineSearchIndex({ |
|
263
|
name: "product", |
|
264
|
entity: "product", |
|
265
|
fields: search.define({ |
|
266
|
// ... |
|
267
|
}), |
|
268
|
async *seed({ container, catchup, last_key: lastKey }) { |
|
269
|
const batchSize = 200 |
|
270
|
let cursor = lastKey |
|
271
|
|
|
272
|
while (true) { |
|
273
|
const { data: products } = await container.query |
|
274
|
.graph({ |
|
275
|
entity: "product", |
|
276
|
fields: [ |
|
277
|
"id", |
|
278
|
"title", |
|
279
|
"updated_at", |
|
280
|
"deleted_at", |
|
281
|
], |
|
282
|
filters: { |
|
283
|
...(catchup |
|
284
|
? { updated_at: { $gte: catchup.since } } |
|
285
|
: {}), |
|
286
|
...(cursor ? { id: { $gt: cursor } } : {}), |
|
287
|
}, |
|
288
|
pagination: { |
|
289
|
take: batchSize, |
|
290
|
order: { id: "ASC" }, |
|
291
|
}, |
|
292
|
withDeleted: !!catchup, |
|
293
|
}) |
|
294
|
|
|
295
|
if (!products.length) { |
|
296
|
return |
|
297
|
} |
|
298
|
|
|
299
|
const live = products.filter((p) => !p.deleted_at) |
|
300
|
const gone = products.filter((p) => !!p.deleted_at) |
|
301
|
|
|
302
|
const { data: frProducts } = await container.query |
|
303
|
.graph({ |
|
304
|
entity: "product", |
|
305
|
fields: ["id", "title"], |
|
306
|
filters: { id: live.map((p) => p.id) }, |
|
307
|
}, { locale: "fr-FR" }) |
|
308
|
|
|
309
|
const frTitles = new Map( |
|
310
|
frProducts.map((product) => [ |
|
311
|
product.id, |
|
312
|
product.title, |
|
313
|
]) |
|
314
|
) |
|
315
|
|
|
316
|
yield [ |
|
317
|
...(live.length |
|
318
|
? [{ |
|
319
|
action: "upsert" as const, |
|
320
|
documents: live.map((product) => ({ |
|
321
|
id: product.id, |
|
322
|
title_en: product.title, |
|
323
|
title_fr: |
|
324
|
frTitles.get(product.id) ?? |
|
325
|
product.title, |
|
326
|
})), |
|
327
|
}] |
|
328
|
: []), |
|
329
|
...(gone.length |
|
330
|
? [{ |
|
331
|
action: "delete" as const, |
|
332
|
filters: { id: gone.map((p) => p.id) }, |
|
333
|
}] |
|
334
|
: []), |
|
335
|
] |
|
336
|
|
|
337
|
if (products.length < batchSize) { |
|
338
|
return |
|
339
|
} |
|
340
|
|
|
341
|
cursor = products[products.length - 1].id |
|
342
|
} |
|
343
|
}, |
|
344
|
}) |
|
345
|
``` |
|
346
|
|
|
347
|
The `seed` function pages the products by their ID, so an interrupted run resumes from the `last_key` it receives rather than starting over. It also runs as the [catch-up pass](../page.mdx#handle-the-catch-up-pass) that follows a full run, which is why it reads `catchup`: the pass narrows the read to the products changed since the run started, includes the deleted ones, and yields a `delete` write for them so the index loses their documents. |
|
348
|
|
|
349
|
<Note title="Tip"> |
|
350
|
|
|
351
|
You can also build this `seed` with [`graphSeed`](../page.mdx#fill-the-index-from-query-with-graphseed) and its `transform` option, which is asynchronous and receives a whole page of rows. Refer to [Read More Data in transform](../page.mdx#read-more-data-in-transform) for an example that reads the same page again with the `locale` option. |
|
352
|
|
|
353
|
</Note> |
|
354
|
|
|
355
|
Then, when running a search query, such as in an API route, pass the `search_options.attributes_to_search_on` option to specify which locale-specific fields to search on: |
|
356
|
|
|
357
|
```ts |
|
358
|
const { data } = await query.search({ |
|
359
|
entity: "product", |
|
360
|
fields: ["id", "title_fr"], |
|
361
|
filters: { |
|
362
|
q: "chaussures", |
|
363
|
}, |
|
364
|
search_options: { |
|
365
|
attributes_to_search_on: ["title_fr"], |
|
366
|
}, |
|
367
|
}) |
|
368
|
``` |
|
369
|
|
|
370
|
--- |
|
371
|
|
|
372
|
## search.integer() |
|
373
|
|
|
374
|
The `integer` identifier indicates a whole number such as an inventory quantity. Useful for filtering, sorting, and faceting. It isn't matched as free text. |
|
375
|
|
|
376
|
```ts title="src/search/product.ts" |
|
377
|
import { |
|
378
|
defineSearchIndex, |
|
379
|
search, |
|
380
|
} from "@medusajs/framework/utils" |
|
381
|
|
|
382
|
export const productIndex = defineSearchIndex({ |
|
383
|
fields: search.define({ |
|
384
|
inventory_quantity: search |
|
385
|
.integer() |
|
386
|
.filterable() |
|
387
|
.sortable(), |
|
388
|
// ... |
|
389
|
}), |
|
390
|
// ... |
|
391
|
}) |
|
392
|
``` |
|
393
|
|
|
394
|
In this example, a query can filter on `inventory_quantity` and order results by it with `pagination.order`. Adding `facetable()` would also let a query summarize the matched documents by quantity brackets. |
|
395
|
|
|
396
|
--- |
|
397
|
|
|
398
|
## search.float() |
|
399
|
|
|
400
|
The `float` identifier indicates a decimal number, such as a price. Useful for filtering, sorting, and faceting. It isn't matched as free text. |
|
401
|
|
|
402
|
```ts title="src/search/product.ts" |
|
403
|
import { |
|
404
|
defineSearchIndex, |
|
405
|
search, |
|
406
|
} from "@medusajs/framework/utils" |
|
407
|
|
|
408
|
export const productIndex = defineSearchIndex({ |
|
409
|
fields: search.define({ |
|
410
|
min_price: search |
|
411
|
.float() |
|
412
|
.filterable() |
|
413
|
.sortable() |
|
414
|
.facetable({ types: ["range"] }), |
|
415
|
// ... |
|
416
|
}), |
|
417
|
// ... |
|
418
|
}) |
|
419
|
``` |
|
420
|
|
|
421
|
In this example, a query can filter on `min_price`, order results by it with `pagination.order`, and facet on it to summarize the matched documents by price brackets. |
|
422
|
|
|
423
|
A range facet on a price turns into the price brackets a storefront shows beside its results. Learn more in the [Search Index Field Modifiers guide](../modifiers/page.mdx#facet-types). |
|
424
|
|
|
425
|
--- |
|
426
|
|
|
427
|
## search.boolean() |
|
428
|
|
|
429
|
The `boolean` identifier indicates a `true` or `false` value. Use it for flags such as whether a product is a gift card. Useful for filtering and faceting. It isn't matched as free text. |
|
430
|
|
|
431
|
```ts title="src/search/product.ts" |
|
432
|
import { |
|
433
|
defineSearchIndex, |
|
434
|
search, |
|
435
|
} from "@medusajs/framework/utils" |
|
436
|
|
|
437
|
export const productIndex = defineSearchIndex({ |
|
438
|
fields: search.define({ |
|
439
|
is_giftcard: search.boolean().filterable(), |
|
440
|
// ... |
|
441
|
}), |
|
442
|
// ... |
|
443
|
}) |
|
444
|
``` |
|
445
|
|
|
446
|
In this example, a query can filter on `is_giftcard` to find only gift cards or the opposite. |
|
447
|
|
|
448
|
--- |
|
449
|
|
|
450
|
## search.date() |
|
451
|
|
|
452
|
The `date` identifier indicates a date. A document can hold either a `Date` object or an ISO string, since `query.graph` returns `Date` objects while an event payload carries a string. Useful for filtering, sorting, and faceting. It isn't matched as free text. |
|
453
|
|
|
454
|
```ts title="src/search/product.ts" |
|
455
|
import { |
|
456
|
defineSearchIndex, |
|
457
|
search, |
|
458
|
} from "@medusajs/framework/utils" |
|
459
|
|
|
460
|
export const productIndex = defineSearchIndex({ |
|
461
|
fields: search.define({ |
|
462
|
created_at: search.date().filterable().sortable(), |
|
463
|
// ... |
|
464
|
}), |
|
465
|
// ... |
|
466
|
}) |
|
467
|
``` |
|
468
|
|
|
469
|
In this example, a query can filter on `created_at` and order results by it with `pagination.order`. Adding `facetable()` would also let a query summarize the matched documents by date brackets. |
|
470
|
|
|
471
|
--- |
|
472
|
|
|
473
|
## search.geo() |
|
474
|
|
|
475
|
The `geo` identifier searches over `{ lat, lng }` coordinate pairs. Use it for locations such as warehouses or stores. Useful for filtering by proximity. It isn't matched as free text. |
|
476
|
|
|
477
|
<Note title="Tip"> |
|
478
|
|
|
479
|
No provider that Medusa ships supports geo fields. You can support it in your custom Search Module Provider. |
|
480
|
|
|
481
|
</Note> |
|
482
|
|
|
483
|
```ts title="src/search/warehouse.ts" |
|
484
|
import { |
|
485
|
defineSearchIndex, |
|
486
|
search, |
|
487
|
} from "@medusajs/framework/utils" |
|
488
|
|
|
489
|
export const warehouseIndex = defineSearchIndex({ |
|
490
|
fields: search.define({ |
|
491
|
location: search.geo().filterable(), |
|
492
|
// ... |
|
493
|
}), |
|
494
|
// ... |
|
495
|
}) |
|
496
|
``` |
|
497
|
|
|
498
|
In this example, a query can filter on `location` to find documents within a radius of a point. |
|
499
|
|
|
500
|
### Yield a Coordinate Pair |
|
501
|
|
|
502
|
A field's type describes the document your index holds, not a property on your data models. So the `{ lat, lng }` shape is what `seed` and `consume` must yield, no matter how you store the coordinates. |
|
503
|
|
|
504
|
For example, if your data model holds `latitude` and `longitude` as separate properties, combine them into the pair when you yield the documents: |
|
505
|
|
|
506
|
```ts title="src/search/warehouse.ts" |
|
507
|
import { |
|
508
|
defineSearchIndex, |
|
509
|
search, |
|
510
|
} from "@medusajs/framework/utils" |
|
511
|
|
|
512
|
export const warehouseIndex = defineSearchIndex({ |
|
513
|
name: "warehouse", |
|
514
|
entity: "warehouse", |
|
515
|
fields: search.define({ |
|
516
|
id: search.keyword().filterable(), |
|
517
|
name: search.text().searchable(), |
|
518
|
location: search.geo().filterable(), |
|
519
|
}), |
|
520
|
async *seed({ container }) { |
|
521
|
const { data } = await container.query.graph({ |
|
522
|
entity: "warehouse", |
|
523
|
fields: ["id", "name", "latitude", "longitude"], |
|
524
|
}) |
|
525
|
|
|
526
|
yield [{ |
|
527
|
action: "upsert", |
|
528
|
documents: data.map((warehouse) => ({ |
|
529
|
id: warehouse.id, |
|
530
|
name: warehouse.name, |
|
531
|
location: { |
|
532
|
lat: warehouse.latitude, |
|
533
|
lng: warehouse.longitude, |
|
534
|
}, |
|
535
|
})), |
|
536
|
}] |
|
537
|
}, |
|
538
|
}) |
|
539
|
``` |
|
540
|
|
|
541
|
`search.define` type-checks what you yield against the fields you declared, so a document whose `location` isn't a `{ lat, lng }` pair fails to compile. |
|
542
|
|
|
543
|
--- |
|
544
|
|
|
545
|
## search.object() |
|
546
|
|
|
547
|
The `object` identifier indicates a nested object. Pass its sub-fields as a schema, using the same types and [modifiers](../modifiers/page.mdx) as top-level fields. |
|
548
|
|
|
549
|
```ts title="src/search/product.ts" |
|
550
|
import { |
|
551
|
defineSearchIndex, |
|
552
|
search, |
|
553
|
} from "@medusajs/framework/utils" |
|
554
|
|
|
555
|
export const productIndex = defineSearchIndex({ |
|
556
|
fields: search.define({ |
|
557
|
brand: search.object({ |
|
558
|
name: search.keyword().searchable().facetable(), |
|
559
|
country: search.keyword().filterable(), |
|
560
|
}), |
|
561
|
// ... |
|
562
|
}), |
|
563
|
// ... |
|
564
|
}) |
|
565
|
``` |
|
566
|
|
|
567
|
In this example, a query can filter on `brand.country`, search on `brand.name`, and facet on `brand.name` to summarize the matched documents by brand. |
|
568
|
|
|
569
|
### Define as an Array of Objects |
|
570
|
|
|
571
|
Add the [array](../modifiers/page.mdx#array) modifier to an `object` field to indicate that the field holds an array of objects, rather than a single object. |
|
572
|
|
|
573
|
```ts title="src/search/product.ts" |
|
574
|
import { |
|
575
|
defineSearchIndex, |
|
576
|
search, |
|
577
|
} from "@medusajs/framework/utils" |
|
578
|
|
|
579
|
export const productIndex = defineSearchIndex({ |
|
580
|
fields: search.define({ |
|
581
|
variants: search |
|
582
|
.object({ |
|
583
|
title: search.text().searchable(), |
|
584
|
sku: search.keyword().filterable(), |
|
585
|
}) |
|
586
|
.array(), |
|
587
|
// ... |
|
588
|
}), |
|
589
|
// ... |
|
590
|
}) |
|
591
|
``` |
|
592
|
|
|
593
|
In this example, a query can filter on `variants.sku` to find products that have a variant with a specific SKU. |
|
594
|
|
|
595
|
<Note> |
|
596
|
|
|
597
|
Filters on the sub-fields of an object array match across elements. So filtering a product on `variants.color = "red"` and `variants.size = "XL"` also matches a product with a red small variant and a blue extra-large one. No provider Medusa ships can restrict the match to a single element. |
|
598
|
|
|
599
|
</Note> |
|
600
|
|
|
601
|
--- |
|
602
|
|
|
603
|
## search.vector() |
|
604
|
|
|
605
|
<Note title="Tip"> |
|
606
|
|
|
607
|
Vector search with Medusa Search is available on the Scale and Enterprise plans. On the Scale plan, you compute the embeddings yourself, whereas the Enterprise plan can also [create them for you](#let-medusa-search-create-the-embedding). |
|
608
|
|
|
609
|
</Note> |
|
610
|
|
|
611
|
The `vector` identifier indicates an embedding, used for [vector search](!docs!/learn/fundamentals/query/search#vector-and-hybrid-search). Unlike other types, a `vector` field can't be filtered, sorted, faceted, or turned into an array, so those modifiers aren't available on it. |
|
612
|
|
|
613
|
`vector` accepts the number of dimensions in the embedding, which is how many numbers are in the list. |
|
614
|
|
|
615
|
```ts title="src/search/product.ts" |
|
616
|
import { |
|
617
|
defineSearchIndex, |
|
618
|
search, |
|
619
|
} from "@medusajs/framework/utils" |
|
620
|
|
|
621
|
export const productIndex = defineSearchIndex({ |
|
622
|
fields: search.define({ |
|
623
|
title_embedding: search.vector(1536), |
|
624
|
// ... |
|
625
|
}), |
|
626
|
// ... |
|
627
|
}) |
|
628
|
``` |
|
629
|
|
|
630
|
In this example, `title_embedding` is a `vector` field with 1536 dimensions, matching the output of OpenAI's `text-embedding-3-small` model. Your `seed` and `consume` functions yield the embedding for the field, so you compute it yourself before you yield the document. |
|
631
|
|
|
632
|
With [Medusa Search](!cloud!/search), an embedding you compute yourself can have at most 1536 dimensions. Medusa Search rejects a query whose embedding is longer than that. |
|
633
|
|
|
634
|
<Note> |
|
635
|
|
|
636
|
A `vector` field doesn't come back on a hit by default, since a list of 1536 numbers is rarely useful to the caller. Chain [retrievable()](../modifiers/page.mdx#retrievable) on it if you need the embedding in the result. |
|
637
|
|
|
638
|
</Note> |
|
639
|
|
|
640
|
### Let Medusa Search Create the Embedding |
|
641
|
|
|
642
|
With [Medusa Search](!cloud!/search), you can chain the [embed](../modifiers/page.mdx#embed) modifier on the field. Medusa Search then turns the text your documents pass on that field into an embedding as it indexes each document, and embeds a query's text the same way at query time. |
|
643
|
|
|
644
|
For example: |
|
645
|
|
|
646
|
```ts title="src/search/product.ts" |
|
647
|
import { |
|
648
|
defineSearchIndex, |
|
649
|
search, |
|
650
|
} from "@medusajs/framework/utils" |
|
651
|
|
|
652
|
export const productIndex = defineSearchIndex({ |
|
653
|
fields: search.define({ |
|
654
|
title: search.text().searchable(), |
|
655
|
title_embedding: search.vector(1536).embed(), |
|
656
|
// ... |
|
657
|
}), |
|
658
|
// ... |
|
659
|
}) |
|
660
|
``` |
|
661
|
|
|
662
|
An embedded field changes what your documents carry and how you query it: |
|
663
|
|
|
664
|
- Your documents pass the text to embed as a string on the field itself, rather than a `number[]`. `search.define` types the field as a string once you chain `embed()`, so a document that passes an array fails to compile. |
|
665
|
- Medusa Search replaces that text with the embedding it computes, so the source text isn't stored under the field. |
|
666
|
- A query can pass raw text as `search_options.vector.query`, which Medusa Search embeds at query time to run a semantic search. Without `embed`, a query can only search the field with a pre-computed embedding passed as `search_options.vector.value`. Refer to [Vector and Hybrid Search](!docs!/learn/fundamentals/query/search#vector-and-hybrid-search) for an example of both. |
|
667
|
|
|
668
|
So, your `seed` and `consume` functions pass the text on `title_embedding` rather than an embedding: |
|
669
|
|
|
670
|
```ts title="src/search/product.ts" |
|
671
|
import { |
|
672
|
defineSearchIndex, |
|
673
|
search, |
|
674
|
} from "@medusajs/framework/utils" |
|
675
|
|
|
676
|
export const productIndex = defineSearchIndex({ |
|
677
|
name: "product", |
|
678
|
entity: "product", |
|
679
|
fields: search.define({ |
|
680
|
id: search.keyword().filterable(), |
|
681
|
title: search.text().searchable(), |
|
682
|
title_embedding: search.vector(1536).embed(), |
|
683
|
// ... |
|
684
|
}), |
|
685
|
async *seed({ container }) { |
|
686
|
const { data: products } = await container.query.graph({ |
|
687
|
entity: "product", |
|
688
|
fields: ["id", "title"], |
|
689
|
}) |
|
690
|
|
|
691
|
yield [{ |
|
692
|
action: "upsert", |
|
693
|
documents: products.map((product) => ({ |
|
694
|
id: product.id, |
|
695
|
title: product.title, |
|
696
|
title_embedding: product.title, |
|
697
|
})), |
|
698
|
}] |
|
699
|
}, |
|
700
|
// ... |
|
701
|
}) |
|
702
|
``` |
|
703
|
|
|
704
|
Then, you can perform a semantic search on the `title_embedding` field by passing raw text as the query: |
|
705
|
|
|
706
|
```ts |
|
707
|
const { data } = await query.search({ |
|
708
|
entity: "product", |
|
709
|
fields: ["id", "title"], |
|
710
|
search_options: { |
|
711
|
vector: { |
|
712
|
field: "title_embedding", |
|
713
|
query: "comfortable shoes for long runs", |
|
714
|
}, |
|
715
|
}, |
|
716
|
}) |
|
717
|
``` |
|
718
|
|
|
719
|
<Note> |
|
720
|
|
|
721
|
Medusa Search decides which embedding model produces the vector, so you don't choose one in the definition. Refer to [Semantic Search with Medusa Search](!cloud!/search/semantic-search) for more details. |
|
722
|
|
|
723
|
</Note> |
|
724
|
|
|
725
|
### Why the Number of Dimensions Matters |
|
726
|
|
|
727
|
You must declare the number of dimensions in a `vector` field because the provider needs to know how to store and compare the embeddings: |
|
728
|
|
|
729
|
- The provider creates a column or index sized for exactly that many numbers, so it needs the value before any document exists. |
|
730
|
- Two embeddings are only comparable if they have the same number of dimensions, so the provider rejects a document or query whose embedding is a different length. |