Challenge - 5 Problems
Full-Text Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Basic full-text search query output
Given a table
Assume the table has:
- id=1, content='Database systems are essential for search functionality.'
- id=2, content='Database search engines use full-text search techniques.'
- id=3, content='Databases store data efficiently.'
articles with a content column, what is the output of this query?SELECT id FROM articles WHERE to_tsvector('english', content) @@ to_tsquery('english', 'database & search');Assume the table has:
- id=1, content='Database systems are essential for search functionality.'
- id=2, content='Database search engines use full-text search techniques.'
- id=3, content='Databases store data efficiently.'
Attempts:
2 left
💡 Hint
Look for rows where both 'database' and 'search' appear in the content.
✗ Incorrect
The query uses full-text search to find rows containing both 'database' and 'search'. Rows 1 and 2 contain both words, row 3 does not contain 'search'.
🧠 Conceptual
intermediate1:30remaining
Why use full-text search over LIKE?
Which of the following is the main advantage of using full-text search instead of the
LIKE operator for searching text in PostgreSQL?Attempts:
2 left
💡 Hint
Think about how full-text search understands word forms and order.
✗ Incorrect
Full-text search supports stemming (finding word variations) and ignores word order, making it more flexible and powerful than LIKE, which only matches exact substrings.
📝 Syntax
advanced2:00remaining
Correct syntax for creating a full-text index
Which option correctly creates a full-text search index on the
content column of the documents table using the English dictionary?Attempts:
2 left
💡 Hint
Full-text indexes use GIN and tsvector, not tsquery or btree.
✗ Incorrect
The correct syntax uses GIN index on the tsvector generated from the content column with the English dictionary. Btree and hash are not suitable for full-text search indexes.
❓ optimization
advanced1:30remaining
Improving full-text search performance
You notice your full-text search queries on a large
articles table are slow. Which change will most improve query speed?Attempts:
2 left
💡 Hint
Indexes help speed up searches on large tables.
✗ Incorrect
Creating a GIN index on the tsvector column allows PostgreSQL to quickly find matching rows, greatly improving full-text search performance.
🔧 Debug
expert2:30remaining
Why does this full-text search query return no results?
Given the query:
and the
SELECT * FROM posts WHERE to_tsvector('english', body) @@ to_tsquery('english', 'running & fast');and the
body contains 'Running fast is important', why does this query return no rows?Attempts:
2 left
💡 Hint
Check how to_tsquery handles word forms and stemming.
✗ Incorrect
to_tsquery does not stem words automatically, so 'running' does not match 'run'. The correct query should use 'run:*' to match word prefixes or use plainto_tsquery.