Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error in tsquery : parameter with whitespaces

I have this Full-Text-Search function:

CREATE OR REPLACE FUNCTION search_questions(psearch text)
  RETURNS TABLE (questionid INTEGER) AS $func$
BEGIN
  return QUERY
  SELECT DISTINCT (questions.publicationid)
  FROM questions
  WHERE to_tsvector(coalesce(questions.title, '')) @@ to_tsquery(psearch)
        OR
        publicationid IN (
          SELECT DISTINCT(publications.publicationid) FROM publications WHERE to_tsvector(coalesce(publications.body, '')) @@ to_tsquery(psearch)
        )
  ;
END
$func$  LANGUAGE plpgsql;

but it only works with a single word parameter. If I search for "user test", it returns

ERROR: syntax error in tsquery: "user test"

Is there any way to search for texts with whitespaces in it?

Kind regards

like image 216
vftw Avatar asked Apr 27 '17 23:04

vftw


2 Answers

I found how to solve it. Here it goes

Replace: to_tsquery(psearch)

with: plainto_tsquery(psearch)

like image 163
vftw Avatar answered Oct 21 '22 18:10

vftw


You can put single quotes around your terms and still use to_tsquery().

like image 41
Kev Avatar answered Oct 21 '22 18:10

Kev