lucasgonze / Safeharbor.in

2 stars 0 forks source link

can't search with spaces #233

Closed lucasgonze closed 11 years ago

lucasgonze commented 11 years ago

See https://github.com/brianc/node-postgres/issues/212

I'm using the pg fulltext search feature. When I use a parameter value with a space in it, it is expanded incorrectly and a SQL error is thrown.

var sql = 
    "select * from mytable"
    + " where to_tsvector('english',haystack)"
    + " @@ to_tsquery('english',$1)";   

var query = getClient().query(sql,[params.needle]);

If params.needle has no spaces in it, it executes correctly. For example I can search on "a_b". But if it has a space, I get:

error: syntax error in tsquery: "a b"

I suspect that that's happening is that the argument is being expanded into multiple arguments, so that after substitution to_tsquery looks like to_tsquery('english','a','b').

lucasgonze commented 11 years ago

http://www.postgresql.org/docs/8.3/static/textsearch-controls.html

Without quotes, to_tsquery will generate a syntax error for tokens that are not separated by an AND or OR operator.

     plainto_tsquery([ config regconfig, ] querytext text) returns tsquery

plainto_tsquery transforms unformatted text querytext to tsquery. The text is parsed and normalized much as for to_tsvector, then the & (AND) Boolean operator is inserted between surviving words.

Example:

 SELECT plainto_tsquery('english', 'The Fat Rats');
 plainto_tsquery 
-----------------
 'fat' & 'rat'
Note that plainto_tsquery cannot recognize either Boolean operators or weight labels in its input