Bird
0
0

You want to highlight multiple search terms 'fast' or 'reliable' in a product description using ts_headline. Which query correctly highlights both terms in the description column?

hard📝 Application Q15 of 15
PostgreSQL - Full-Text Search
You want to highlight multiple search terms 'fast' or 'reliable' in a product description using ts_headline. Which query correctly highlights both terms in the description column?
ASELECT ts_headline(description, to_tsquery('english', 'fast | reliable')) FROM products;
BSELECT ts_headline(description, to_tsquery('fast & reliable')) FROM products;
CSELECT ts_headline(description, 'fast & reliable', 'english') FROM products;
DSELECT ts_headline(description, 'fast reliable') FROM products;
Step-by-Step Solution
Solution:
  1. Step 1: Understand tsquery for multiple terms

    To highlight multiple terms, use to_tsquery with operators like | (OR) or & (AND).
  2. Step 2: Analyze options for correct syntax

    SELECT ts_headline(description, to_tsquery('english', 'fast | reliable')) FROM products; uses to_tsquery('english', 'fast | reliable') which searches for either term correctly. SELECT ts_headline(description, 'fast & reliable', 'english') FROM products; uses plain string with & which is invalid. SELECT ts_headline(description, to_tsquery('fast & reliable')) FROM products; misses language config. SELECT ts_headline(description, 'fast reliable') FROM products; uses plain string without operators.
  3. Final Answer:

    SELECT ts_headline(description, to_tsquery('english', 'fast | reliable')) FROM products; -> Option A
  4. Quick Check:

    Use to_tsquery with OR operator = C [OK]
Quick Trick: Use to_tsquery with | to highlight multiple terms [OK]
Common Mistakes:
  • Passing plain strings without to_tsquery for multiple terms
  • Using & in plain strings instead of tsquery syntax
  • Omitting language in to_tsquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes