pg_search
pg_search copied to clipboard
Question about searching against multiple attributes with highlight
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?
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 ?
This is still an issue :(
Still and issue
This would be very helpful. Is there a recommended workaround for searching multiple columns and then displaying results as individual highlighted fields?
Any updates on this?
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
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