PSQLException: ERROR: syntax error in tsquery

北战南征 提交于 2020-01-12 04:33:06

问题


Which characters must be avoided to make sure PSQLException: ERROR: syntax error in tsquery will not occur? The documentation does not say anything about how to escape the search string: http://www.postgresql.org/docs/8.3/static/datatype-textsearch.html


回答1:


Use quotes around your terms if you want them as phrases/verbatim or they contain characters used in the syntax:

select to_tsquery('"hello there" | hi');

Bare in mind that you shouldn't really have crazy characters in your terms, since they are not going to match anything in the tsvector.

The (non-token) characters recognized by the tsquery parser are: \0 (null), (, ), (whitespace), |, &, :, * and !. But how you tokenize your query should be based on how you have setup your dictionary. There are a great many other characters that you will likely not want in your query, not because they will cause a syntax error but because it means you are not tokenizing your query correctly.

Use the plainto_tsquery version if it's a simple AND query and you don't want to deal with creating the query manually.



来源:https://stackoverflow.com/questions/16020164/psqlexception-error-syntax-error-in-tsquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!