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 usage of @@ match operator
Given a table documents with a
Assume the table has these rows:
1: 'cat dog mouse'
2: 'cat mouse'
3: 'dog cat'
4: 'dog elephant'
content column of type tsvector, what is the result of this query?SELECT id FROM documents WHERE content @@ to_tsquery('cat & dog');Assume the table has these rows:
1: 'cat dog mouse'
2: 'cat mouse'
3: 'dog cat'
4: 'dog elephant'
Attempts:
2 left
💡 Hint
The @@ operator checks if the tsvector matches the tsquery condition.
✗ Incorrect
The query looks for rows where the content contains both 'cat' AND 'dog'. Rows 1 and 3 have both words, so their ids are returned.
❓ query_result
intermediate2:00remaining
Using @@ with OR condition in tsquery
What rows will this query return?
Given the same
1: 'cat dog mouse'
2: 'cat mouse'
3: 'dog cat'
4: 'dog elephant'
SELECT id FROM documents WHERE content @@ to_tsquery('cat | elephant');Given the same
documents table as before with rows:1: 'cat dog mouse'
2: 'cat mouse'
3: 'dog cat'
4: 'dog elephant'
Attempts:
2 left
💡 Hint
The | operator means OR in tsquery.
✗ Incorrect
The query matches rows containing either 'cat' or 'elephant'. Rows 1, 2, 3, and 4 match because they contain 'cat' or 'elephant'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in tsquery expression
Which option contains a syntax error in the tsquery passed to the @@ operator?
PostgreSQL
SELECT id FROM documents WHERE content @@ to_tsquery(?);
Attempts:
2 left
💡 Hint
Look for invalid operator placement in the tsquery string.
✗ Incorrect
Option C has an invalid operator sequence '& |' which is not allowed in tsquery syntax.
❓ optimization
advanced2:00remaining
Improving performance of @@ queries
Which option best improves performance of queries using the @@ operator on a large
documents table with a content tsvector column?Attempts:
2 left
💡 Hint
Think about which index type supports full text search efficiently.
✗ Incorrect
GIN indexes are designed for fast full text search with tsvector columns and @@ operator.
🧠 Conceptual
expert3:00remaining
Understanding @@ operator behavior with phrase search
Consider this query:
How does the @@ operator behave differently here compared to using
SELECT id FROM documents WHERE content @@ phraseto_tsquery('cat dog');How does the @@ operator behave differently here compared to using
to_tsquery('cat & dog')?Attempts:
2 left
💡 Hint
phraseto_tsquery creates a phrase search condition.
✗ Incorrect
phraseto_tsquery creates a tsquery that matches the exact phrase with words adjacent and in order, unlike to_tsquery which matches words anywhere.