-->

部分匹配GAE搜索API(Partial matching GAE search API)

2019-06-18 12:38发布

使用GAE搜索API是它可以搜索部分匹配?

我试图创建自动填充功能,其中术语将是一个局部的词。 例如。

> b
> BUI
>构建

将全部回归“建筑”。

这怎么可能与GAE?

Answer 1:

虽然LIKE语句(部分匹配)未在全文检索的支持,但你可以围绕它破解。

首先,令牌化所有可能的子数据串(你好= H,他,HEL,卤味等)

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)

执行搜索和walah!

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

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



Answer 2:

就像@Desmond Lua的答案,但有不同的记号化功能:

def tokenize(word):
  token=[]
  words = word.split(' ')
  for word in words:
    for i in range(len(word)):
      if i==0: continue
      w = word[i]
      if i==1: 
        token+=[word[0]+w]
        continue

      token+=[token[-1:][0]+w]

  return ",".join(token)

它将解析hello worldhe,hel,hell,hello,wo,wor,worl,world

它的良好的光线自动完成目的



Answer 3:

截至描述的全文搜索和LIKE语句 ,不,这不是可能的,因为搜索API实现全文索引。

希望这可以帮助!



Answer 4:

我有预输入控制同样的问题,我的解决方法是解析字符串小部分:

name='hello world'
name_search = ' '.join([name[:i] for i in xrange(2, len(name)+1)])
print name_search;
# -> he hel hell hello hello  hello w hello wo hello wor hello worl hello world

希望这有助于



Answer 5:

我的版本优化:不重复令牌

def tokenization(text):
    a = []
    min = 3
    words = text.split()
    for word in words:
        if len(word) > min:
            for i in range(min, len(word)):
                token = word[0:i]
                if token not in a:
                    a.append(token)
    return a


文章来源: Partial matching GAE search API