pg_search icon indicating copy to clipboard operation
pg_search copied to clipboard

Question about searching against multiple attributes with highlight

Open zosiu opened this issue 8 years ago • 7 comments

I have the following pg_search_scope on my model:

pg_search_scope :search_full_text,
                against: { title: 'A',
                           description: 'B' },
                using: { tsearch: { dictionary: 'english',
                                    prefix: true,
                                    any_word: true,
                                    tsvector_column: 'tsv',
                                    highlight: { start_sel: '<strong>',
                                                 stop_sel: '</strong>' } } }

I would like to return the full title and description, with both of them including the highlights, is this possible?

zosiu avatar Nov 05 '16 05:11 zosiu

I would also like to see there to be pg_search_#{field}_highlight equivalent for each field as opposed to highlighting them all after concatenation.

Is there a way to do this right now ?

dhruvasagar avatar Dec 19 '16 06:12 dhruvasagar

This is still an issue :(

kwent avatar Apr 11 '18 04:04 kwent

Still and issue

jcostello avatar Jul 02 '19 20:07 jcostello

This would be very helpful. Is there a recommended workaround for searching multiple columns and then displaying results as individual highlighted fields?

jbakerdev avatar Oct 31 '19 20:10 jbakerdev

Any updates on this?

andrepcg avatar Jul 25 '20 16:07 andrepcg

I think this is due to the fact that it's using a tsvector column, so the highlight is constructed on such column, as pg_search is not actually marking highlights on the other two columns, they're not present in the query at all.

If you examine the SQL query, you'll see something akin to:

SELECT ((ts_headline('simple', (coalesce("articles"."tsv"::text, '')), (to_tsquery('simple', ''' ' || unaccent('test') || ' ''' || ':*')), 'StartSel = "<mark>", StopSel = "</mark>", MaxFragments = 2'))) AS pg_search_highlight,
       articles.*
FROM "articles"
INNER JOIN
    (SELECT "articles"."id" AS pg_search_id,
            (ts_rank(("articles"."tsv"), (to_tsquery('simple', ''' ' || unaccent('test') || ' ''' || ':*')), 0)) AS rank
     FROM "articles"
     WHERE (("articles"."tsv") @@ (to_tsquery('simple', ''' ' || unaccent('test') || ' ''' || ':*')))) AS pg_search_906c7ff47cd7c682246c27 ON "articles"."id" = pg_search_906c7ff47cd7c682246c27.pg_search_id
ORDER BY pg_search_906c7ff47cd7c682246c27.rank DESC,
         "articles"."id" ASC
LIMIT 10

rhymes avatar Apr 27 '21 09:04 rhymes

Seeing that this is still an issue in 2023, I have made a pg_search_multiple_highlight gem that is supposed to solve the issue of multiple highlighting. Please give it a try

msuliq avatar Nov 29 '23 04:11 msuliq