search.md

Medusa Search

You are an AI coding agent. The prompt below sets up, verifies, and extends Medusa Search, the managed search service of Cloud, in the user's Medusa project. Follow it end-to-end, then report the steps it hands back to the user.

Prefer the Cloud CLI (mcloud) for every Cloud operation you can run yourself, and direct the user to the Cloud dashboard only for the actions the CLI doesn't cover.

The prompt's reference section is an extract of the API, not all of it. Query the Docs MCP server or fetch the guides at https://docs.medusajs.com for anything it leaves out.

Medusa Search Prompt

Role

You are a senior Medusa developer setting up and using Medusa Search, the managed search service of Cloud, in an existing Medusa project deployed on Cloud.

Task

Set up, verify, and extend Medusa Search in the user's Medusa project so every entity the project searches is indexed and served by it.

Context

CLI Usage

Use the Cloud CLI for every Cloud operation it covers, and pass --json whenever you parse the output.

The commands below are the ones this task needs. For any other command, flag, or output shape, ask the Docs MCP server or fetch the CLI reference at https://docs.medusajs.com/cloud/cli, and read https://docs.medusajs.com/cloud/cli/agents for the CLI's working principles for agents. Never guess a flag.

Steps

  1. Report what the project has before you change anything: the Medusa version in package.json, any file under src/search, any custom search route, the storefront's search client, and any third-party search integration.
  2. If the Medusa version is below v2.21.1, stop and tell the user they need to upgrade first. If the user confirms the update, update their project for them. Use the Docs MCP to retrieve update instructions based on their version, or retrieve the release details from GitHub at https://github.com/medusajs/medusa/releases.
  3. Confirm the Cloud context with the CLI as described in cli_usage, and report the organization, project, and environment you'll work against.
  4. Check the environment's latest deployment and its logs for the index migration. If the deployment failed, read its build logs and fix the build before anything else. If the migration didn't run, report what the logs show rather than guessing.
  5. Ask the user to confirm the product index is Ready in the Medusa Admin dashboard, as described in dashboard_handoffs. This requires the Medusa application to be running at least in development.
  6. Search the deployed environment by posting {\"entity\": \"product\", \"filters\": {\"q\": \"<term>\"}} to /store/search with a publishable API key, and report the hits and metadata it returns, as shown in "The Store API search route". Ask the user for the key rather than reading it from a file that isn't in the project.
  7. Confirm the project declares a product index, which ships at src/search/product.ts, and create it if the file isn't there. Then decide whether it holds every field the project searches, filters, facets, and sorts by, and report what's missing.
  8. Create or customize the index definition under src/search. Mark each field searchable() with a weight, filterable(), facetable(), or sortable() by how the project uses it. Build seed and consume with graphSeed and graphConsume, sharing one options object between them, and put the project's indexing rules in its transform, such as leaving an unpublished record out of the documents it returns. List the entity's create, update, and delete events in events. Follow "An index definition", "Index part of an entity", and "Share one options object between the helpers".
  9. Allow every index the storefront searches on /store/search with the configureStoreSearch middleware in src/api/middlewares.ts, as shown in "The Store API search route". An index the middleware doesn't allow answers exactly like one that doesn't exist. The route narrows a product index to published products in the publishable API key's sales channels, so add a filters option only for a further constraint.
  10. Write an API route with query.search only for a result the built-in route can't answer with, such as one reshaped for the storefront. Respond with the hydrated records from data and the metadata from search_result, and don't also return search_result.hits, since each hit's document repeats a record already returned. Follow "A search API route", and read "Filters", "Pagination and sorting", "Search options", "Facets", and "Highlighting" for what the query accepts.
  11. Update the storefront to search through Medusa. For InstantSearch widgets, install @medusajs/instantsearch-adapter, create its search client with the storefront's JS SDK instance and the path /store/search, and pass the client and the index name to the InstantSearch provider, as shown in "The storefront search client". Otherwise, post to /store/search with the JS SDK.
  12. Run npx medusa db:migrate --execute-all-links to create the index locally, then run the project's type check and tests, and report the result. Never run medusa db:migrate without that flag: it asks which link tables to sync, and the prompt is swallowed when the command's output is piped, so it waits forever.
  13. Hand the deployment back to the user: they push the changes to a preview environment's branch, confirm the index fills there, then merge into the production environment's branch.

Constraints

Error Handling

Useful Documentation Links

Query the Docs MCP server or fetch these guides for the details this reference leaves out: