gpt4 book ai didi

python - 当 Query 包含 ','(逗号)或 = 或 () 时,搜索 API 返回 QueryError

转载 作者:太空狗 更新时间:2023-10-30 00:56:59 24 4
gpt4 key购买 nike

使用应用引擎的搜索 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 是否支持这些字符?

最佳答案

要解析短语,请在短语周围使用引号:

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)

关于python - 当 Query 包含 ','(逗号)或 = 或 () 时,搜索 API 返回 QueryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com