Google App Engine (python):搜索 API:字符串搜索
我正在使用 Google App Engine Search API (https://developers.google.com/appengine/docs/python/search/).我已经索引了所有实体并且搜索工作正常.但只有当我搜索完全匹配时,它才会返回 0 个结果.例如:
i am using the Google App Engine Search API (https://developers.google.com/appengine/docs/python/search/). I have indexed all of the entities and the search is working fine. but only if i search for the exact matches else it returns 0 results. For Example:
from google.appengine.api import search
_INDEX_NAME = 'searchall'
query_string ="United Kingdom"
query = search.Query(query_string=query_string)
index = search.Index(name=_INDEX_NAME)
print index.search(query)
如果我运行以下脚本,我会得到如下结果:
if i run the following script i do get results as follows :
search.SearchResults(results='[search.ScoredDocument(doc_id='c475fd24-34ba-42bd-a3b5-d9a48d880012', fields='[search.TextField(name='name', value='United Kingdom')]', language='en', order_id='45395666'), search.ScoredDocument(doc_id='5fa757d1-05bf-4012-93ff-79dd4b77a878', fields='[search.TextField(name='name', value='United Kingdom')]', language='en', order_id='45395201')]', number_found='2')
但是如果我将 query_string
更改为 "United Kin"
或 "United"
它返回 0 结果如下:
but if i change the query_string
to "United Kin"
or "United"
it return 0 results as follows:
search.SearchResults(number_found='0')
我想将此 API 用于普通搜索和 AutoSuggest.实现这一目标的最佳方法是什么?
I want to use this API for both normal search and AutoSuggest. What would be the best way to achieve this ?
App Engine 的全文搜索 API 不支持子字符串匹配.
App Engine's full text search API does not support substring matching.
但是,我自己需要这种行为来支持用户键入时的搜索建议.这是我的解决方案:
However, I needed this behavior myself to support search suggestions as the user types. Here is my solution for that:
""" Takes a sentence and returns the set of all possible prefixes for each word.
For instance "hello world" becomes "h he hel hell hello w wo wor worl world" """
def build_suggestions(str):
suggestions = []
for word in str.split():
prefix = ""
for letter in word:
prefix += letter
suggestions.append(prefix)
return ' '.join(suggestions)
# Example use
document = search.Document(
fields=[search.TextField(name='name', value=object_name),
search.TextField(name='suggest', value=build_suggestions(object_name))])
基本思想是为每个可能的子字符串手动生成单独的关键字.这仅适用于短句,但对我的目的非常有用.
The basic idea is to manually generate separate keywords for every possible substring. This is only practical for short sentences, but it works great for my purposes.