Sub topic 8
Weighting Title Over Body with `setweight`
Assume you know basic SELECT, WHERE, and ORDER BY statements, and you can create PostgreSQL tables and indexes. This chapter shows how to make a match in an article title rank higher than the same match in the body.
Why weights matter
A search result can match several fields. A match in a title usually signals stronger relevance than a match deep in an article. PostgreSQL represents this difference with weights attached to lexemes in a tsvector.
setweight assigns one of four labels:
| Weight | Typical use | Relative strength |
|---|---|---|
| A | Title or name | Highest |
| B | Main body | High |
| C | Author or category | Lower |
| D | Metadata | Lowest |
The labels do not filter results. They affect ranking functions such as ts_rank and ts_rank_cd.
Building a weighted document
Create one vector for the title and another for the body. Concatenate them with ||:
WITH documents (id, title, body) AS (
VALUES
(1, 'PostgreSQL indexing',
'Learn how B-tree and GIN indexes improve query performance.'),
(2, 'Database basics',
'This article introduces PostgreSQL full-text search and indexing.')
),
indexed AS (
SELECT
id,
title,
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'B') AS search
FROM documents
),
query AS (
SELECT websearch_to_tsquery('english', 'PostgreSQL indexing') AS terms
)
SELECT
indexed.id,
indexed.title,
ts_rank(indexed.search, query.terms) AS rank
FROM indexed
CROSS JOIN query
WHERE indexed.search @@ query.terms
ORDER BY rank DESC;The coalesce calls convert a possible NULL value to an empty string. The title lexemes receive weight A, while body lexemes receive weight B. The @@ operator still decides whether a document matches. ts_rank then uses the weights while calculating the order.
A weight is not an absolute rule. A document with many body matches can still rank highly. If the default difference is not suitable, provide custom weights. The array order is D, C, B, A:
SELECT ts_rank(
search,
websearch_to_tsquery('english', 'PostgreSQL indexing'),
ARRAY[0.05, 0.15, 0.4, 2.0]
) AS rank
FROM indexed;Here, an A match contributes 2.0, while a D match contributes 0.05.
Common errors
- Applying
setweightto the whole combined vector prevents field-specific weighting. - Using weight
Afor every field removes the distinction between title and body. - Expecting weights to change matching behavior. They affect ranking, not whether
@@finds a match. - Forgetting to include the weighted vector in the query path. Ranking an unweighted vector loses the field priority.
Exercise
How would you change the vector so that author receives weight C, while title receives A and body receives B?
Show answer
Convert each field separately with to_tsvector, apply setweight to each result, and concatenate them with ||: setweight(to_tsvector('english', title), 'A') || setweight(to_tsvector('english', body), 'B') || setweight(to_tsvector('english', author), 'C').