AI.RANK reached general availability in BigQuery on November 17, 2026. What it does is more useful than most functions this new usually are. AI.RANK takes a table (or the result of a query) and a natural-language description of what you’re looking for, and reorders the rows by relevance to that description. It’s the same underlying idea as AI.AGG and AI.SCORE applied to a different problem: instead of summarising text or scoring it, this one sorts a whole table by “which of these is actually what I asked for.”
We’re reusing the customer_note column from bq_ai_demo.ecommerce_orders again for the main example, specifically the Bluetooth pairing complaints we planted in the Electronics notes when we added the column for the AI.AGG article. If you haven’t set up the shared demo dataset, start with the setup guide.
Prerequisites
AI.RANK isn’t a self-contained model call like most of the functions in this series. It’s billed and served through Google’s Agent Search ranking API, which means two things need to be true before it’ll run:
- API enabled: the Discovery Engine API needs to be turned on for your project.
- IAM role: whichever principal is running the query needs the
roles/discoveryengine.viewerrole (which contains thediscoveryengine.rankingConfigs.rankpermission), or an equivalent custom role covering the same permission.
If you skip either step, the query fails with a permissions error rather than a syntax one.
Syntax
AI.RANK(
TABLE table_name | (query_statement),
column_to_rank,
query
[, model => 'model']
[, top_n => top_n]
)
The first argument is either a table name or a query, passed as TABLE or a parenthesised query_statement. column_to_rank and query are both positional string arguments, not named ones, which is easy to get wrong by analogy with model and top_n below. column_to_rank is required and must name a STRING column in that table or query result. The input can’t already contain a column called rank or score in any case, since the output uses those names. query is the required, non-empty natural-language description you’re ranking against, passed as the next plain string in position, with no parameter name in front of it. model is optional and, unlike the two arguments before it, uses the named model => '...' syntax; it defaults to semantic-ranker-default@latest, which Google’s own reference page says “always points to the latest available ranking model” and recommends leaving alone rather than pinning a version. top_n is likewise named (top_n => ...), optional, and limits the output to the N most relevant rows.
The output adds a rank column (INT64, position starting at 1) and a score column (FLOAT64 between 0.0 and 1.0), alongside whatever columns were in the input. Before you build anything on top of the scores, it is worth knowing: they’re only comparable within one result set, not across different queries or runs, so a fixed threshold won’t reliably transfer; and a score of 0.0 is ambiguous, it can mean “irrelevant” or “never ranked” (rows with a NULL or empty column_to_rank value are dropped, and rows beyond top_n are simply omitted. Neither case raises an error, so the output can have fewer rows than the input going in).
Finding the Bluetooth complaints without searching for the word “Bluetooth”
The whole point of a relevance ranker rather than a keyword filter is that it doesn’t need the exact word to show up in the text. Here’s the full Electronics customer_note column, ranked by relevance to a description of the problem rather than a string match:
SELECT
rank,
score,
order_id,
customer_note
FROM
AI.RANK(
(
SELECT order_id, customer_note
FROM `bq_ai_demo.ecommerce_orders`
WHERE product_category = 'Electronics'
AND customer_note IS NOT NULL
),
'customer_note',
'Connectivity problems with pairing or maintaining a Bluetooth connection',
top_n => 10
);
The two Bluetooth-specific note variants we planted land at the top with the highest scores. More interestingly, any other Electronics notes that touch on connectivity in looser language, without using the word “Bluetooth” at all, rank above the notes about unrelated issues like packaging or delivery. A LIKE '%bluetooth%' filter would have missed those; AI.RANK doesn’t need the keyword to recognise the topic.

Choosing which column to rank on
column_to_rank matters more than it looks. Ranking on a short field versus a long one changes what “relevant” ends up meaning in practice. Google’s own reference page makes this point directly against bigquery-public-data.bbc_news.fulltext, which has title, body and category columns, and its worked examples are specific enough to run exactly as published:
SELECT
rank,
score,
title
FROM
AI.RANK(
TABLE `bigquery-public-data.bbc_news.fulltext`,
'title',
'downward trends in the housing market',
top_n => 5
);
Ranking on title for that query returns, in order: “House prices drop as sales slow” (0.6353), down to “No seasonal lift for house market” at rank 5 (0.2671); headlines directly about the topic.
The column choice bites harder on a less headline-friendly query. Google’s reference page runs ‘executives who resigned after an accounting scandal’ against title and against body separately, and an Aer Lingus article about a resigning executive shows up when body is ranked but is missed entirely when only title is ranked. The headline never used language close enough to the query for a title-only ranking to surface it. Narrowing the candidate set with an ordinary filter first works the same way as the Bluetooth example above:
SELECT
rank,
score,
title
FROM
AI.RANK(
(
SELECT title, body
FROM `bigquery-public-data.bbc_news.fulltext`
WHERE category = 'business'
),
'body',
'executives who resigned after an accounting scandal',
top_n => 3
);
That returns “Senior Fannie Mae bosses resign” (0.9338), “$1m payoff for former Shell boss” (0.9296), and “Japanese mogul arrested for fraud” (0.8640). The Aer Lingus piece the title-only ranking missed is exactly the kind of result this version catches. Which column you want depends on the question: a “what’s this specifically about” search wants the tighter title column; a “does this touch on the topic anywhere” search wants the broader body column.
Ranking inside a filtered query
The first argument to AI.RANK doesn’t have to be a bare table name, as the business-news example above already shows it can be any query. That’s useful for narrowing a candidate set with ordinary SQL filters before the ranking step runs, but on customer_note specifically it’s worth knowing what you’re actually ranking first.
Our shared demo dataset draws customer_note from a small, fixed set of just six template notes per product category, assigned deterministically when the column was added, not six distinct notes per order. Thousands of orders in each category repeat the exact same handful of phrases. Rank every order row directly and the single template that scores highest for a given query can fill most or all of top_n with duplicate text from different orders, correct ranking behaviour, but not a useful result to look at. Ranking the distinct notes instead avoids that, and is the more useful pattern on a column like this regardless of the demo:
SELECT
rank,
score,
product_category,
customer_note
FROM
AI.RANK(
(
SELECT DISTINCT product_category, customer_note
FROM `bq_ai_demo.ecommerce_orders`
WHERE customer_note IS NOT NULL
),
'customer_note',
'A customer reporting a fault with the product itself, rather than delivery or packaging',
top_n => 10
);
That returns at most ten rows, each a distinct (category, note) pair, so you’re actually looking at ten different phrases ranked against each other rather than ten copies of whichever one phrase won. If you then want the actual orders behind a specific ranked note, for example the ones from a given date range for follow-up, join back onto the base table rather than ranking it directly a second time:
SELECT order_id, order_date
FROM `bq_ai_demo.ecommerce_orders`
WHERE product_category = 'Outerwear'
AND customer_note = 'Runs a little large, had to size down.'
AND order_date >= '2026-07-01'
ORDER BY order_date;
The general lesson, not specific to this dataset: before ranking a text column directly, check how many distinct values it actually has. A column with a handful of repeated templates behind it (a dropdown-driven note field, a fixed set of canned responses, anything generated rather than freely typed) ranks more usefully on its distinct values than on every row.

A note on cost: AI.RANK has no index
AI.RANK re-evaluates relevance against every row you pass it, every time you run the query. There’s no persistent index sitting behind it the way there is with a vector search over precomputed embeddings. That’s exactly why narrowing the input with ordinary WHERE filters, as in the examples above, isn’t just a performance nicety, it’s the main lever you have over cost. Ranking 15 filtered rows against a query is a different bill to ranking an entire unfiltered table.
For a genuinely large candidate set, Google’s own reference page gives a concrete two-stage pattern rather than just describing one in the abstract: run VECTOR_SEARCH over a precomputed embedding column first, to cheaply narrow millions of rows down to a shortlist, then run AI.RANK over just that shortlist for a more accurate final ordering. The published example does exactly this against the same BBC News table, which already carries a body_embedded column of precomputed embeddings:
SELECT rank, score, title
FROM AI.RANK(
(
SELECT base.title AS title, base.body AS body
FROM VECTOR_SEARCH(
TABLE `bigquery-public-data.bbc_news.fulltext`,
'body_embedded',
(
SELECT body_embedded
FROM `bigquery-public-data.bbc_news.fulltext`
WHERE title = 'Bank opts to leave rates on hold'
),
top_k => 50)
),
'body',
'downward trends in the housing market',
top_n => 5
);
VECTOR_SEARCH returns its matches nested inside a base struct column (the original matched row), which is why the subquery flattens it out first with base.title AS title, base.body AS body. Pass VECTOR_SEARCH’s raw output straight into AI.RANK without that flattening step and column_to_rank won’t find a plain body column to rank.
VECTOR_SEARCH does the cheap part first, pulling the 50 articles whose embeddings sit closest to a seed article (“Bank opts to leave rates on hold”), and AI.RANK does the expensive, more accurate part only on that shortlist of 50 rather than the whole table. Google’s reference page shows this returning, in order: “No seasonal lift for house market” (0.9268), “House prices drop as sales slow” (0.9170), “‘Post-Christmas lull’ in lending” (0.8830), “House prices show slight increase” (0.7935), and “House prices rebound says Halifax” (0.7747), which is a different ranking from the title-only example above, since this one is ranking body text pulled from a pre-filtered shortlist rather than every title in the table.
The page notes scores vary between runs and models, so compare rows by rank, not by the exact score. Google’s own docs note that AI.EMBED can generate an embedding for a free-text phrase instead of an existing row, if you want to seed the vector search from a description rather than a document already in the table. Our shared demo dataset doesn’t have a precomputed embedding column yet, so this specific example runs against the public BBC dataset rather than bq_ai_demo.ecommerce_orders.
What this is useful for
A lot of what gets called “search” inside a brand’s own data is really a keyword filter wearing a search box. AI.RANK is a genuine relevance ranker: it finds the support note, the product review, the article that’s actually about what you asked for, including the ones that never use your search term. The Bluetooth example above is the toy version of a real problem: a support team searching their own ticket history for “connectivity issues” and missing half the relevant tickets because the customer wrote “keeps disconnecting” instead.
The two things worth being disciplined about are the ones covered above: pick column_to_rank deliberately rather than defaulting to whichever column is longest, and filter the candidate set before ranking rather than after. This is done both for cost, and because a ranker given a smaller, more relevant pool to begin with tends to produce a more useful ordering at the top of it. For anything beyond a few thousand candidate rows, pair it with VECTOR_SEARCH as above rather than pointing it at the whole table.
Newsletter
Occasional notes on data, tracking & AI-readiness
No spam, no growth-hacking. Just an email when there's something worth reading.