1 # Migrate from Meilisearch to Medusa Search
2
3 You are an AI coding agent. The prompt below migrates a Medusa project from a custom Meilisearch integration to [Medusa Search](../page.mdx), the managed search service of [Cloud](../../page.mdx). Follow it end-to-end in the user's project, then report the manual follow-ups it lists.
4
5 ```md title="Migration Prompt" noLineNumbers
6 <role>
7 You are a senior Medusa developer migrating an existing
8 Medusa project from a custom Meilisearch integration to
9 Medusa Search, the managed search service of Cloud.
10 </role>
11
12 <task>
13 Replace this project's custom Meilisearch integration with
14 the Search Module and Medusa Search, keeping the storefront's
15 search behavior intact.
16 </task>
17
18 <context>
19 - Medusa Search is a provider of the Search Module, which
20 ships with Medusa v2.21.1 and later. Cloud registers and
21 configures it for every environment, so it needs no
22 credentials or configuration in the project.
23 - Medusa declares no index by default, so a project
24 declares its own `product` index definition under
25 `src/search`. A project installed after v2.21.1 ships with
26 one at `src/search/product.ts`, and any other project may
27 or may not have it.
28 - An index definition lives in a file under `src/search`,
29 declares the fields the engine holds, fills the index with
30 a `seed` async generator, and keeps it current with its
31 `events` and `consume` properties.
32 - The `graphSeed` and `graphConsume` helpers build `seed`
33 and `consume` from Query, so an index of an entity Query
34 exposes needs neither written by hand. They take the same
35 options, including a `transform` that maps a page of
36 records to the documents to index. Every document must
37 carry the record's primary key as `id`, and a record you
38 leave out of the returned array stays out of the index.
39 - Medusa's Store API has a `POST /store/search` route that
40 searches any index of the application, so a project
41 doesn't need its own product search route. The body is a
42 search query naming the index in `entity`, or a batch of
43 them under `queries`.
44 - An index is only reachable through `POST /store/search`
45 once a middleware allows it with `configureStoreSearch`
46 from `@medusajs/framework/http`, which takes an
47 `allowed_indexes` object. The route narrows a product
48 index to published products in the publishable API key's
49 sales channels, and any further constraint is that
50 middleware's `filters` option.
51 - Queries run through `query.search` in an API route. Medusa
52 Search has no browser-side querying and no search-only
53 key, so the storefront searches through the Medusa backend
54 rather than the engine.
55 - The `@medusajs/instantsearch-adapter` package is a search
56 client for InstantSearch widgets, so a storefront built on
57 `react-instantsearch` keeps its widgets and swaps the
58 client.
59 - Medusa Search does not support synonyms, custom stop-word
60 lists, geo search, configurable ranking rules, engine-side
61 embedders, or sorting alongside a text query. An index or a
62 query that relies on them fails.
63 - Medusa Search supports typo tolerance and highlighting, but
64 unlike Meilisearch both are opt-in per query through
65 `search_options`. Pass them in the search query to keep the
66 storefront's current behavior.
67 </context>
68
69 <steps>
70 1. Inspect the project and report what it has before you
71 change anything: the Meilisearch module, the sync
72 workflows and their steps, the subscribers, the admin sync
73 route and UI route, the storefront's search client and
74 search modal, and the Medusa version in `package.json`.
75 2. If the Medusa version is below v2.21.1, stop and tell the
76 user to upgrade first.
77 3. List the fields the project indexes in Meilisearch, taken
78 from the `fields` array of `syncProductsWorkflow`, and ask
79 the user for the index settings of their Meilisearch
80 instance. They can read them from
81 `GET /indexes/{index}/settings`.
82 4. Check whether the project already declares a `product`
83 index, which ships at `src/search/product.ts`. If it
84 does and every indexed field and setting is covered by
85 it, skip to step 8 and say why. Otherwise, the project
86 needs an index definition of its own.
87 5. Create the index definition under `src/search`. Map
88 Meilisearch's `searchableAttributes` to `searchable()`
89 fields with a weight, `filterableAttributes` to
90 `facetable()` or `filterable()`, `sortableAttributes` to
91 `sortable()`, and the attributes missing from
92 `displayedAttributes` to `retrievable(false)`. Build
93 `seed` and `consume` with `graphSeed` and `graphConsume`,
94 sharing one options object between them, and reproduce
95 the project's indexing rules in its `transform`, such as
96 leaving a product that isn't published out of the
97 documents it returns. List
98 the entity's create, update, and delete events in
99 `events`.
100 6. Allow every index the storefront searches on
101 `/store/search` with the `configureStoreSearch`
102 middleware in `src/api/middlewares.ts`. An index the
103 middleware doesn't allow answers exactly like one that
104 doesn't exist. The route narrows a product index to
105 published products in the publishable API key's sales
106 channels, so add a `filters` option only for a further
107 constraint.
108 7. Don't create a product search route, since
109 `POST /store/search` serves the storefront. Only write a
110 route with `query.search` for a result the built-in route
111 can't answer with, such as one reshaped for the
112 storefront. In such a route, respond with the hydrated
113 records from `data` and the metadata from `search_result`,
114 and don't also return `search_result.hits`, since each
115 hit's `document` repeats a record already returned.
116 8. Replace any manual sync trigger with a workflow step that
117 calls the Search Module's `reindex` method.
118 9. Delete the Meilisearch module, the sync workflows and
119 steps, the product subscribers, the module's entry in
120 `medusa-config.ts`, and the `meilisearch` dependency.
121 Do this here and not earlier, since the application fails
122 to boot while the module is still registered and the
123 package is gone. Leave the Meilisearch environment
124 variables in place and tell the user to remove them after
125 the cutover.
126 10. Update the storefront to search through Medusa. If it
127 uses InstantSearch widgets, install
128 `@medusajs/instantsearch-adapter`, create its search
129 client with the storefront's JS SDK instance and the
130 path `/store/search`, pass the client and the index
131 name to the existing `InstantSearch` provider, and
132 change every component that reads a hit's `_formatted`
133 fields to read its `highlights` instead. Otherwise, post
134 to `/store/search` with the JS SDK and read the `hits`
135 and `metadata` it returns. Remove the
136 `@meilisearch/instant-meilisearch` package, the
137 Meilisearch `searchClient` export, and the
138 `NEXT_PUBLIC_MEILISEARCH_*` variables. Keep
139 `react-instantsearch`, which the adapter works with.
140 11. Run `npx medusa db:migrate --execute-all-links` to
141 create the index locally, then run the project's type
142 check and tests, and report the result. Never run
143 `medusa db:migrate` without that flag: it asks which
144 link tables to sync, and the prompt is swallowed when
145 the command's output is piped, so it waits forever.
146 </steps>
147
148 <constraints>
149 - Do not add a feature that Medusa Search does not support.
150 When the project relies on one, report it and propose the
151 closest alternative instead of implementing it silently.
152 - Do not delete the user's Meilisearch instance, indexes, or
153 keys, and do not call the Meilisearch API.
154 - Do not expose the search index to the browser. Every query
155 goes through the Medusa backend.
156 - Do not modify `POST /store/search` or recreate it in the
157 project.
158 - Do not refine a widget or a filter on a field the index
159 definition doesn't mark `searchable()`, `filterable()`,
160 `facetable()`, or `sortable()`.
161 - Do not run a deployment or push to any branch.
162 - Never run a Medusa CLI command that can prompt without the
163 flag that skips its prompts. A swallowed prompt reads as a
164 hung command rather than an error, since the command keeps
165 waiting with no output.
166 - Keep every package the current integration imports
167 installed until step 9, so the application boots at every
168 point before it.
169 - Consult the Medusa documentation at
170 https://docs.medusajs.com or the Docs MCP server for any
171 API details you need, including the index definition
172 properties, field types and modifiers, the options of
173 `graphSeed` and `graphConsume`, and the options of
174 `query.search`.
175 </constraints>
176
177 <error_handling>
178 - If the project has no Meilisearch integration, report that
179 and stop.
180 - If the integration differs from the files listed in step
181 1, map each responsibility you find to its Search Module
182 equivalent and report the mapping before you change code.
183 - If you cannot tell whether a field is searched, filtered,
184 faceted, or only displayed, ask the user rather than
185 guessing its modifiers.
186 - If a storefront feature has no Medusa Search equivalent,
187 list it under manual follow-ups instead of removing the
188 feature. The adapter doesn't support `geoSearch`, Insights
189 and Analytics widgets, Query Rules, related-items widgets,
190 autocomplete, or Algolia-style `filters` strings.
191 - If the storefront's search can't be expressed with the
192 built-in route's parameters, ask the user before adding a
193 custom search route for products.
194 </error_handling>
195
196 <output_format>
197 Respond with the following markdown sections:
198
199 ## Changes
200 A table of every file you created, modified, or deleted,
201 with one sentence on what changed in it.
202
203 ## Behavior differences
204 Each Meilisearch behavior the project relied on that Medusa
205 Search does not support, and what you did about it.
206
207 ## Manual follow-ups
208 The steps the user has to take themselves, such as removing
209 environment variables, validating relevance, and deploying.
210 </output_format>
211
212 <success_criteria>
213 - The project builds and type checks with no reference to
214 `meilisearch` or `@meilisearch/instant-meilisearch` left
215 in the backend or the storefront.
216 - Every field the project indexed in Meilisearch is either
217 held by the new index definition, covered by the default
218 product index, or listed as an intentional removal.
219 - Every index the storefront searches is named in an
220 `configureStoreSearch` middleware on `/store/search`.
221 - The storefront searches products through
222 `POST /store/search`, and no Meilisearch key reaches the
223 browser.
224 - Every remaining search route uses `query.search` and
225 returns each record once.
226 - Every unsupported Meilisearch behavior appears under
227 "Behavior differences".
228 </success_criteria>
229 ```
230
231 ---
232
233 ## After the Migration
234
235 The steps below need the user's Cloud account and their judgment, so hand them over rather than running them:
236
237 1. **Deploy to a preview environment.** Push the changes to the branch of a [preview environment](../../environments/preview/page.mdx) rather than production. Cloud runs the search migrations during the deployment, then the Search Module fills the index with the documents that `seed` yields when the application starts.
238 2. **Confirm the index filled.** Check the environment's [logs](../../logs/page.mdx) for `Search indexes migrated`, then send a request to the search route and confirm it returns results. If it returns none, query the `search_index_sync` table to see whether the seed ran and what it failed with.
239 3. **Validate before the cutover.** Compare both engines on the same environment: the top customer queries, every filter and facet, the index's document count against the published products, the sync after creating and deleting a product, and the admin dashboard's global search.
240 4. **Deploy to production.** Merge into the branch of the production environment once the preview environment holds up. Refer to the [Deployments](../../deployments/page.mdx) guide.
241 5. **Remove the Meilisearch credentials.** Delete the `MEILISEARCH_*` and `NEXT_PUBLIC_MEILISEARCH_*` environment variables from the project and the storefront, and remove the Meilisearch account's indexes.
242
243 ---
244
245 ## Reference
246
247 - [Medusa Search vs Meilisearch](../meilisearch/page.mdx): a feature-by-feature comparison, including the Meilisearch features that Medusa Search doesn't offer.
248 - [Search Module](!resources!/infrastructure-modules/search): the module's documentation, including [index definitions](!resources!/infrastructure-modules/search/index-definitions) and [reindexing](!resources!/infrastructure-modules/search/reindexing).
249 - [`query.search`](!docs!/learn/fundamentals/query/search): the API behind `POST /store/search`, for a custom route that reshapes a search result.
250 - [InstantSearch Adapter](!resources!/instantsearch): the storefront search client for InstantSearch widgets.