lovasoa / SQLpage

SQL-only webapp builder, empowering data analysts to build websites and applications quickly
https://sql.ophir.dev
MIT License
882 stars 62 forks source link

highlighting the search term FTS PostgreSQL #358

Closed amrutadotorg closed 1 month ago

amrutadotorg commented 1 month ago

Hi, is there a way to highlight the words using FTS in PostgreSQL?

thank you

Screenshot 2024-05-31 at 11 43 46


SELECT 
    'text' AS component;

SELECT post.id, 
       ts_headline('english', post.content, plainto_tsquery('english', $search), 'StartSel=<mark>, StopSel=</mark>') AS contents,
       translations.language_code
FROM post
JOIN translations ON translations.element_id = post.id
  AND translations.element_type = 'post_post'
  AND translations.language_code = 'en'
WHERE post.content_tsv @@ plainto_tsquery('english', $search);
lovasoa commented 1 month ago

You can use the StartSel and StopSel options of the ts_headline function to make postgres return search results highlighted in markdown, and then use the description_md row-level parameter to display the resulting markdown text.

https://www.postgresql.org/docs/current/textsearch-controls.html

amrutadotorg commented 1 month ago

Thank you. I think I was not precise enough. I would like to use a color for highlighting.

lovasoa commented 1 month ago

That is not possible with the built-in components, but you have two possibilities:

amrutadotorg commented 1 month ago

Thank you for the ideas.