bra*_*ley 13 python search google-app-engine autocomplete webapp2
使用GAE搜索API可以搜索部分匹配吗?
我正在尝试创建自动完成功能,其中该术语将是一个部分词.例如.
> b
> bui
> build
将全部归还"建筑".
如何用GAE实现这一目标?
Des*_*Lua 31
虽然全文搜索不支持LIKE语句(部分匹配),但您可以解决它.
首先,为所有可能的子串标记数据字符串(hello = h,he,hel,lo等)
def tokenize_autocomplete(phrase):
a = []
for word in phrase.split():
j = 1
while True:
for i in range(len(word) - j + 1):
a.append(word[i:i + j])
if j == len(word):
break
j += 1
return a
Run Code Online (Sandbox Code Playgroud)
使用标记化字符串构建索引+文档(Search API)
index = search.Index(name='item_autocomplete')
for item in items: # item = ndb.model
name = ','.join(tokenize_autocomplete(item.name))
document = search.Document(
doc_id=item.key.urlsafe(),
fields=[search.TextField(name='name', value=name)])
index.put(document)
Run Code Online (Sandbox Code Playgroud)
执行搜索,并执行!
results = search.Index(name="item_autocomplete").search("name:elo")
Run Code Online (Sandbox Code Playgroud)
https://code.luasoftware.com/tutorials/google-app-engine/partial-search-on-gae-with-search-api/
| 归档时间: |
|
| 查看次数: |
6037 次 |
| 最近记录: |