Search code examples
postgresqlfull-text-searchsearch-enginehighlight

highlighting search results using Postgresql GIN/GiST


Is there a built in way of highlighting matched search keywords when using Postgresql's GIN or GiST index? Or if not, a way of doing this outside Postgresql?

I'm using PHP 5.3.10 and Postgresql 9.1.3.

All ideas are appreciated.


Solution

  • Look on ts_headline function ts_headline.

    SELECT ts_headline('english',
      'The most common type of search
    is to find all documents containing given query terms
    and return them in order of their similarity to the
    query.',
      to_tsquery('query & similarity'));
                            ts_headline                         
    ------------------------------------------------------------
     containing given <b>query</b> terms
     and return them in order of their <b>similarity</b> to the
     <b>query</b>.