|
1
|
# PostgreSQL search provider |
|
2
|
|
|
3
|
Search provider for Medusa backed by PostgreSQL, with two engines: |
|
4
|
|
|
5
|
| Engine | When to use | Keyword | Vector | |
|
6
|
| ------------------ | ---------------------------------- | --------------------------- | -------------------- | |
|
7
|
| `native` (default) | Local / self-hosted / any Postgres | GIN + `ts_rank` + `pg_trgm` | Not supported | |
|
8
|
| `lakebase` | Medusa Cloud / Lakebase Search | `lakebase_bm25` (BM25) | `lakebase_ann` (ANN) | |
|
9
|
|
|
10
|
## Enable it |
|
11
|
|
|
12
|
### Native (default, works locally and on Medusa Cloud) |
|
13
|
|
|
14
|
```ts |
|
15
|
modules: [ |
|
16
|
{ |
|
17
|
resolve: "@medusajs/medusa/search", |
|
18
|
options: { |
|
19
|
providers: [ |
|
20
|
{ |
|
21
|
resolve: "@medusajs/medusa/search-postgres", |
|
22
|
id: "postgres", |
|
23
|
options: { |
|
24
|
// engine: "native", // default |
|
25
|
// language: "english", |
|
26
|
}, |
|
27
|
}, |
|
28
|
], |
|
29
|
}, |
|
30
|
}, |
|
31
|
] |
|
32
|
``` |
|
33
|
|
|
34
|
### Lakebase (works only on Medusa Cloud) |
|
35
|
|
|
36
|
```ts |
|
37
|
{ |
|
38
|
resolve: "@medusajs/medusa/search-postgres", |
|
39
|
id: "postgres", |
|
40
|
options: { |
|
41
|
engine: "lakebase", |
|
42
|
// Optional: embed text for search_options.vector.query |
|
43
|
// embedder: async (text) => { ... return number[] }, |
|
44
|
// vector_distance: "cosine", // or "l2" | "inner_product" |
|
45
|
}, |
|
46
|
} |
|
47
|
``` |
|
48
|
|
|
49
|
Then: |
|
50
|
|
|
51
|
```bash |
|
52
|
npx medusa db:migrate |
|
53
|
npx medusa db:migrate-search |
|
54
|
``` |
|
55
|
|
|
56
|
The migration enables `pg_trgm` + `unaccent` and creates the catalog. On Medusa Cloud it also enables `lakebase_vector` and `lakebase_text` (`CREATE EXTENSION ... CASCADE`). Those statements soft-fail on engines that do not ship Lakebase Search. |
|
57
|
|
|
58
|
## Provider options |
|
59
|
|
|
60
|
| Option | Default | Description | |
|
61
|
| ----------------- | ----------- | -------------------------------------------------------------------------------------- | |
|
62
|
| `engine` | `"native"` | `"native"` or `"lakebase"` | |
|
63
|
| `language` | `"english"` | Text search config. With `unaccent`, creates `medusa_search_<language>`. | |
|
64
|
| `embedder` | — | `(text) => Promise<number[]>`. Required for `search_options.vector.query` on lakebase. | |
|
65
|
| `vector_distance` | `"cosine"` | ANN metric: `cosine`, `l2`, or `inner_product`. | |
|
66
|
|
|
67
|
## Vector fields (lakebase only) |
|
68
|
|
|
69
|
On the native engine vector fields are not rejected: the provider logs a warning and drops them from the index, so one definition stays portable across providers. A query that asks for `search_options.vector` there is still rejected, since answering it with keyword results would be wrong. |
|
70
|
|
|
71
|
Supply embeddings yourself: |
|
72
|
|
|
73
|
```ts |
|
74
|
defineSearchIndex({ |
|
75
|
name: "product", |
|
76
|
entity: "product", |
|
77
|
fields: search.define({ |
|
78
|
id: search.keyword().filterable(), |
|
79
|
title: search.text().searchable({ weight: 3 }), |
|
80
|
embedding: search.vector(1536), |
|
81
|
}), |
|
82
|
// ... |
|
83
|
}) |
|
84
|
``` |
|
85
|
|
|
86
|
Documents must include the embedding array on upsert. Query with `search_options.vector.value`. |
|
87
|
|
|
88
|
Or let the provider embed a string on the same field (`.embed()` requires `embedder`): |
|
89
|
|
|
90
|
```ts |
|
91
|
embedding: search.vector(1536).embed() |
|
92
|
|
|
93
|
// documents: { embedding: "title and description to encode" } |
|
94
|
|
|
95
|
await query.search({ |
|
96
|
entity: "product", |
|
97
|
search_options: { |
|
98
|
vector: { |
|
99
|
field: "embedding", |
|
100
|
query: "red shoes", |
|
101
|
semantic_ratio: 0.5, // 0 = keyword, 1 = vector, in between = RRF hybrid |
|
102
|
}, |
|
103
|
}, |
|
104
|
}) |
|
105
|
``` |
|
106
|
|
|
107
|
Query with a client-supplied embedding against either kind of field: |
|
108
|
|
|
109
|
```ts |
|
110
|
await query.search({ |
|
111
|
entity: "product", |
|
112
|
filters: { q: "red shoes" }, |
|
113
|
search_options: { |
|
114
|
vector: { |
|
115
|
field: "embedding", |
|
116
|
value: embeddingArray, |
|
117
|
semantic_ratio: 0.5, |
|
118
|
}, |
|
119
|
}, |
|
120
|
}) |
|
121
|
``` |
|
122
|
|
|
123
|
## What it supports |
|
124
|
|
|
125
|
| | native | lakebase | |
|
126
|
| ---------------- | ------------------------------------------------------- | ------------------------ | |
|
127
|
| Free text | `ts_rank` + weights | BM25 via `lakebase_bm25` | |
|
128
|
| Typo tolerance | `pg_trgm` (`word_similarity`) | `pg_trgm` (same) | |
|
129
|
| `match_strategy` | `"all"` (default), `"any"`, `"last"` | same | |
|
130
|
| Filters | `$eq` `$ne` `$in` `$nin` ranges, `$and`/`$or`/`$not`, … | same | |
|
131
|
| Facets | value, range, stats — scoped to the query matches | same | |
|
132
|
| `distinct` | one hit per value, count follows | same | |
|
133
|
| `min_score` | yes, keeps the requested sort | same | |
|
134
|
| Vector / hybrid | fields ignored, queries rejected | ANN + RRF | |
|
135
|
|
|
136
|
Unsupported on both (rejected explicitly): highlighting, geo, cursor pagination, query-time locales. |
|
137
|
|
|
138
|
`"last"` is typeahead: completed terms must match in full and the last term is a prefix, so `"dtc sta"` matches `"Dtc starter"`. |
|
139
|
|
|
140
|
### Filter semantics |
|
141
|
|
|
142
|
- Equality and `$in` compile to jsonb containment (`indexed @> …`), which the |
|
143
|
`jsonb_path_ops` GIN index accelerates and which compares numbers and |
|
144
|
booleans natively. |
|
145
|
- On array fields, a bare value or `$eq` means membership — `{ tags: "sale" }` |
|
146
|
matches documents whose `tags` array contains `"sale"`. |
|
147
|
- Range operators (`$gt`, …), `$prefix` and `$like` are rejected on array |
|
148
|
fields. |
|
149
|
|
|
150
|
### Hybrid queries (lakebase) |
|
151
|
|
|
152
|
- Results are rank-fused (RRF), so they can only be ordered by `_score`. |
|
153
|
- `min_score` applies to the fused RRF score (per-arm contributions are at most |
|
154
|
`weight / 61`). |
|
155
|
- `metadata.count` is the exact size of the union of both arms' match sets; |
|
156
|
the fused hit list itself is a bounded candidate window. |