# Add Full-Text Search to an Axum API

Yes. An agent adds a Postgres tsvector column and GIN index by SQLx migration, a `q` filter on the Axum article list, a test, and opens a pull request.

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

## What the agent does

The agent starts with the schema and the query it has to extend, because in a SQLx project the two are checked against each other at compile time. In an API shaped like launchbadge/realworld-axum-sqlx, `Cargo.toml` pins `axum 0.3.4`, `sqlx 0.5` with the `postgres`, `uuid` and `time` features, and `tower-http 0.2.0`. `migrations/1_setup.sql` enables `uuid-ossp`, defines `trigger_updated_at()` and a `case_insensitive` collation; `migrations/4_article.sql` creates `article` with `slug`, `title`, `description`, `body` and a `tag_list text[]` column that already has a GIN index, `article_tags_gin`. The list endpoint lives in `src/http/articles/listing.rs`: `ListArticlesQuery` takes `tag`, `author`, `favorited`, `limit` and `offset`, and `list_articles` runs one `sqlx::query_as!` with a `where` clause of `$n is null or …` branches, ordered by `article.created_at desc`.

It adds `migrations/5_article_search.sql`, numbered after the existing four so `sqlx migrate run` applies it last. The migration adds a stored generated column, `search tsvector generated always as (setweight(to_tsvector('english', title), 'A') || setweight(to_tsvector('english', description), 'B') || setweight(to_tsvector('english', body), 'C')) stored`, and a GIN index on it. A generated column keeps the vector current on every insert and update without a trigger, which matters here because `trigger_updated_at` already fires on the table and the agent does not want two triggers racing on the same row.

In `listing.rs` it adds `q: Option<String>` to `ListArticlesQuery` and one more branch to the existing `where`: `$7::text is null or search @@ websearch_to_tsquery('english', $7)`. `websearch_to_tsquery` accepts what a person types, quoted phrases and a leading minus included, and never raises a syntax error the way `to_tsquery` can. The ordering stays `created_at desc` when `q` is absent; with a term present the agent orders by `ts_rank(search, websearch_to_tsquery('english', $7)) desc` first, inside the same query so the `query_as!` macro still sees one statement. Because `sqlx 0.5` has no offline metadata file in this repository, the agent starts Postgres in the sandbox, exports `DATABASE_URL` as `.env.sample` shows, runs `sqlx db setup`, and only then does `cargo build` succeed with the new column known to the macro.

The repository has no unit tests of its own; `.github/workflows/rust.yml` runs `cargo build --verbose` and `cargo test --verbose` against a Postgres service, then starts the server and runs the Realworld Postman collection with `./run-api-tests.sh`. The agent adds `tests/search.rs`, an integration test that connects with `DATABASE_URL`, inserts two articles through the `users` and `articles` routes, calls `GET /api/articles?q=…` and asserts that only the matching slug comes back. It runs `cargo fmt --check`, `cargo build`, `cargo test` and a few `curl` calls against the running server in the sandbox, and pastes the responses in the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add full-text search to the article list of this Axum API and open a pull request. Read Cargo.toml, migrations/1_setup.sql, migrations/4_article.sql, src/http/articles/listing.rs, src/http/articles/mod.rs, src/http/mod.rs, .env.sample and .github/workflows/rust.yml first. Add migrations/5_article_search.sql with a stored generated tsvector column named search over title (weight A), description (weight B) and body (weight C), and a GIN index on it. Add an optional q query parameter to ListArticlesQuery in listing.rs and extend the existing where clause with a websearch_to_tsquery match; order by ts_rank when q is present and keep created_at desc otherwise, inside the same sqlx::query_as! statement. Start Postgres in the sandbox, set DATABASE_URL, run sqlx db setup, then cargo fmt --check, cargo build and cargo test. Add tests/search.rs that inserts two articles through the API and asserts GET /api/articles?q= returns only the matching one. Start the server, curl the endpoint with and without q, and paste the responses in the pull request. Do not change the Realworld response shape, the JWT extractor or the existing migrations.",
  "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

What comes back is a pull request with one new migration, `migrations/5_article_search.sql`, a `q` field on `ListArticlesQuery` and a longer `where` clause in `src/http/articles/listing.rs`, and a new `tests/search.rs`. The description shows the `curl` output for `GET /api/articles?q=rust` against the sandbox server, the `cargo build` line proving the macro accepted the new column, and the `cargo test` summary.

The repository's workflow applies the migrations with `sqlx db setup`, builds, runs `cargo test` and the Postman collection on the pull request, so a migration that fails to apply or a query the macro rejects shows up there. The Postman collection never sends `q`, so the new integration test and the pasted `curl` output are the evidence for the search path.

A reviewer checks that the generated column uses the `english` configuration their content actually needs, that the GIN index is on `search` and not on the three text columns, that `websearch_to_tsquery` rather than `to_tsquery` handles user input, and that the response shape is unchanged so the frontend keeps working. Then they run the migration on staging and watch the index build on a real-sized table.

## Run it on every new list endpoint

A team adding search to several SQLx-backed services sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own migrations and `Cargo.toml`, so a service on `sqlx 0.7` with an offline `.sqlx` directory gets `cargo sqlx prepare` run as well, and each opens its own pull request on its own branch.

Send a `request_id` with each body so a retried request returns the run it already started instead of a second one. With a `webhook_url` on the API key, Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from.

## Why run it on Rush?

SQLx checks every query against a live database at compile time, so adding a column is not an edit you can make blind: the migration must apply, the database must be up and `DATABASE_URL` must point at it before `cargo build` passes. In a Rush sandbox the agent runs Postgres, `sqlx db setup` and `cargo build` itself and keeps every command and its output in the run's record, so a reviewer sees the macro accept the new `search` column rather than trusting a diff.

The proof is a response. The agent inserts articles through the real `POST /api/articles` route, queries `/api/articles?q=…` against the real server, and pastes what came back in the pull request. The same request body works unchanged on any Axum service a team runs, and runs for several repositories proceed at once, each in its own sandbox.

## Frequently asked questions

### Why a generated column instead of a trigger?

The `article` table already has an `updated_at` trigger from `trigger_updated_at('article')` in `4_article.sql`. A stored generated column recomputes the tsvector on every write with no second trigger and no ordering question between them, and it is one line in a migration.

### Will the build still work in CI?

Yes. `.github/workflows/rust.yml` already runs `cargo install sqlx-cli` and `sqlx db setup` against a Postgres service before `cargo build`, so the new migration applies there and the `query_as!` macro sees the `search` column. Nothing about the workflow changes.

### Does it change the response shape?

No. `MultipleArticlesBody` still returns `articles` and `articlesCount`, and the `tag`, `author`, `favorited`, `limit` and `offset` filters keep working. `q` is one more optional query parameter; a request without it behaves as before.

### What about search in languages other than English?

The migration uses the `english` text search configuration, which stems English words. For another language the agent changes the configuration name in the generated column and the two `websearch_to_tsquery` calls; the pull request notes where, and a mixed-language site would use `simple` instead.

## More Axum jobs

- [Add Graceful Shutdown to an Axum API](https://getrush.ai/run/add-graceful-shutdown/axum/)
- [Add Health Checks to an Axum Service](https://getrush.ai/run/add-health-checks/axum/)
- [Add Input Validation to an Axum API](https://getrush.ai/run/add-input-validation/axum/)

## The same job on other stacks

- [Add Full-Text Search to a Drizzle and PostgreSQL App](https://getrush.ai/run/add-full-text-search/drizzle/)
- [Add Full-Text Search to a Mongoose API](https://getrush.ai/run/add-full-text-search/mongoose/)
- [Add Full-Text Search to a Prisma App](https://getrush.ai/run/add-full-text-search/prisma/)

## Keep reading

- [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/)
