Checking is TSearch2 query is valid

Started by Benjamin Araiover 18 years ago8 messagesgeneral
Jump to latest
#1Benjamin Arai
me@benjaminarai.com

Is there a way to pass a query to PostgreSQL to check if the
TSeasrch2 search text is valid? For example,

SELECT to_tsquery('default', '!');

returns an error. I want to know if there is a way get true/false
for the '!' portion of the query?

Benjamin

#2Oleg Bartunov
oleg@sai.msu.su
In reply to: Benjamin Arai (#1)
Re: Checking is TSearch2 query is valid

There are two useful functions - numnode() and querytree()
More details in http://www.sai.msu.su/~megera/postgres/fts/doc/fts-query.html

Oleg
On Sat, 8 Sep 2007, Benjamin Arai wrote:

Is there a way to pass a query to PostgreSQL to check if the TSeasrch2 search
text is valid? For example,

SELECT to_tsquery('default', '!');

returns an error. I want to know if there is a way get true/false for the
'!' portion of the query?

Benjamin

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Regards,
Oleg
_____________________________________________________________
Oleg Bartunov, Research Scientist, Head of AstroNet (www.astronet.ru),
Sternberg Astronomical Institute, Moscow University, Russia
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(495)939-16-83, +007(495)939-23-83

#3Benjamin Arai
me@benjaminarai.com
In reply to: Oleg Bartunov (#2)
Re: Checking is TSearch2 query is valid

That is helpful but these functions to do help me detect errors in
queries such as "(moose & frog" where the left parentheses is
missing. I may just have to write a lexical analyzer.

Benjamin

On Sep 8, 2007, at 10:45 PM, Oleg Bartunov wrote:

Show quoted text

There are two useful functions - numnode() and querytree()
More details in http://www.sai.msu.su/~megera/postgres/fts/doc/fts-
query.html

Oleg
On Sat, 8 Sep 2007, Benjamin Arai wrote:

Is there a way to pass a query to PostgreSQL to check if the
TSeasrch2 search text is valid? For example,

SELECT to_tsquery('default', '!');

returns an error. I want to know if there is a way get true/
false for the '!' portion of the query?

Benjamin

---------------------------(end of
broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Regards,
Oleg
_____________________________________________________________
Oleg Bartunov, Research Scientist, Head of AstroNet (www.astronet.ru),
Sternberg Astronomical Institute, Moscow University, Russia
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(495)939-16-83, +007(495)939-23-83

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Benjamin Arai (#1)
Re: Checking is TSearch2 query is valid

Benjamin Arai <me@benjaminarai.com> writes:

Is there a way to pass a query to PostgreSQL to check if the
TSeasrch2 search text is valid? For example,
SELECT to_tsquery('default', '!');
returns an error. I want to know if there is a way get true/false
for the '!' portion of the query?

The generic solution to this type of problem is to write a function that
tries to do whatever-it-is-that-throws-an-error inside a plpgsql
BEGIN/EXCEPTION block, and catch the errors you are expecting.

regards, tom lane

#5Benjamin Arai
me@benjaminarai.com
In reply to: Tom Lane (#4)
Re: Checking is TSearch2 query is valid

Ok, this appears to have worked but I have to check for exception
code "OTHERS" because I could not figure out what the actual code
being thrown was. Is there a specific exception code for:

ERROR: no operand in tsearch query: "("

Thanks for the help!

Benjamin

On Sep 9, 2007, at 7:54 AM, Tom Lane wrote:

Show quoted text

Benjamin Arai <me@benjaminarai.com> writes:

Is there a way to pass a query to PostgreSQL to check if the
TSeasrch2 search text is valid? For example,
SELECT to_tsquery('default', '!');
returns an error. I want to know if there is a way get true/false
for the '!' portion of the query?

The generic solution to this type of problem is to write a function
that
tries to do whatever-it-is-that-throws-an-error inside a plpgsql
BEGIN/EXCEPTION block, and catch the errors you are expecting.

regards, tom lane

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Benjamin Arai (#5)
Re: Checking is TSearch2 query is valid

Benjamin Arai <me@benjaminarai.com> writes:

Is there a specific exception code for:
ERROR: no operand in tsearch query: "("

regression=# \set VERBOSITY verbose
regression=# select to_tsquery('(');
ERROR: 42601: no operand in tsearch query: "("
LOCATION: gettoken_query, tsquery.c:163

Seems to be "SYNTAX_ERROR".

regards, tom lane

#7Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#6)
Re: Checking is TSearch2 query is valid

Tom Lane wrote:

Benjamin Arai <me@benjaminarai.com> writes:

Is there a specific exception code for:
ERROR: no operand in tsearch query: "("

regression=# \set VERBOSITY verbose
regression=# select to_tsquery('(');
ERROR: 42601: no operand in tsearch query: "("
LOCATION: gettoken_query, tsquery.c:163

Seems to be "SYNTAX_ERROR".

Hmm, maybe we should be displaying the textual name of the SQLSTATE
somehow.

--
Alvaro Herrera http://www.amazon.com/gp/registry/5ZYLFMCVHXC
"El d�a que dejes de cambiar dejar�s de vivir"

#8Bruce Momjian
bruce@momjian.us
In reply to: Alvaro Herrera (#7)
Re: Checking is TSearch2 query is valid

Add psql TODO:

o Include the symbolic SQLSTATE name in verbose error reports

http://archives.postgresql.org/pgsql-general/2007-09/msg00438.php

---------------------------------------------------------------------------

Alvaro Herrera wrote:

Tom Lane wrote:

Benjamin Arai <me@benjaminarai.com> writes:

Is there a specific exception code for:
ERROR: no operand in tsearch query: "("

regression=# \set VERBOSITY verbose
regression=# select to_tsquery('(');
ERROR: 42601: no operand in tsearch query: "("
LOCATION: gettoken_query, tsquery.c:163

Seems to be "SYNTAX_ERROR".

Hmm, maybe we should be displaying the textual name of the SQLSTATE
somehow.

--
Alvaro Herrera http://www.amazon.com/gp/registry/5ZYLFMCVHXC
"El d?a que dejes de cambiar dejar?s de vivir"

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://postgres.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +