I have a query that uses phrase search to match whole phrases.
SELECT ts_headline(
'simple',
'This is my test text. My test text has many words. Well, not THAT many words.',
phraseto_tsquery('simple', 'text has many words')
);
Which results in:
This is my test <b>text</b>. My test <b>text</b> <b>has</b> <b>many</b> <b>words</b>. Well, not THAT <b>many</b> <b>words</b>.
But I would have expected this:
This is my test text. My test <b>text</b> <b>has</b> <b>many</b> <b>words</b>. Well, not THAT many words.
Or ideally even this:
This is my test text. My test <b>text has many words</b>. Well, not THAT many words.
Sidenote:
phraseto_tsquery('simple', 'text has many words')
is equivalent to
to_tsquery('simple', 'text <-> has <-> many <-> words')
I'm not sure if I'm doing something wrong, or if ts_headline simply does not support this kind of highlighting.
The to_tsvector function internally calls a parser which breaks the document text into tokens and assigns a type to each token. For each token, a list of dictionaries (Section 12.6) is consulted, where the list can vary depending on the token type.
@@ operator checks if tsquery matches tsvector. For instance, if the word to be queried is “fox” for the above-mentioned example, then: SELECT to_tsvector('The quick brown fox jumped over the lazy dog') @@ to_tsquery('fox');
And the more size you want to search in, the more Elasticsearch is better than PostgreSQL in performance. Additionally, you could also get many benefits and great performance if you pre-process the posts into several fields and indexes well before storing into Elasticsearch.
In PostgreSQL, you use two functions to perform Full Text Search. They are to_tsvector() and to_tsquery(). Let's see how they work and to use them first. to_tsvector() function breaks up the input string and creates tokens out of it, which is then used to perform Full Text Search using the to_tsquery() function.
phraseto_tsquery('simple', 'text has many words')
generates correct query but it seems the problem is in ts_headline
function. Seems like an already reported BUG #155172.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With