Postgres 9.6 introduces full-text phrase search capabilities. So now this works:
SELECT title FROM tbl WHERE title_tsv @@ to_tsquery('zend <-> fram:*');
<-> is the FOLLOWED BY statement.
It finds a "foo Zend framework bar" or "Zend frames", but not 'foo Zend has no frame.
Quote from the release note for Postgres 9.6:
A phrase search query can be specified in the tsquery input using the new <-> and < N > operators. The first means that tokens are before and after it should appear next to each other in that order. The latter means that they must be exactly N tokens.
For better performance support, a query with a GIN index:
CREATE INDEX tbl_title_tsv_idx ON tbl USING GIN (title_tsv);
Or do not store title_tsv in a table at all (bloating and writing difficulty). Instead, you can use the expression index:
CREATE INDEX tbl_title_tsv_idx ON tbl USING GIN (to_tsvector('english', title));
You need to specify the text search configuration (often language dependent) to make the expression unchanged. And adapt the request accordingly:
... WHERE to_tsvector('english', title) @@ to_tsquery('english', 'zend <-> fram:*');
source share