Issue: PostgreSQL Full text search failing on multiple words and/or special characters
Author: ZucchiniZe
State: closed
Created: 2016-05-15 06:30:00

I am trying to use the full text search feature offered by peewee and when I run the `Match()` function on something that contains special characters or spaces it fails and give the error `peewee.ProgrammingError: syntax error in tsquery:`. Looking through the traceback it looks like the %s is not getting quoted when running the SQL causing it to fail when executing.

here is the full traceback

``````
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/alex/code/personal/marvel/marvel.py", line 83, in show_search
    return render_template('show_search.html', totals=(tcount, icount), results=query, search=True)
  File "/usr/lib/python3.4/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/usr/lib/python3.4/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/usr/lib/python3.4/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python3.4/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/home/alex/code/personal/marvel/templates/show_search.html", line 1, in top-level template code
    {% extends "layout.html" %}
  File "/home/alex/code/personal/marvel/templates/layout.html", line 16, in top-level template code
    {% block body %}{% endblock %}
  File "/home/alex/code/personal/marvel/templates/show_search.html", line 8, in block "body"
    {% if results %}
  File "/usr/lib/python3.4/site-packages/peewee.py", line 2984, in __len__
    return len(self.execute())
  File "/usr/lib/python3.4/site-packages/peewee.py", line 2960, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/usr/lib/python3.4/site-packages/peewee.py", line 2656, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 380, in execute_sql
    self.commit()
  File "/usr/lib/python3.4/site-packages/peewee.py", line 3316, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/lib/python3.4/site-packages/peewee.py", line 127, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3.4/site-packages/playhouse/postgres_ext.py", line 373, in execute_sql
    cursor.execute(sql, params or ())
peewee.ProgrammingError: syntax error in tsquery: "amazing spider-man"```
``````


Sorry, after some debugging I found out that you have to do some weird quoting on the variables or use `plainto_tsquery` for it to recognize spacing. my bad


http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#pg-fts

The docs say:

> The [Match()](http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#Match) function will automatically convert the left-hand operand to a tsvector, and the right-hand operand to a tsquery. For better performance, it is recommended you create a GIN index on the column you plan to search.

Did you see that in the docs?