|
1
|
import { Table, Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `PostgreSQL Search Module Provider`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
The PostgreSQL Search Module Provider indexes and searches documents in your Medusa application's PostgreSQL database, using PostgreSQL's built-in full-text search. It can be used for development and in production. |
|
10
|
|
|
11
|
<Note title="Easier setup with Cloud"> |
|
12
|
|
|
13
|
Cloud provides pre-configured search infrastructure for your Medusa application. You can use it to set up without configuring and managing your own search engine. Learn more in the [Medusa Search](!cloud!/search) documentation. |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
Refer to [Medusa Search vs PostgreSQL](!cloud!/search/postgres) for how this provider's support for index definitions, filters, facets, and search options differs from the other providers. |
|
18
|
|
|
19
|
<Prerequisites |
|
20
|
items={[ |
|
21
|
{ |
|
22
|
text: "Medusa v2.21.1+", |
|
23
|
link: "https://github.com/medusajs/medusa/releases/tag/v2.21.1" |
|
24
|
} |
|
25
|
]} |
|
26
|
/> |
|
27
|
|
|
28
|
--- |
|
29
|
|
|
30
|
## Register the PostgreSQL Search Module Provider |
|
31
|
|
|
32
|
Medusa registers the PostgreSQL Search Module Provider locally by default, so you don't need to configure anything to use it. Its identifier is `search-postgres`. |
|
33
|
|
|
34
|
### Change the Provider's Options |
|
35
|
|
|
36
|
To change the PostgreSQL Search Module Provider's default options, or to use the provider in all environments, register the [Search Module](../../page.mdx) with the provider in `medusa-config.ts`: |
|
37
|
|
|
38
|
```ts title="medusa-config.ts" |
|
39
|
// To register only for development. This is necessary to use Medusa Search in Cloud |
|
40
|
const isDev = process.env.NODE_ENV !== "production" |
|
41
|
|
|
42
|
module.exports = defineConfig({ |
|
43
|
// ... |
|
44
|
modules: [ |
|
45
|
isDev && { |
|
46
|
resolve: "@medusajs/medusa/search", |
|
47
|
options: { |
|
48
|
// Only needed with more than one provider. |
|
49
|
// default_provider: "search-postgres", |
|
50
|
providers: [ |
|
51
|
{ |
|
52
|
resolve: "@medusajs/medusa/search-postgres", |
|
53
|
id: "search-postgres", |
|
54
|
options: { |
|
55
|
// requires a medusa_search_german text search configuration |
|
56
|
language: "german", |
|
57
|
}, |
|
58
|
}, |
|
59
|
// ... |
|
60
|
], |
|
61
|
}, |
|
62
|
}, |
|
63
|
].filter(Boolean), |
|
64
|
}) |
|
65
|
``` |
|
66
|
|
|
67
|
### Run Migrations |
|
68
|
|
|
69
|
If you're registering the provider for the first time, such as in an existing Medusa application, run the following command to create the necessary database tables for the provider: |
|
70
|
|
|
71
|
```bash npm2yarn |
|
72
|
npx medusa db:migrate |
|
73
|
``` |
|
74
|
|
|
75
|
### PostgreSQL Search Module Provider Options |
|
76
|
|
|
77
|
<Table> |
|
78
|
<Table.Header> |
|
79
|
<Table.Row> |
|
80
|
<Table.HeaderCell> |
|
81
|
Option |
|
82
|
</Table.HeaderCell> |
|
83
|
<Table.HeaderCell> |
|
84
|
Description |
|
85
|
</Table.HeaderCell> |
|
86
|
<Table.HeaderCell> |
|
87
|
Default |
|
88
|
</Table.HeaderCell> |
|
89
|
</Table.Row> |
|
90
|
</Table.Header> |
|
91
|
<Table.Body> |
|
92
|
<Table.Row> |
|
93
|
<Table.Cell> |
|
94
|
`language` |
|
95
|
</Table.Cell> |
|
96
|
<Table.Cell> |
|
97
|
The text search configuration language used to analyze text, such as `english` or `german`. Refer to [Custom Languages](#support-custom-languages). |
|
98
|
</Table.Cell> |
|
99
|
<Table.Cell> |
|
100
|
`english` |
|
101
|
</Table.Cell> |
|
102
|
</Table.Row> |
|
103
|
<Table.Row> |
|
104
|
<Table.Cell> |
|
105
|
`engine` |
|
106
|
</Table.Cell> |
|
107
|
<Table.Cell> |
|
108
|
The search backend to use, which can be `native`. |
|
109
|
</Table.Cell> |
|
110
|
<Table.Cell> |
|
111
|
`native` |
|
112
|
</Table.Cell> |
|
113
|
</Table.Row> |
|
114
|
</Table.Body> |
|
115
|
</Table> |
|
116
|
|
|
117
|
## Test the PostgreSQL Search Module Provider |
|
118
|
|
|
119
|
To test the provider, make sure your application has a `product` index definition and that the index is allowed on the `/store/search` API route, as explained in the [Search Products guide](../../page.mdx#search-products). |
|
120
|
|
|
121
|
First, start your Medusa application: |
|
122
|
|
|
123
|
```bash npm2yarn |
|
124
|
npm run dev |
|
125
|
``` |
|
126
|
|
|
127
|
Then, send a request to the [Store Search API route](!api!/store/search/search-indexes): |
|
128
|
|
|
129
|
```bash |
|
130
|
curl -X POST "http://localhost:9000/store/search" \ |
|
131
|
-H "x-publishable-api-key: pk_123" \ |
|
132
|
-H "Content-Type: application/json" \ |
|
133
|
--data '{ |
|
134
|
"entity": "product", |
|
135
|
"filters": { "q": "shirt" } |
|
136
|
}' |
|
137
|
``` |
|
138
|
|
|
139
|
Make sure to replace `pk_123` with a publishable API key, which you can find under Settings -> Publishable API Keys in the Medusa Admin dashboard. |
|
140
|
|
|
141
|
You'll receive the products whose searchable fields match the query, ordered by relevance: |
|
142
|
|
|
143
|
```json title="Example Response" |
|
144
|
{ |
|
145
|
"results": [ |
|
146
|
{ |
|
147
|
"hits": [ |
|
148
|
{ |
|
149
|
"id": "prod_01KXR3J9J610DT161E2E4ZS6P1", |
|
150
|
"score": 1.23, |
|
151
|
"document": { |
|
152
|
"id": "prod_01KXR3J9J610DT161E2E4ZS6P1", |
|
153
|
"title": "Medusa T-Shirt", |
|
154
|
"handle": "t-shirt" |
|
155
|
} |
|
156
|
} |
|
157
|
], |
|
158
|
"metadata": { |
|
159
|
"skip": 0, |
|
160
|
"take": 20, |
|
161
|
"count": 1, |
|
162
|
"query": "shirt" |
|
163
|
} |
|
164
|
} |
|
165
|
] |
|
166
|
} |
|
167
|
``` |
|
168
|
|
|
169
|
### Search Custom Data with the PostgreSQL Search Module Provider |
|
170
|
|
|
171
|
To search data other than products, such as a custom data model of your own module, declare a search index for it. The PostgreSQL Search Module Provider then creates the physical index, fills it, and serves searches on it the same way it does for products. |
|
172
|
|
|
173
|
Learn how to declare an index and search it in the [Search Other Entities guide](../../page.mdx#search-other-entities), and learn about the properties you can set on an index in the [Search Index Definitions guide](../../index-definitions/page.mdx). |
|
174
|
|
|
175
|
|
|
176
|
--- |
|
177
|
|
|
178
|
## Database Requirements in the PostgreSQL Search Module Provider |
|
179
|
|
|
180
|
### What the Migration Creates |
|
181
|
|
|
182
|
Running `db:migrate` creates everything the `native` engine needs: |
|
183
|
|
|
184
|
<Table> |
|
185
|
<Table.Header> |
|
186
|
<Table.Row> |
|
187
|
<Table.HeaderCell> |
|
188
|
Object |
|
189
|
</Table.HeaderCell> |
|
190
|
<Table.HeaderCell> |
|
191
|
What it's For |
|
192
|
</Table.HeaderCell> |
|
193
|
</Table.Row> |
|
194
|
</Table.Header> |
|
195
|
<Table.Body> |
|
196
|
<Table.Row> |
|
197
|
<Table.Cell> |
|
198
|
`pg_trgm` extension |
|
199
|
</Table.Cell> |
|
200
|
<Table.Cell> |
|
201
|
Typo tolerance, which the provider applies with trigram word similarity. |
|
202
|
</Table.Cell> |
|
203
|
</Table.Row> |
|
204
|
<Table.Row> |
|
205
|
<Table.Cell> |
|
206
|
`unaccent` extension |
|
207
|
</Table.Cell> |
|
208
|
<Table.Cell> |
|
209
|
Accent-insensitive matching, so a search for `cafe` matches `café`. |
|
210
|
</Table.Cell> |
|
211
|
</Table.Row> |
|
212
|
<Table.Row> |
|
213
|
<Table.Cell> |
|
214
|
`medusa_search_english` text search configuration |
|
215
|
</Table.Cell> |
|
216
|
<Table.Cell> |
|
217
|
The configuration the provider analyzes text with. It copies PostgreSQL's `english` configuration and adds `unaccent`. |
|
218
|
</Table.Cell> |
|
219
|
</Table.Row> |
|
220
|
</Table.Body> |
|
221
|
</Table> |
|
222
|
|
|
223
|
<Note title="Tip"> |
|
224
|
|
|
225
|
Creating an extension needs privileges that your application's database role may not have on managed PostgreSQL. The migration doesn't fail in that case, so check its output. If it reports that it couldn't enable an extension, create it yourself with a privileged role before you search. |
|
226
|
|
|
227
|
</Note> |
|
228
|
|
|
229
|
### Support Custom Languages |
|
230
|
|
|
231
|
The migration only creates the `medusa_search_english` configuration. If you set the `language` option to something else, create a matching `medusa_search_<language>` text search configuration. |
|
232
|
|
|
233
|
To do that, add a [data migration script](!docs!/learn/fundamentals/data-models/write-migration#data-migration-scripts) in the `src/migration-scripts` directory. For example, create the file `src/migration-scripts/create-german-search-config.ts` for German: |
|
234
|
|
|
235
|
```ts title="src/migration-scripts/create-german-search-config.ts" |
|
236
|
import { ExecArgs } from "@medusajs/framework/types" |
|
237
|
import { |
|
238
|
ContainerRegistrationKeys, |
|
239
|
} from "@medusajs/framework/utils" |
|
240
|
|
|
241
|
export default async function createGermanSearchConfig({ |
|
242
|
container, |
|
243
|
}: ExecArgs) { |
|
244
|
const knex = container.resolve( |
|
245
|
ContainerRegistrationKeys.PG_CONNECTION |
|
246
|
) |
|
247
|
|
|
248
|
await knex.raw(` |
|
249
|
DO $$ |
|
250
|
BEGIN |
|
251
|
IF NOT EXISTS ( |
|
252
|
SELECT 1 FROM pg_ts_config |
|
253
|
WHERE cfgname = 'medusa_search_german' |
|
254
|
) THEN |
|
255
|
CREATE TEXT SEARCH CONFIGURATION |
|
256
|
medusa_search_german (COPY = german); |
|
257
|
ALTER TEXT SEARCH CONFIGURATION medusa_search_german |
|
258
|
ALTER MAPPING FOR hword, hword_part, word |
|
259
|
WITH unaccent, german_stem; |
|
260
|
END IF; |
|
261
|
END |
|
262
|
$$; |
|
263
|
`) |
|
264
|
} |
|
265
|
``` |
|
266
|
|
|
267
|
Then, run the migrations: |
|
268
|
|
|
269
|
```bash npm2yarn |
|
270
|
npx medusa db:migrate |
|
271
|
``` |
|
272
|
|
|
273
|
`db:migrate` runs migration scripts after it creates the search indexes, and the provider only reads the text search configuration when it writes documents. So the configuration exists by the time your application indexes anything. |