Complete the code to select the rank of the text search vector using ts_rank.
SELECT ts_rank([1], to_tsquery('english', 'database')) AS rank FROM documents;
The ts_rank function requires a tsvector as its first argument. Using to_tsvector('english', content) converts the text content into a tsvector suitable for ranking.
Complete the code to filter documents matching the search query using @@ operator.
SELECT content FROM documents WHERE to_tsvector('english', content) [1] to_tsquery('english', 'database');
The @@ operator checks if the tsvector matches the tsquery, filtering relevant documents.
Fix the error in the code to correctly rank and order documents by relevance.
SELECT content, ts_rank(to_tsvector('english', content), to_tsquery('english', 'database')) AS rank FROM documents ORDER BY [1] DESC;
ts_rank directly in ORDER BY without alias.The alias rank is used in the ORDER BY clause to sort results by their relevance score.
Fill both blanks to create a query that selects content and ranks it, filtering only relevant documents.
SELECT content, ts_rank([1], [2]) AS rank FROM documents WHERE [1] @@ [2] ORDER BY rank DESC;
The to_tsvector converts content to a searchable vector, and to_tsquery creates the search query. Both are used for ranking and filtering.
Fill all three blanks to create a query that ranks documents with weighted ts_rank and filters by search query.
SELECT content, ts_rank([3], [1], [2]) AS rank FROM documents WHERE [1] @@ [2] ORDER BY rank DESC;
The weights string is the first argument to ts_rank, followed by the tsvector and the tsquery. This prioritizes matches based on position (A=0.1, B=0.2, C=0.4, D=1.0).