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?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;
ts_rank takes the document vector first, then the query vector, and the WHERE clause must filter matching documents.Option D correctly uses ts_rank with the document vector first and the query second. It filters documents matching the query and orders by rank descending, limiting to top 3.
Option D reverses the arguments to ts_rank, which is invalid.
Option D uses a different query with | (OR) instead of & (AND), changing the meaning.
Option D lacks the WHERE clause, so it ranks all documents including non-matching ones, which is incorrect.
ts_rank(vector, query, normalization) in PostgreSQL?Normalization in ts_rank adjusts the ranking score to account for document length and term frequency, so longer documents don't get higher scores just because they contain more words.
Options A, C, and D describe unrelated behaviors.
ts_rank in PostgreSQL?ts_rank.Option C passes a plain string instead of a tsquery type as the second argument, causing a syntax or runtime error.
Options A, B, and C correctly use to_tsquery for the query argument and valid normalization values.
ts_rank queries on a large documents table?Option A uses a GIN index on the tsvector expression, which speeds up full-text search queries using the @@ operator, improving performance.
Option A uses a B-tree index and LIKE, which is inefficient for full-text search.
Option A changes the ranking function but does not improve indexing or search speed.
Option A adds complexity and maintenance overhead without improving query speed.
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?
PostgreSQL's full-text search uses stemming, so 'database' can be reduced to the lexeme 'data', causing it to match the query 'data & science'. This can cause documents with 'database' to rank higher if they also contain 'science'.
Options B, C, and D describe incorrect behaviors not consistent with the query.