2

I am using PostgreSQL 10. I have a "booktbl" table which has 4

book_name
paragraph_title
content
ts_content

ts_content column is a tsvector column. When Iexecute this query:

select
    book_name , 
    paragraph_title ts_headline(content,to_tsquery('english','happy'), 'HighlightAll=true MaxFragments=100 FragmentDelimiter=$')
from bookstbl 
where ts_content @@ (to_tsquery('happy')) ;

I got a single row as a result which has a "ts_headline" column with all highlighted text search fragments. I want to convert all these fragments to individual rows. Like this:

book    para_title1     fragment1
book    para_title2     fragment2

How can I achieve this in PostgreSQL full text search?

1 Answer 1

0

One could use regexp_split_to_table, splitting on the FragmentDelimiter, like so:

REGEXP_SPLIT_TO_TABLE(
  ts_headline(content,
              to_tsquery('english','happy'), 
              'HighlightAll=true MaxFragments=100 FragmentDelimiter=$'), 
  '$')

Assembled into a query:

SELECT
    REGEXP_SPLIT_TO_TABLE(
        ts_headline(content,
                    to_tsquery('english','happy'), 
                    'HighlightAll=true MaxFragments=100 FragmentDelimiter=$'), 
        '$')
from bookstbl 
where ts_content @@ (to_tsquery('happy')) ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.