Sub topic 7
Exact Nearest-Neighbor Queries with `ORDER BY` and `LIMIT`
Assume you know basic SQL and PostgreSQL, and that the vector extension is enabled. You also need a table with a fixed-dimension vector column, such as embedding vector(3).
How exact nearest-neighbor search works
An embedding is a numeric representation of an item, such as a sentence or image. A query embedding represents what you are looking for. Nearest-neighbor search compares the query vector with every stored vector and returns the closest ones.
An exact query examines all eligible rows before selecting the nearest results. This gives the correct result for the chosen distance metric, but the work grows with the number of rows.
The basic SQL shape is:
SELECT id, content, embedding <-> '[3,1,2]' AS distance
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 5;The expression after ORDER BY calculates the distance from each stored vector to the query vector. PostgreSQL sorts the rows from smallest distance to largest distance. LIMIT 5 keeps only the five nearest rows.
The query vector has three values because embedding was declared as vector(3). A vector with a different number of dimensions cannot be compared with that column.
Choosing the distance expression
pgvector provides different operators for different similarity models.
| Operator | Measure | Smaller value means |
|---|---|---|
| `<->` | Euclidean, or L2, distance | More geometrically close |
| `<=>` | Cosine distance | More similar direction |
| `<#>` | Negative inner product | Larger inner product |
For cosine similarity, order by cosine distance and convert the result for display:
SELECT
id,
content,
1 - (embedding <=> '[3,1,2]') AS cosine_similarity
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;The ordering uses the distance expression, not the displayed similarity expression. Cosine distance is 1 - cosine similarity, so ascending distance produces descending similarity.
The <#> operator returns the negative inner product because PostgreSQL indexes use ascending order. If you want to display the inner product itself, multiply the result by -1, but keep the operator unchanged in ORDER BY:
SELECT
id,
content,
-(embedding <#> '[3,1,2]') AS inner_product
FROM items
ORDER BY embedding <#> '[3,1,2]'
LIMIT 5;Use the metric that matches the embedding model. If the model was trained or evaluated with cosine similarity, use cosine distance. Changing the metric can change the meaning of “nearest.”
Filters and result counts
A WHERE clause is applied to the candidate rows:
SELECT id, content
FROM items
WHERE category_id = 1
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;If fewer than five rows match the filter, the query returns fewer than five rows. A vector index does not guarantee that the requested number of results exists within the filtered set.
Exercise
Why does a cosine query order by <=> even when it displays 1 - (embedding <=> query)?
Show answer
<=> returns cosine distance, and smaller distance means greater similarity. Subtracting it from 1 changes the displayed value but does not change the correct ascending ordering expression.