Search code examples
sqlpostgresqlhighlight

ts_headline (PostgreSQL) behavior


I am using the ts_headline function for highlight search text.

so there are two scenarios:

In the first case, we use "&" operator in tsquery that means id any of the keywords come in the text it will highlight, which is working fine.

SELECT ts_headline(
    text,
    to_tsquery('apply & for & the & purposes'),
    'StartSel = <span>, StopSel = </span>,HighlightAll = true'
)

In the second case, we use "tsquery after tsquery (<->)" in tsquery that means if the exact string matches then it should highlight, but its working as the previous one.

SELECT ts_headline(
    text,
    to_tsquery('apply<->for<->the<->purposes'),
    'StartSel = <span>, StopSel = </span>,HighlightAll = true'
)

Solution

  • "In the second case, we use "tsquery after tsquery (<->)" in tsquery that means if the exact string matches then it should highlight"

    I assume you mean highlight only that phrase, and not words within the phrase but occurring elsewhere? While that maybe a reasonable thing to want, ts_headline does't promise to do that, at least now anywhere I can see in the docs. Or do you mean highlight even the stop words within the phrase? It doesn't promise to do that either, and the stop words are removed before ts_headline even sees them.

    You could try to come up with a patch to change it to do what you want, although I suspect it would be rejected for backwards compatibility reasons unless you created new flags to control it.