Highlight search result match? #100

cmalpeli opened this Issue Mar 31, 2013 · 3 comments


3 participants


Is there a way to highlight matches in the search results returned by pg_search?



nertzy commented Mar 31, 2013

Right now, there isn't a quick way to do this. It's on my list of things I'd like to get to eventually.

For anyone who is more advanced in PostgreSQL and wants to attempt a pull request, there is a function called ts_headline that provides an excerpt.


Here's my (probably in need of optimization) query:

delimiter = "<br>"
@results = PgSearch.multisearch(@query).select("ts_headline(pg_search_documents.content, plainto_tsquery('english', ''' ' || unaccent('#{@query}') || ' ''' || ':*'),  'MaxFragments=3, MinWords=10, MaxWords=12, FragmentDelimiter=\" ...#{delimiter} \", StartSel=\"<em class=highlight>\", StopSel=\"</em>\"') AS excerpt")

Can't take credit, I found the query I based mine off of here: http://stackoverflow.com/questions/11140339/how-to-show-excerpts-from-pg-search-multisearch-results

What I would love is even more control over the results so that I can prepend an ellipsis if the results content was found in the middle of a content area, and no ellipsis if it's at the beginning.


nertzy commented Dec 21, 2016

Highlights are now present in the latest versions. See the README.

nertzy closed this Dec 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment