部分匹配GAE搜索API [英] Partial matching GAE search API
问题描述
使用 GAE搜索API 是它可以搜索部分匹配?
Using the GAE search API is it possible to search for a partial match?
我试图创建自动填充功能在该条款将是一个局部的词。例如:
I'm trying to create autocomplete functionality where the term would be a partial word. eg.
> B结果
>裴结果
>构建
> b
> bui
> build
都会回报的建设。
这是如何实现与GAE?
How is this possible with GAE?
推荐答案
虽然LIKE语句(部分匹配)未在全文检索的支持,但你可以围绕它破解。
Though LIKE statement (partial match) is not supported in Full Text Search, but you could hack around it.
首先,记号化的所有可能的子数据串(你好= H,他,HEL,卤味等)
First, tokenize the data string for all possible substrings (hello = h, he, hel, lo, etc.)
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)
Build an index + document (Search API) using the tokenized strings
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)
执行搜索,并walah!
Perform search, and walah!
results = search.Index(name="item_autocomplete").search("name:elo")
参考:的http://blog.startuptale.com/2012/10/partial-search-on-gae-with-search-api.html
这篇关于部分匹配GAE搜索API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!