使用应用引擎的搜索API,我尝试搜索包含,
,=
,(
等的查询。
它返回以下错误:
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)"
为什么呢?搜索API是否支持这些字符?
答案 0 :(得分:6)
要解析短语,请在短语周围使用引号:
query = '"Engines (Modular)"'
search.Index(name=_INDEX_NAME).search(query)
您可以简单地在现有查询周围加上引号:
query = '"{0}"'.format(query)
search.Index(name=_INDEX_NAME).search(query)
但您可能必须删除查询中的现有引号才能使其正常工作。 Google的文档没有说明如何在搜索查询中包含引号。因此,完整的,故障安全方法将是:
query = '"{0}"'.format(query.replace('"', ''))
search.Index(name=_INDEX_NAME).search(query)