0
0
PostgreSQLquery~20 mins

Highlighting with ts_headline in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Highlighting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Basic usage of ts_headline
Given the table documents with a text column content, what is the output of the following query?
SELECT ts_headline('english', content, to_tsquery('english', 'database')) AS headline FROM documents WHERE id = 1;
PostgreSQL
CREATE TABLE documents (id INT, content TEXT);
INSERT INTO documents VALUES (1, 'PostgreSQL is a powerful, open source object-relational database system.');
APostgreSQL is a powerful, open source object-relational database system.
BPostgreSQL is a powerful, open source object-relational <em>database</em> system.
CPostgreSQL is a powerful, open source object-relational <b>database</b> system.
DPostgreSQL is a powerful, open source object-relational <u>database</u> system.
Attempts:
2 left
💡 Hint
ts_headline highlights matching words by default using tags.
query_result
intermediate
2:00remaining
Customizing Highlighting Tags
What will be the output of this query?
SELECT ts_headline('english', content, to_tsquery('english', 'system'), 'StartSel=, StopSel=') AS headline FROM documents WHERE id = 1;
PostgreSQL
CREATE TABLE documents (id INT, content TEXT);
INSERT INTO documents VALUES (1, 'PostgreSQL is a powerful, open source object-relational database system.');
APostgreSQL is a powerful, open source object-relational database <em>system</em>.
BPostgreSQL is a powerful, open source object-relational database <b>system</b>.
CPostgreSQL is a powerful, open source object-relational database system.
DPostgreSQL is a powerful, open source object-relational database <u>system</u>.
Attempts:
2 left
💡 Hint
The options parameter controls the tags used for highlighting.
🧠 Conceptual
advanced
2:00remaining
Understanding ts_headline Behavior with Multiple Matches
If the content contains multiple occurrences of the search term, how does ts_headline decide which occurrences to highlight by default?
AIt highlights all occurrences of the search term in the content.
BIt highlights only the first occurrence of the search term.
CIt highlights occurrences randomly throughout the content.
DIt highlights the occurrences closest to the start of the content and limits the output length.
Attempts:
2 left
💡 Hint
Consider how ts_headline tries to produce a concise snippet.
📝 Syntax
advanced
2:00remaining
Correct Syntax for ts_headline with Config and Options
Which of the following is the correct syntax to highlight the word 'search' in the column text_data using ts_headline with English configuration and custom highlight tags <mark> and </mark>?
ASELECT ts_headline('english', text_data, to_tsquery('search'), 'StartSel=<mark>, StopSel=</mark>') FROM my_table;
BSELECT ts_headline('english', text_data, to_tsquery('english', 'search'), 'StartSel=<mark>, StopSel=</mark>') FROM my_table;
CSELECT ts_headline('english', text_data, 'search', 'StartSel=<mark>, StopSel=</mark>') FROM my_table;
DSELECT ts_headline(text_data, 'english', to_tsquery('search'), 'StartSel=<mark>, StopSel=</mark>') FROM my_table;
Attempts:
2 left
💡 Hint
Remember the order of parameters: config, document, query, options.
optimization
expert
3:00remaining
Optimizing ts_headline for Large Text Columns
You have a table with a large text column and want to highlight search terms efficiently using ts_headline. Which approach is best to improve performance when running many queries?
ACreate a GIN index on the tsvector of the text column and use to_tsquery in the WHERE clause before calling ts_headline.
BCall ts_headline directly on the entire text column without any filtering.
CUse ts_headline with a very large StartSel and StopSel option to highlight more text.
DStore pre-highlighted text in a separate column and update it manually.
Attempts:
2 left
💡 Hint
Think about how to reduce the number of rows processed by ts_headline.