1 # Migrate from Algolia to Medusa Search
2
3 You are an AI coding agent. The prompt below migrates a Medusa project from a custom Algolia 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 Algolia integration to Medusa
9 Search, the managed search service of Cloud.
10 </role>
11
12 <task>
13 Replace this project's custom Algolia search integration
14 with the Search Module and Medusa Search, keeping the
15 storefront's 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. There
52 is no browser-side querying and no search-only key.
53 - The `@medusajs/instantsearch-adapter` package is a search
54 client for InstantSearch widgets, so a storefront built on
55 `react-instantsearch` keeps its widgets and swaps the
56 client.
57 - Medusa Search does not support synonyms, merchandising
58 rules, geo search, or search analytics. An index or a query
59 that relies on them fails.
60 - Medusa Search supports typo tolerance and highlighting, but
61 both are opt-in per query through `search_options` and both
62 require a text query.
63 </context>
64
65 <steps>
66 1. Inspect the project and report what it has before you
67 change anything: the Algolia module, the sync workflows
68 and their steps, the subscribers, the admin sync route
69 and UI route, the search API route, the storefront search
70 client, and the Medusa version in `package.json`.
71 2. If the Medusa version is below v2.21.1, stop and tell the
72 user to upgrade first.
73 3. List the fields the project indexes in Algolia, taken
74 from the sync workflow's `fields` array, and ask the user
75 for the index settings from their Algolia dashboard.
76 4. Check whether the project already declares a `product`
77 index, which ships at `src/search/product.ts`. If it
78 does and every indexed field and setting is covered by
79 it, skip to step 6 and say why. Otherwise, the project
80 needs an index definition of its own.
81 5. Create the index definition under `src/search`. Map
82 Algolia's searchable attributes to `searchable()` fields
83 with a weight, faceting attributes to `facetable()` or
84 `filterable()`, sorting replicas to `sortable()`, and
85 unretrievable attributes to `retrievable(false)`. Build
86 `seed` and `consume` with `graphSeed` and `graphConsume`,
87 sharing one options object between them, and reproduce
88 the project's indexing rules in its `transform`, such as
89 leaving a product that isn't published out of the
90 documents it returns. List
91 the entity's create, update, and delete events in
92 `events`.
93 6. Allow every index the storefront searches on
94 `/store/search` with the `configureStoreSearch`
95 middleware in `src/api/middlewares.ts`. An index the
96 middleware doesn't allow answers exactly like one that
97 doesn't exist. The route narrows a product index to
98 published products in the publishable API key's sales
99 channels, so add a `filters` option only for a further
100 constraint.
101 7. Delete the project's product search API route and its
102 validation middleware, since `POST /store/search`
103 replaces it. Only write a route with `query.search` for a
104 result the built-in route can't answer with, such as one
105 reshaped for the storefront. In such a route, respond
106 with the hydrated records from `data` and the metadata
107 from `search_result`, and don't also return
108 `search_result.hits`, since each hit's `document` repeats
109 a record already returned.
110 8. Replace any manual sync trigger with a workflow step that
111 calls the Search Module's `reindex` method.
112 9. Delete the Algolia module, the sync workflows and steps,
113 the product subscribers, the module's entry in
114 `medusa-config.ts`, and the `algoliasearch` dependency.
115 Do this here and not earlier, since the application fails
116 to boot while the module is still registered and the
117 package is gone. Leave the Algolia environment variables
118 in place and tell the user to remove them after the
119 cutover.
120 10. Update the storefront to search through Medusa. If it
121 uses InstantSearch widgets, install
122 `@medusajs/instantsearch-adapter`, create its search
123 client with the storefront's JS SDK instance and the
124 path `/store/search`, pass the client and the index
125 name to the existing `InstantSearch` provider, and
126 change every component that reads a hit's `objectID` to
127 read `id`. Otherwise, post to `/store/search` with the
128 JS SDK and read the `hits` and `metadata` it returns. Remove the Algolia packages, the Algolia search
129 client, and the Algolia environment variables from the
130 storefront.
131 11. Run `npx medusa db:migrate --execute-all-links` to
132 create the index locally, then run the project's type
133 check and tests, and report the result. Never run
134 `medusa db:migrate` without that flag: it asks which
135 link tables to sync, and the prompt is swallowed when
136 the command's output is piped, so it waits forever.
137 </steps>
138
139 <constraints>
140 - Do not add a feature that Medusa Search does not support.
141 When the project relies on one, report it and propose the
142 closest alternative instead of implementing it silently.
143 - Do not delete the user's Algolia account, indexes, or
144 credentials, and do not call the Algolia API.
145 - Do not modify `POST /store/search` or recreate it in the
146 project.
147 - Do not refine a widget or a filter on a field the index
148 definition doesn't mark `searchable()`, `filterable()`,
149 `facetable()`, or `sortable()`.
150 - Do not run a deployment or push to any branch.
151 - Never run a Medusa CLI command that can prompt without the
152 flag that skips its prompts. A swallowed prompt reads as a
153 hung command rather than an error, since the command keeps
154 waiting with no output.
155 - Keep every package the current integration imports
156 installed until step 9, so the application boots at every
157 point before it.
158 - Consult the Medusa documentation at
159 https://docs.medusajs.com or the Docs MCP server for any
160 API details you need, including the index definition
161 properties, field types and modifiers, the options of
162 `graphSeed` and `graphConsume`, and the options of
163 `query.search`.
164 </constraints>
165
166 <error_handling>
167 - If the project has no Algolia integration, report that and
168 stop.
169 - If the integration differs from the files listed in step
170 1, map each responsibility you find to its Search Module
171 equivalent and report the mapping before you change code.
172 - If you cannot tell whether a field is searched, filtered,
173 faceted, or only displayed, ask the user rather than
174 guessing its modifiers.
175 - If a storefront feature has no Medusa Search equivalent,
176 list it under manual follow-ups instead of removing the
177 feature. The adapter doesn't support `geoSearch`, Insights
178 and Analytics widgets, Query Rules, related-items widgets,
179 autocomplete, or Algolia `filters` strings.
180 - If the storefront's search can't be expressed with the
181 built-in route's parameters, ask the user before adding a
182 custom search route for products.
183 </error_handling>
184
185 <output_format>
186 Respond with the following markdown sections:
187
188 ## Changes
189 A table of every file you created, modified, or deleted,
190 with one sentence on what changed in it.
191
192 ## Behavior differences
193 Each Algolia behavior the project relied on that Medusa
194 Search does not support, and what you did about it.
195
196 ## Manual follow-ups
197 The steps the user has to take themselves, such as removing
198 environment variables, validating relevance, and deploying.
199 </output_format>
200
201 <success_criteria>
202 - The project builds and type checks with no reference to
203 `algoliasearch` left in the backend or the storefront.
204 - Every field the project indexed in Algolia is either held
205 by the new index definition, covered by the default
206 product index, or listed as an intentional removal.
207 - Every index the storefront searches is named in an
208 `configureStoreSearch` middleware on `/store/search`.
209 - The storefront searches products through
210 `POST /store/search` and renders the hits it returns.
211 - Every remaining search route uses `query.search` and
212 returns each record once.
213 - Every unsupported Algolia behavior appears under
214 "Behavior differences".
215 </success_criteria>
216 ```
217
218 ---
219
220 ## After the Migration
221
222 The steps below need the user's Cloud account and their judgment, so hand them over rather than running them:
223
224 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.
225 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.
226 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.
227 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.
228 5. **Remove the Algolia credentials.** Delete the `ALGOLIA_*` and `NEXT_PUBLIC_ALGOLIA_*` environment variables from the project and the storefront, and remove the Algolia account's indexes.
229
230 ---
231
232 ## Reference
233
234 - [Medusa Search vs Algolia](../algolia/page.mdx): a feature-by-feature comparison, including the Algolia features that Medusa Search doesn't offer.
235 - [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).
236 - [`query.search`](!docs!/learn/fundamentals/query/search): the API behind `POST /store/search`, for a custom route that reshapes a search result.
237 - [InstantSearch Adapter](!resources!/instantsearch): the storefront search client for InstantSearch widgets.