# Add Full-Text Search to a Prisma App

Yes. An agent enables Prisma's Postgres full-text search, adds a search procedure with a safe query, a GIN index migration, tests and a search box, then opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-full-text-search/prisma/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the schema and the one router before it chooses an approach. In an app laid out like trpc/examples-next-prisma-starter, `prisma/schema.prisma` has a `postgres` datasource, a `prisma-client-js` generator with no `previewFeatures`, and one model, `Post`, with `id`, `title`, `text`, `createdAt` and `updatedAt`. `src/server/routers/post.ts` exposes `list` (cursor pagination over `createdAt`), `byId` and `add` (a title of 1 to 32 characters and a non-empty `text`), all selecting through `defaultPostSelect`. `package.json` pins `@prisma/client ^6.7.0` and `prisma ^6.7.0`, runs migrations with `prisma migrate dev` and `prisma migrate deploy`, and `prebuild` runs `generate` then `migrate`, which is also what `.github/workflows/main.yml` runs before `pnpm test-unit` and the Playwright job, each against a `postgres` service. `src/server/routers/post.test.ts` is the whole unit suite: it calls `createCaller` with `createContextInner({})` and asserts `add` then `byId` round-trips.

Prisma 6 names the Postgres feature `fullTextSearchPostgres`; the upgrade guide says the old `fullTextSearch` flag became general availability for MySQL only and that Postgres users must rename the flag. The agent adds `previewFeatures = ["fullTextSearchPostgres"]` to the generator, runs `pnpm generate`, and writes `post.search` with a zod input of `q` (trimmed, 1 to 100 characters) and an optional `limit`, querying `where: { OR: [{ title: { search } }, { text: { search } }] }` and ordering by `_relevance` over both fields with the same search string, `sort: 'desc'`, then `createdAt`. Prisma hands the string to Postgres's `to_tsquery`, which throws on stray operators, so the procedure builds `search` itself: split the input on whitespace, strip anything that is not a letter or digit, drop empties and join with ` & `. An input that leaves no terms returns an empty list rather than a database error.

A sequential scan over `to_tsvector` is fine at seed size and not at any other. The agent runs `pnpm prisma migrate dev --create-only --name post_search_index` and edits the generated SQL to create a GIN index on the `to_tsvector('english', ...)` expression. Which expression depends on the SQL Prisma emits: `src/server/prisma.ts` logs every query when `NODE_ENV` is `development`, so the agent reads the generated statement and makes the index expression match it, or, when Prisma's form cannot use an expression index, switches the procedure to `prisma.$queryRaw` with `websearch_to_tsquery` and a matching index, and says in the pull request which of the two it did and why. `prisma/migrations/migration_lock.toml` already records `postgresql`, and `prebuild` applies the new migration in CI.

Tests and UI follow what exists. `post.test.ts` gains a case that adds two posts with distinct words and asserts `search` finds the right one, ranks a title match above a body match, and returns nothing for a query made only of punctuation. `src/pages/index.tsx` gets a search input above Latest Posts that calls `trpc.post.search.useQuery({ q }, { enabled: q.length > 0 })` and renders the same article markup, and `playwright/smoke.test.ts` gains a step that adds a post with a nonce and finds it through the box. The agent runs `pnpm lint`, `pnpm typecheck`, `pnpm test-unit` and `pnpm test-e2e` against a Postgres in its sandbox, as the workflow does.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add full-text search over posts to this tRPC, Next.js and Prisma app on Postgres. Read package.json, prisma/schema.prisma, prisma/migrations, prisma/seed.ts, src/server/prisma.ts, src/server/routers/post.ts, src/server/routers/post.test.ts, src/server/context.ts, src/pages/index.tsx, playwright/smoke.test.ts and .github/workflows/main.yml first. Enable the fullTextSearchPostgres preview feature and regenerate the client. Add a post.search procedure with a zod input of q and an optional limit that builds a safe tsquery from the words of q, filters title or text with search, orders by _relevance then createdAt, and returns the default select. Create a migration with --create-only that adds a GIN index on the to_tsvector expression Prisma's generated SQL uses; if Prisma's SQL cannot use such an index, use $queryRaw with websearch_to_tsquery instead and explain which you chose. Add unit tests for a hit, ranking and a punctuation-only query, a search box on the index page, and a Playwright step that finds a new post through it. Run lint, typecheck, unit and e2e tests against a local Postgres, then open a pull request that quotes the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `prisma/schema.prisma`, `src/server/routers/post.ts`, `src/server/routers/post.test.ts`, `src/pages/index.tsx` and `playwright/smoke.test.ts`, and adds one migration directory under `prisma/migrations` with the index. Its description quotes the Vitest and Playwright output, shows the SQL Prisma emitted for the search so a reviewer can see the index expression matches, and pastes `EXPLAIN` output from the sandbox for the search statement showing the index in use.

A reviewer checks the text search configuration (`english` is the agent's default; a multilingual corpus wants `simple`), that the term sanitiser does not drop characters their content relies on, and that the index expression is exactly the one the query uses, since a mismatch silently falls back to a scan. The preview flag is the other point to weigh: it is a Prisma preview feature, and the pull request says so.

`list`, `byId` and `add` are unchanged, and the seed still upserts the first post. A deployment that runs `prisma migrate deploy` builds the index once; on a large table that is the one slow step, and the migration says so in a comment.

## Run it on every Linear issue labelled search

An automation on the Project with a `linear` trigger on `linear.issue.labeled`, `labels` set to `search` and the state in `plan` mode starts a read-only run that reads the issue and the schema and posts a plan as a comment on the issue: which model and fields, whether Prisma's `search` filter or raw SQL fits, and what the migration would add. The trigger puts the issue after the prompt and fills in no placeholder.

Once the plan is approved, a handler on your own server sends this body to `POST /o/{org}/sessions` with the issue's URL as a second `resources` entry, read through your Linear connection, and a `request_id` that is a UUID v5 of the issue id, so a re-labelled issue returns the run it already started. That run can push, and the key's `webhook_url` gets `run.completed` with the pull request's URL.

## Why run it on Rush?

Full-text search is easy to get subtly wrong: the query works in tests on five rows and never touches the index in production because the expression differs by a cast. The agent runs Prisma with query logging in its own sandbox, reads the SQL it actually emits, builds the index to match and keeps the `EXPLAIN` output with the pull request, so the reviewer sees the plan rather than a promise.

Several services on the same Prisma version can take the same change, each with its own model and fields. One run per repository reads that schema and router, writes the procedure in that app's conventions and opens one pull request each, and the preview flag, sanitiser and index shape come out the same in all of them.

## Frequently asked questions

### Why a preview feature instead of raw SQL from the start?

The `search` filter and `_relevance` ordering keep the procedure typed and inside the existing `defaultPostSelect`, which is what the rest of the router uses. The agent falls back to `$queryRaw` with `websearch_to_tsquery` only when Prisma's generated SQL cannot use the index, and says which path it took.

### What does the sanitiser do to the user's query?

It keeps letters and digits in each whitespace-separated word, drops empty words and joins the rest with `&`, so every term must match. Postgres's `to_tsquery` would otherwise raise a syntax error on input such as a lone `&` or an unbalanced quote.

### Does the index slow down writes?

A GIN index costs something on every insert and update of `title` or `text`. For a posts table that is read far more than written it is the right trade; the pull request names the index so it can be dropped with one migration if that changes.

### Which language configuration does it use?

`english`, in both the query and the index expression, since the two must match for the index to be used. A reviewer with content in several languages can switch both to `simple`, which does no stemming.

### Will CI pick up the migration?

Yes. Both workflow jobs run `pnpm prebuild`, which runs `prisma generate` and `prisma migrate deploy` against the job's `postgres` service, so the index exists before the unit and Playwright tests run.

## More Prisma jobs

- [Add a Prisma Migration Drift Check to CI](https://getrush.ai/run/add-migration-drift-check/prisma/)
- [Add Optimistic Locking to Prisma Models](https://getrush.ai/run/add-optimistic-locking/prisma/)
- [Add Pagination to a Prisma Query](https://getrush.ai/run/add-pagination/prisma/)

## The same job on other stacks

- [Add Full-Text Search to a SQLModel and FastAPI App](https://getrush.ai/run/add-full-text-search/sqlmodel/)
- [Add Full-Text Search to a Supabase App](https://getrush.ai/run/add-full-text-search/supabase/)
- [Add Full-Text Search to an Axum API](https://getrush.ai/run/add-full-text-search/axum/)

## Keep reading

- [What an agent can do in Prisma itself, once you connect it](https://getrush.ai/integrations/prisma/)
- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
