0
0
PostgreSQLquery~20 mins

Search configuration and languages in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Search Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a full-text search with language configuration
Given a table documents with a content column, what is the output of this query?

SELECT content FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'cat & dog');

Assume the table has these rows:
1. 'The cat chased the dog.'
2. 'A dog and a cat are friends.'
3. 'Cats are independent animals.'
PostgreSQL
CREATE TABLE documents (content TEXT);
INSERT INTO documents VALUES
('The cat chased the dog.'),
('A dog and a cat are friends.'),
('Cats are independent animals.');

SELECT content FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'cat & dog');
ANo rows returned
B'Cats are independent animals.'
C
'The cat chased the dog.'
'A dog and a cat are friends.'
'Cats are independent animals.'
D
'The cat chased the dog.'
'A dog and a cat are friends.'
Attempts:
2 left
💡 Hint
Remember that the query looks for rows containing both 'cat' and 'dog' after stemming and stop-word removal.
🧠 Conceptual
intermediate
1:30remaining
Purpose of search configuration languages in PostgreSQL
What is the main purpose of specifying a language in PostgreSQL's full-text search configuration?
ATo change the database encoding to match the language
BTo apply language-specific rules for stemming and stop-word removal
CTo translate the search query into multiple languages automatically
DTo enable case-sensitive search only for that language
Attempts:
2 left
💡 Hint
Think about how words are reduced to their root forms and common words are ignored.
📝 Syntax
advanced
2:00remaining
Correct syntax to create a custom text search configuration
Which of the following commands correctly creates a new text search configuration named my_english based on the built-in english configuration?
ACREATE TEXT SEARCH CONFIGURATION my_english ( COPY = english );
BCREATE TEXT SEARCH CONFIGURATION my_english AS english;
CCREATE TEXT SEARCH CONFIGURATION my_english FROM english;
DCREATE TEXT SEARCH CONFIGURATION my_english LIKE english;
Attempts:
2 left
💡 Hint
The syntax uses COPY to duplicate an existing configuration.
🔧 Debug
advanced
2:00remaining
Why does this full-text search query return no results?
Consider this query:

SELECT * FROM articles WHERE to_tsvector('simple', body) @@ to_tsquery('english', 'databases');

Given that body contains the word 'databases' in some rows, why might this query return no rows?
ABecause the <code>to_tsvector</code> and <code>to_tsquery</code> use different language configurations
BBecause the <code>body</code> column is not indexed
CBecause <code>to_tsquery</code> requires the query string to be in uppercase
DBecause the word 'databases' is a stop word in the 'simple' configuration
Attempts:
2 left
💡 Hint
Check if the text search configurations match in both functions.
optimization
expert
3:00remaining
Optimizing full-text search for multiple languages
You have a multilingual table with a content column and a language column specifying the language of each row (e.g., 'english', 'french'). You want to create a full-text search index that respects each row's language for stemming and stop-word removal.

Which approach is the best to optimize search performance?
ACreate separate tsvector columns for each language and create separate indexes, then query the correct one based on language
BCreate no index and rely on sequential scans with <code>to_tsvector(language, content)</code> in the WHERE clause
CCreate a single tsvector column using <code>to_tsvector(language, content)</code> and create a GIN index on it
DCreate a tsvector column using a fixed language configuration (e.g., 'english') for all rows and ignore the language column
Attempts:
2 left
💡 Hint
Think about how to store and index the tsvector efficiently for multiple languages.