gpt4 book ai didi

python - 部分匹配GAE搜索API

转载 作者:太空狗 更新时间:2023-10-29 17:22:33 27 4
gpt4 key购买 nike

使用 GAE search API是否可以搜索部分匹配项?

我正在尝试创建自动完成功能,其中术语是部分单词。例如。

> b
> bui
> build

都会返回“building”。

GAE 如何做到这一点?

最佳答案

虽然全文搜索不支持 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

使用标记化字符串构建索引 + 文档(搜索 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)

执行搜索,哇哦!

results = search.Index(name="item_autocomplete").search("name:elo")

https://code.luasoftware.com/tutorials/google-app-engine/partial-search-on-gae-with-search-api/

关于python - 部分匹配GAE搜索API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12899083/

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