Wildcard Search
Search for partial matches using pattern wildcards. Useful when you need prefix, infix, suffix, or complex pattern matching on indexed terms.
See Setup for the shared dataset used in all examples.
Wildcard characters
| Character | Meaning |
|---|---|
_ | Match any single character |
% | Match zero or more characters |
\_ | Literal underscore |
\% | Literal percent sign |
Prefix matching
Find genres starting with 'sci':
Query
SELECT id, title, genreFROM movies_idxWHERE genre @@ ts_like('sci%')ORDER BY id;Result
id | title | genre----+-------------------------------+-------- 1 | The Matrix | sci-fi 2 | The Matrix Reloaded | sci-fi 3 | The Matrix Revolutions | sci-fi 7 | Star Trek: The Motion Picture | sci-fi 8 | Alien | sci-fiSuffix matching
Find genres ending with 'dy':
Query
SELECT id, title, genreFROM movies_idxWHERE genre @@ ts_like('%dy')ORDER BY id;Result
id | title | genre----+--------------------------+-------- 6 | Scary Movie | comedy 10 | The Grand Budapest Hotel | comedyInfix matching
Find genres containing 'vent':
Query
SELECT id, title, genreFROM movies_idxWHERE genre @@ ts_like('%vent%')ORDER BY id;Result
id | title | genre----+---------------+----------- 4 | Jurassic Park | adventureSingle character wildcard
Match genres with exactly one character between 'co' and 'edy':
Query
SELECT id, title, genreFROM movies_idxWHERE genre @@ ts_like('co_edy')ORDER BY id;Result
id | title | genre----+--------------------------+-------- 6 | Scary Movie | comedy 10 | The Grand Budapest Hotel | comedyComplex patterns
Combine wildcards for more specific matching. Find words matching 'h_____' (h + exactly 5 characters):
Query
SELECT id, titleFROM movies_idxWHERE description @@ ts_like('h_____')ORDER BY id;Result
id | title----+-------------- 1 | The Matrix 9 | Café SocietyWildcard on text fields
Search for tokens matching a pattern within full-text columns:
Query
SELECT id, titleFROM movies_idxWHERE description @@ ts_like('block%')ORDER BY id;Result
id | title----+--------------- 4 | Jurassic Park 6 | Scary MovieCombine with other search functions
Query
SELECT id, title, genreFROM movies_idxWHERE genre @@ ts_like('sci%') AND description @@ ts_phrase('alien')ORDER BY id;Result
id | title | genre----+-------------------------------+-------- 7 | Star Trek: The Motion Picture | sci-fiSee also
- Exact Value Matching — exact term matching with the
@@operator - Phrase and Proximity Search — ordered token matching