0
0
PostgreSQLquery~20 mins

Ranking with ts_rank in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Full-Text Search Ranking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Ranking documents by relevance using ts_rank
Given a table documents with columns id and content, and a full-text search query to_tsquery('english', 'database & indexing'), which SQL query correctly ranks documents by relevance using ts_rank and returns the top 3?
PostgreSQL
SELECT id, ts_rank(to_tsvector('english', content), to_tsquery('english', 'database & indexing')) AS rank
FROM documents
WHERE to_tsvector('english', content) @@ to_tsquery('english', 'database & indexing')
ORDER BY rank DESC
LIMIT 3;
ASELECT id, ts_rank(to_tsvector('english', content), to_tsquery('english', 'database & indexing')) AS rank FROM documents ORDER BY rank DESC LIMIT 3;
BSELECT id, ts_rank(to_tsquery('english', 'database & indexing'), to_tsvector('english', content)) AS rank FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'database & indexing') ORDER BY rank DESC LIMIT 3;
CSELECT id, ts_rank(to_tsvector('english', content), to_tsquery('english', 'database | indexing')) AS rank FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'database & indexing') ORDER BY rank DESC LIMIT 3;
DSELECT id, ts_rank(to_tsvector('english', content), to_tsquery('english', 'database & indexing')) AS rank FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'database & indexing') ORDER BY rank DESC LIMIT 3;
Attempts:
2 left
💡 Hint
Remember that ts_rank takes the document vector first, then the query vector, and the WHERE clause must filter matching documents.
🧠 Conceptual
intermediate
1:30remaining
Understanding ts_rank normalization options
Which statement correctly describes the effect of the normalization parameter in ts_rank(vector, query, normalization) in PostgreSQL?
ANormalization changes the language dictionary used for parsing the document vector.
BNormalization adjusts the rank by document length and frequency to prevent longer documents from scoring higher just because they have more words.
CNormalization converts the rank score into a boolean value indicating match or no match.
DNormalization sorts the results alphabetically instead of by rank.
Attempts:
2 left
💡 Hint
Think about why longer documents might unfairly get higher ranks without adjustment.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in ts_rank usage
Which of the following SQL queries will raise a syntax error when using ts_rank in PostgreSQL?
ASELECT ts_rank(to_tsvector('english', content), to_tsquery('english', 'search & term')) FROM documents;
BSELECT ts_rank(to_tsvector('english', content), to_tsquery('english', 'search & term'), 0) FROM documents;
CSELECT ts_rank(to_tsvector('english', content), 'search & term') FROM documents;
DSELECT ts_rank(to_tsvector('english', content), to_tsquery('english', 'search & term'), 1) FROM documents;
Attempts:
2 left
💡 Hint
Check the types of arguments passed to ts_rank.
optimization
advanced
2:00remaining
Improving performance of ts_rank queries
Which approach will best improve performance when running ts_rank queries on a large documents table?
ACreate a GIN index on the <code>to_tsvector('english', content)</code> expression and use the <code>@@</code> operator in the WHERE clause.
BAdd a B-tree index on the <code>content</code> column and use <code>LIKE '%search%'</code> in the WHERE clause.
CUse <code>ts_rank_cd</code> instead of <code>ts_rank</code> without any indexes.
DStore the rank values in a separate column and update them manually after inserts.
Attempts:
2 left
💡 Hint
Think about how PostgreSQL can quickly find matching rows for full-text search.
🔧 Debug
expert
2:30remaining
Debugging unexpected ts_rank results
A developer runs this query to rank documents by relevance:

SELECT id, ts_rank(to_tsvector('english', content), to_tsquery('english', 'data & science')) AS rank FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'data & science') ORDER BY rank DESC;

They notice some documents with the word 'database' rank higher than those with 'data' and 'science' explicitly. What is the most likely cause?
AThe <code>to_tsquery</code> query is parsed as <code>data & science</code>, but 'database' matches the lexeme 'data' due to stemming, causing unexpected matches.
BThe <code>ts_rank</code> function is ignoring the <code>WHERE</code> clause and ranking all documents.
CThe <code>ORDER BY rank DESC</code> clause is sorting results in ascending order by mistake.
DThe <code>to_tsvector</code> function is not applied to the <code>content</code> column, so ranking is random.
Attempts:
2 left
💡 Hint
Consider how stemming affects lexeme matching in full-text search.