Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ts_headline to respect phraseto_tsquery

Tags:

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.

like image 329
stefanfoulis Avatar asked Mar 26 '18 16:03

stefanfoulis


People also ask

How to_ tsvector works?

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.

What PostgreSQL operator checks to see if a Tsquery matches a Tsvector?

@@ 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');

Is Elasticsearch faster than Postgres?

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.

How do I search for a Postgres database?

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.


1 Answers

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.

like image 184
Dipen Shah Avatar answered Sep 21 '22 12:09

Dipen Shah