Do all queries have to be be in dictionary?
No. Because only word stems (according to the used text search configuration) are in the index to begin with. But more importantly:
No. Because, on top of that Full Text Search is also capable of prefix matching:
This would work:
SELECT id, subject
WHERE tsv @@ to_tsquery('simple', 'avail:*')
ORDER BY id DESC;
Note 3 things:
plainto_tsquery(), in this case because (quoting the manual):
plainto_tsquery will not recognize
tsquery operators, weight labels, or prefix-match labels in its input
'simple' text search configuration to generate the
tsquery since you obviously want to take the word 'avail' as is and not apply stemming.
:* to make it a prefix search, i.e find all lexemes starting with 'avail'.
Important: This is a prefix search on lexemes (word stems) in the document. A regular expression match without wildcards (
content ~* 'avail') is not exactly the same! The latter is not left-anchored (to the start of lexemes) and would also find 'FOOavail' etc.
It's unclear whether you want the behavior outlined in your query or the equivalent of the added regular expression. Trigram indexes (
pg_trgm) like @Evan already suggested are the right tool for that. There are many related questions on dba.SE, try a search.
('Zend has no framework')
, ('Zend Framework')
) sub(t), to_tsvector(t) AS tsv
WHERE tsv @@ to_tsquery('zend <-> fram:*');
id | t | tsv
2 | Zend Framework | 'framework':2 'zend':1
Recent related answer (chapter Different approach to optimize search):
Since you mentioned emails, be aware that the text search parser identifies emails and does not split them into separate words / lexemes. Consider:
SELECT ts_debug('english', 'firstname.lastname@example.org')
I would replace the separators
. in your emails with space (
' ') to index contained words.
Also, since you are dealing with names in emails, not with English (or some other language) words, I would use the
'simple' text search configuration to disable stemming and other language features:
ts_vector column with:
SELECT to_tsvector('simple', translate('email@example.com', '@.', ' ')) AS tsv;