syntax error in tsquery : parameter with whitespaces
Asked Answered
J

3

20

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

Joviality answered 27/4, 2017 at 23:9 Comment(0)
J
30

I found how to solve it. Here it goes

Replace: to_tsquery(psearch)

with: plainto_tsquery(psearch)

Joviality answered 27/4, 2017 at 23:15 Comment(2)
If you are wondering, why: to_tsquery creates a tsquery value from querytext, which must consist of single tokens separated by the tsquery operators & (AND), | (OR), ! (NOT), and <-> (FOLLOWED BY), possibly grouped using parentheses. In other words, the input to to_tsquery must already follow the general rules for tsquery inputCero
Note that it will AND all the tokens.Chlorous
K
3

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

Karinekariotta answered 13/7, 2017 at 18:42 Comment(0)
C
0

Another option is to use arbitrary operator to join tokens:

select to_tsquery('simple', 'نون & خ')

'نون' & 'خ'

or:

select to_tsquery('simple', 'نون | خ')

'نون' | 'خ'
Chlorous answered 28/3, 2023 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.