Exercise 2: tsvector, tsquery, @@, and the Worked Example's Components — Possible Solution ==================================================================== THE ROLE OF tsvector ------------------------------ Per this chapter, "a tsvector is a preprocessed, normalized representation of a document's searchable text — it converts raw text into lexemes (normalized word forms), strips out stop words, and can optionally weight different parts of a document differently." In the worked example, the articles.search_vector column is a generated tsvector built from both the title and body columns, so the document being searched is preprocessed once and stored, rather than reparsed on every search. THE ROLE OF tsquery ------------------------------ Per this chapter, "a tsquery is a processed search query, converted into the same lexeme form, supporting boolean operators." In the worked example, websearch_to_tsquery('english', 'postgres indexing') converts the user's plain search text into a tsquery automatically. THE ROLE OF THE @@ OPERATOR ------------------------------ Per this chapter, "the @@ match operator tests whether a tsvector satisfies a tsquery." In the worked example's WHERE clause, search_vector @@ query filters the articles down to only those whose tsvector actually matches the search tsquery. HOW THE WORKED EXAMPLE'S SPECIFIC ELEMENTS FIT TOGETHER ------------------------------ Per this chapter, setweight(to_tsvector('english', title), 'A') || setweight(to_tsvector('english', body), 'B') builds the tsvector with title matches given a higher weight ('A') than body matches ('B'), so "title matches rank higher than body matches." websearch_to_tsquery "parses ordinary user search input (rather than requiring the user to type boolean operators directly)," converting a plain search phrase into a real tsquery. Finally, ts_rank(search_vector, query) "orders results by actual relevance, not just by whether they matched at all" — using the weighting set up by setweight to produce a meaningful ORDER BY rank DESC. WHY THIS WORKS AS AN ANSWER ------------------------------ It defines all three core concepts using the chapter's own exact wording, and connects each of the worked example's named functions (setweight, websearch_to_tsquery, ts_rank) to its specific role, rather than describing tsvector/tsquery/@@ in the abstract only.