Complete the code to create a full-text search index on the 'content' column.
CREATE INDEX idx_content_search ON documents USING gin(to_tsvector('english', [1]));
The to_tsvector function converts the 'content' column into a searchable text vector. Creating a GIN index on this vector speeds up full-text search queries.
Complete the code to perform a full-text search query for the word 'cloud'.
SELECT * FROM documents WHERE to_tsvector('english', content) @@ [1]('cloud');
to_tsquery which requires special syntax for complex queries.ts_rank which is for ranking results, not querying.plainto_tsquery converts a plain text query into a tsquery format suitable for matching. It is simple and effective for single words like 'cloud'.
Fix the error in the query to rank search results by relevance.
SELECT *, [1](to_tsvector('english', content), plainto_tsquery('english', 'cloud')) AS rank FROM documents ORDER BY rank DESC;
to_tsquery or plainto_tsquery which create queries but do not rank.ts_vector which is not a function.ts_rank calculates the relevance rank of each document based on the search query. This allows ordering results by how well they match.
Fill both blanks to create a materialized view that stores ranked search results for 'cloud'.
CREATE MATERIALIZED VIEW cloud_search_results AS SELECT id, content, [1](to_tsvector('english', content), [2]('english', 'cloud')) AS rank FROM documents ORDER BY rank DESC;
to_tsquery and plainto_tsquery.tsvector which is not a function.The materialized view uses ts_rank to calculate relevance and plainto_tsquery to create the search query for 'cloud'. This stores ranked results for fast access.
Fill all three blanks to create a trigger that updates a tsvector column on insert or update.
CREATE FUNCTION update_search_vector() RETURNS trigger AS $$ BEGIN NEW.search_vector := [1]('english', [2]); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON documents FOR EACH ROW EXECUTE FUNCTION [3]();
plainto_tsquery instead of to_tsvector.The function uses to_tsvector on the 'content' column to update the 'search_vector' column. The trigger calls this function on insert or update to keep the search vector current.