Search API returns QueryError when Query contains ','(Comma) or = or ()

full-text-search, google-app-engine, python

Solution

To parse a phrase, use quotes around the phrase:

query = '"Engines (Modular)"'
search.Index(name=_INDEX_NAME).search(query)

You can simply put quotes around an existing query:

query = '"{0}"'.format(query)
search.Index(name=_INDEX_NAME).search(query)

but you probably have to remove existing quotes in the query to make this work. Google's documentation is silent on how to include quotes in your search queries. So the complete, fail-safe method would be:

query = '"{0}"'.format(query.replace('"', ''))
search.Index(name=_INDEX_NAME).search(query)

Problem

Using app engine's search API, I tried to search the queries which contain `,`, `=`, `(`, etc. It returns the following error: ``` Failed to parse query "engines (Modular)" Traceback (most recent call last): File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__ rv = self.router.dispatch(request, response) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher return route.handler_adapter(request, response) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__ return handler.dispatch() File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch return self.handle_exception(e, self.app.debug) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch return method(*args, **kwargs) File "/base/data/home/apps/s~generatestock/12.362076640167792770/search.py", line 1641, in get result = find_search_document(search_item) File "/base/data/home/apps/s~generatestock/12.362076640167792770/search.py", line 177, in find_search_document return search.Index(name=_INDEX_NAME).search(query) File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 2715, in search query = Query(query_string=query) File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 2286, in __init__ _CheckQuery(self._query_string) File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 1964, in _CheckQuery raise QueryError('Failed to parse query "%s"' % query) QueryError: Failed to parse query "Engines (Modular)" ``` Why? Does the search API support these characters?

Original source