3

ts_headline() does not seem to highlight the input text in a way that respects the <-> operator.

The following query:

SELECT ts_headline('English','This Commercial Bank does not have any Equity in Europe but European Commercial Bank does', 
                    phraseto_tsquery('English','European Commercial Bank')::tsquery);

returns the following as output:

This <b>Commercial</b> <b>Bank</b> does not have any Equity in Europe but <b>European</b> <b>Commercial</b> <b>Bank</b> does

This highlights the words Commercial and Bank separately in addition to the phrase European Commercial Bank.

The expected output would be:

This Commercial Bank does not have any Equity in Europe but <b>European</b> <b>Commercial</b> <b>Bank</b> does

Which only highlights European Commercial Bank due to the <-> operators inserted by phraseto_tsquery().

I am using Postgres 10.3 on Linux x64. Any suggestions or solutions to this problem?

I reported a bug.

1
  • 1
    I think that's a shortcoming of ts_headline(), which has not been extended to cover the new phrase search in pg 9.6. Ah, I see you filed a bug report - which should be mentioned in your question: postgresql.org/message-id/flat/… Commented Apr 25, 2018 at 2:34

1 Answer 1

1

I am writing an extension that improves the ts_headline functionality to correctly highlight matching phrases with a single tag, while not highlighting partial matches. The extension in available at https://github.com/thevermeer/pg_ts_semantic_headline and is intended to replace ts_headline directly.

Usage:

SELECT ts_semantic_headline('English','This Commercial Bank does not have any Equity in Europe but European Commercial Bank does', 
                    phraseto_tsquery('English','European Commercial Bank')::tsquery);

which produces:

ts_semantic_headline
This Commercial Bank does not have any Equity in Europe but <b>European Commercial Bank</b> does

The ts_semantic_headline solution uses ts_headline under the hood to produce content fragments. It then uses text parsing and customized TSVectors, along with the included ts_fast_headline function to perform multi-word highlighting with minimal (5-10%) performance cost on top of ts_headline.

If performance is of any concern, the ts_fast_headline function can also use 2 pre-processed columns (TSPVector + TEXT[]), and deliver highlighted content 5X-10X faster than ts_headline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.