Bird
0
0

You want to rank documents by relevance but give higher weight to matches in the title than in the body. Which ts_rank usage achieves this?

hard📝 Application Q8 of 15
PostgreSQL - Full-Text Search
You want to rank documents by relevance but give higher weight to matches in the title than in the body. Which ts_rank usage achieves this?
Ats_rank(to_tsvector(title) + to_tsvector(body), to_tsquery('search'))
Bts_rank(setweight(to_tsvector(title), 'A') || setweight(to_tsvector(body), 'B'), to_tsquery('search'))
Cts_rank(to_tsvector(body), to_tsquery('search'))
Dts_rank(to_tsquery('search'), to_tsvector(title || body))
Step-by-Step Solution
Solution:
  1. Step 1: Understand weighting with setweight

    Use setweight to assign higher priority ('A') to title and lower ('B') to body.

  2. Step 2: Combine weighted vectors and apply ts_rank

    Concatenate weighted tsvectors and rank against query.

  3. Final Answer:

    ts_rank(setweight(to_tsvector(title), 'A') || setweight(to_tsvector(body), 'B'), to_tsquery('search')) -> Option B
  4. Quick Check:

    Weighted vectors + ts_rank = Correct ranking [OK]
Quick Trick: Use setweight to prioritize fields in ts_rank [OK]
Common Mistakes:
  • Adding vectors without weighting
  • Swapping tsquery and tsvector arguments
  • Concatenating strings instead of vectors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes