|
1
|
import { CardList, Table, Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Search Module Providers`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you'll learn about Search Module Providers in Medusa, including how to register them and choose which one holds each index. |
|
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 title="Easier setup with Cloud"> |
|
21
|
|
|
22
|
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. |
|
23
|
|
|
24
|
</Note> |
|
25
|
|
|
26
|
## What is a Search Module Provider? |
|
27
|
|
|
28
|
A Search Module Provider implements the logic of talking to a search engine, including creating indexes, writing documents, and running queries. The [Search Module](../page.mdx) then uses the registered providers to serve every index. |
|
29
|
|
|
30
|
A provider holds the integration logic only. It never decides when a document changes or when an index is filled, as the Search Module drives all of that and calls the provider to carry it out. |
|
31
|
|
|
32
|
<Table> |
|
33
|
<Table.Header> |
|
34
|
<Table.Row> |
|
35
|
<Table.HeaderCell> |
|
36
|
The provider is responsible for |
|
37
|
</Table.HeaderCell> |
|
38
|
<Table.HeaderCell> |
|
39
|
The Search Module is responsible for |
|
40
|
</Table.HeaderCell> |
|
41
|
</Table.Row> |
|
42
|
</Table.Header> |
|
43
|
<Table.Body> |
|
44
|
<Table.Row> |
|
45
|
<Table.Cell> |
|
46
|
Creating and migrating the physical indexes in the search engine. |
|
47
|
</Table.Cell> |
|
48
|
<Table.Cell> |
|
49
|
Reading your [index definitions](../index-definitions/page.mdx) and deciding which indexes exist and when they change. |
|
50
|
</Table.Cell> |
|
51
|
</Table.Row> |
|
52
|
<Table.Row> |
|
53
|
<Table.Cell> |
|
54
|
Writing and deleting the documents the module hands it. |
|
55
|
</Table.Cell> |
|
56
|
<Table.Cell> |
|
57
|
[Filling an index](../reindexing/page.mdx) by running the definition's `seed` function, then batching the results into provider writes. |
|
58
|
</Table.Cell> |
|
59
|
</Table.Row> |
|
60
|
<Table.Row> |
|
61
|
<Table.Cell> |
|
62
|
Translating a search query into the engine's own query language. |
|
63
|
</Table.Cell> |
|
64
|
<Table.Cell> |
|
65
|
Subscribing to events and routing each one to the definitions that declared it, so indexes stay current. |
|
66
|
</Table.Cell> |
|
67
|
</Table.Row> |
|
68
|
<Table.Row> |
|
69
|
<Table.Cell> |
|
70
|
Reporting whether a write finished, which lets the module wait on engines that apply writes asynchronously. |
|
71
|
</Table.Cell> |
|
72
|
<Table.Cell> |
|
73
|
Versioning an index, rebuilding it when its definition changes, and running the [catch-up pass](../reindexing/page.mdx#the-catch-up-pass) that follows a full run. |
|
74
|
</Table.Cell> |
|
75
|
</Table.Row> |
|
76
|
</Table.Body> |
|
77
|
</Table> |
|
78
|
|
|
79
|
### Available Search Module Providers |
|
80
|
|
|
81
|
Medusa provides the following Search Module Providers: |
|
82
|
|
|
83
|
<CardList |
|
84
|
items={[ |
|
85
|
{ |
|
86
|
title: "PostgreSQL", |
|
87
|
href: "/infrastructure-modules/search/providers/postgres", |
|
88
|
}, |
|
89
|
{ |
|
90
|
title: "Medusa Search", |
|
91
|
href: "!cloud!/search", |
|
92
|
badge: { |
|
93
|
variant: "blue", |
|
94
|
children: "Cloud" |
|
95
|
} |
|
96
|
}, |
|
97
|
]} |
|
98
|
/> |
|
99
|
|
|
100
|
<Note> |
|
101
|
|
|
102
|
Refer to the [Compare Search Providers guide](https://docs.medusajs.com/cloud/search/comparison) for a detailed comparison of the features and capabilities of each provider. |
|
103
|
|
|
104
|
</Note> |
|
105
|
|
|
106
|
### Custom Search Module Providers |
|
107
|
|
|
108
|
You can also create a custom provider to integrate a search engine that Medusa doesn't support out of the box. You only implement the methods that talk to that engine, as shown in the table above. Refer to the [Create Search Module Provider guide](/references/search/provider) for the methods to implement. |
|
109
|
|
|
110
|
--- |
|
111
|
|
|
112
|
## Register Multiple Providers |
|
113
|
|
|
114
|
The Search Module supports more than one provider at a time. Each [index definition](../index-definitions/page.mdx) picks one through its `provider` property, so a high-traffic index can live on a dedicated engine while quieter indexes stay on PostgreSQL. |
|
115
|
|
|
116
|
For example: |
|
117
|
|
|
118
|
```ts title="medusa-config.ts" |
|
119
|
module.exports = defineConfig({ |
|
120
|
// ... |
|
121
|
modules: [ |
|
122
|
{ |
|
123
|
resolve: "@medusajs/medusa/search", |
|
124
|
options: { |
|
125
|
default_provider: "search-postgres", |
|
126
|
providers: [ |
|
127
|
{ |
|
128
|
resolve: "@medusajs/medusa/search-postgres", |
|
129
|
id: "search-postgres", |
|
130
|
options: { |
|
131
|
language: "english", |
|
132
|
}, |
|
133
|
}, |
|
134
|
{ |
|
135
|
resolve: "./src/modules/my-search-provider", |
|
136
|
id: "my-engine", |
|
137
|
options: { |
|
138
|
// provider options... |
|
139
|
}, |
|
140
|
}, |
|
141
|
], |
|
142
|
}, |
|
143
|
}, |
|
144
|
], |
|
145
|
}) |
|
146
|
``` |
|
147
|
|
|
148
|
<Note> |
|
149
|
|
|
150
|
Register each provider once. Two registrations of the same provider share one identifier, so an index definition couldn't tell them apart, and the module throws at startup. |
|
151
|
|
|
152
|
</Note> |
|
153
|
|
|
154
|
### Default Search Module Provider |
|
155
|
|
|
156
|
An index definition that names no provider uses the module's default provider. The Search Module resolves the default once when it initializes, so a misconfiguration fails at startup rather than on the first query. |
|
157
|
|
|
158
|
<Table> |
|
159
|
<Table.Header> |
|
160
|
<Table.Row> |
|
161
|
<Table.HeaderCell> |
|
162
|
Scenario |
|
163
|
</Table.HeaderCell> |
|
164
|
<Table.HeaderCell> |
|
165
|
Default Provider |
|
166
|
</Table.HeaderCell> |
|
167
|
</Table.Row> |
|
168
|
</Table.Header> |
|
169
|
<Table.Body> |
|
170
|
<Table.Row> |
|
171
|
<Table.Cell> |
|
172
|
One provider is registered. |
|
173
|
</Table.Cell> |
|
174
|
<Table.Cell> |
|
175
|
The registered provider. |
|
176
|
</Table.Cell> |
|
177
|
</Table.Row> |
|
178
|
<Table.Row> |
|
179
|
<Table.Cell> |
|
180
|
Multiple providers are registered and the module has a `default_provider` option. |
|
181
|
</Table.Cell> |
|
182
|
<Table.Cell> |
|
183
|
The provider whose identifier matches `default_provider`. |
|
184
|
</Table.Cell> |
|
185
|
</Table.Row> |
|
186
|
<Table.Row> |
|
187
|
<Table.Cell> |
|
188
|
Multiple providers are registered and the module doesn't have a `default_provider` option. |
|
189
|
</Table.Cell> |
|
190
|
<Table.Cell> |
|
191
|
The module throws at startup because it can't determine a default provider. |
|
192
|
</Table.Cell> |
|
193
|
</Table.Row> |
|
194
|
<Table.Row> |
|
195
|
<Table.Cell> |
|
196
|
Multiple providers are registered and the module has a `default_provider` option that doesn't match any registered provider. |
|
197
|
</Table.Cell> |
|
198
|
<Table.Cell> |
|
199
|
The module throws at startup because it can't determine a default provider. |
|
200
|
</Table.Cell> |
|
201
|
</Table.Row> |
|
202
|
</Table.Body> |
|
203
|
</Table> |
|
204
|
|
|
205
|
### Select a Provider by its Identifier |
|
206
|
|
|
207
|
Every provider declares its own identifier, such as `search-postgres` for the PostgreSQL provider and `search-medusa` for the Medusa Search provider. A custom provider declares one too, as a static `identifier` property on the service it exports: |
|
208
|
|
|
209
|
```ts title="src/modules/my-search-provider/service.ts" |
|
210
|
import { |
|
211
|
AbstractSearchProviderService, |
|
212
|
} from "@medusajs/framework/utils" |
|
213
|
|
|
214
|
export class MySearchService |
|
215
|
extends AbstractSearchProviderService |
|
216
|
{ |
|
217
|
static identifier = "my-engine" |
|
218
|
// ... |
|
219
|
} |
|
220
|
``` |
|
221
|
|
|
222
|
An index definition then selects a provider by that identifier: |
|
223
|
|
|
224
|
```ts title="src/search/brand.ts" |
|
225
|
export const brandIndex = defineSearchIndex({ |
|
226
|
provider: "my-engine", |
|
227
|
// ... |
|
228
|
}) |
|
229
|
``` |