8

I have just stumbled over a weird behaviour in postgres ts_headline function (used for highlighting fulltext search results). First I thought the simple dictionary does not play with highlighting as the below example shows (there should be <b> tags):

# SELECT ts_headline('simple', 'This is artificial text', to_tsquery('artificial'));
       ts_headline       
-------------------------
 This is artificial text
(1 row)

But a different word works fine...

# SELECT ts_headline('simple', 'some Word in', to_tsquery('Word'));
 ts_headline     
---------------------
 some <b>Word</b> in
(1 row)

Does anybody have an explanation for this behaviour?

1 Answer 1

8

I noticed my mistake. The to_tsquery function inside also needs to know which dictionary to use (aka use the simple one). By default it uses english stemming which produces this result:

SELECT to_tsquery('artificial');
 to_tsquery 
------------
 'artifici'

which of course can not be found in the text converted by simple dictionary. So the correct query would have been:

# SELECT ts_headline('simple', 'This is artificial text', to_tsquery('simple', 'artificial'));
          ts_headline           
--------------------------------
 This is <b>artificial</b> text
(1 row)
2
  • 1
    Good catch. Always set the regconfig. Most tutorials neglect this sadly Commented Jun 12, 2017 at 23:32
  • 1
    I just stumbled across this, but in reverse: the to_tsquery calls were set to a custom dictionary but the ts_headline call didn't set the same and I didn't realize it wouldn't inherit that value. Make sure to provide a consistent dictionary definition on each call.
    – bsplosion
    Commented Feb 17, 2023 at 16:13

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.