Google App Engine(python):搜索API:字符串搜索 [英] Google App Engine (python) : Search API : String Search

查看:149
本文介绍了Google App Engine(python):搜索API:字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google App Engine搜索API( https://developers.google.com/appengine/文档/蟒蛇/搜索/ )。我索引了所有的实体,搜索工作正常。但只有当我搜索完全匹配时才返回0结果。例如:

  from google.appengine.api导入搜索

_INDEX_NAME ='searchall'

$ b $ query_string =United Kingdom
query = search.Query(query_string = query_string)
index = search.Index(name = _INDEX_NAME)

print index.search(query)

如果我运行以下脚本,我会得到如下结果:

  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 KinUnited 它返回0结果如下:

  search.SearchResults(number_found ='0')

我想将此API用于普通搜索和AutoSuggest。什么是最好的方式来实现这一目标?

解决方案

App Engine的全文搜索API不支持子字符串匹配。 >

但是,我自己需要这种行为来支持用户输入的搜索建议。这是我的解决方案:

接受一个句子并返回全部集合
例如,hello world变成he hell hell hell hello w wo wor worl world
def build_suggestions(str):
suggestions = []
for word in str.split():
prefix =
for word in word:
prefix + = letter
suggestions.append(前缀)
return''.join(建议)

#示例使用
document = search.Document(
fields = [search.TextField(name ='name',value = object_name),
search.TextField(name ='suggest',value = build_suggestions(object_name))])

基本思路是为每个可能的子字符串手动生成单独的关键字。这只适用于简短的句子,但它适用于我的目的。


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')

but if i change the query_string to "United Kin" or "United" it return 0 results as follows:

search.SearchResults(number_found='0')

I want to use this API for both normal search and AutoSuggest. What would be the best way to achieve this ?

解决方案

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.

这篇关于Google App Engine(python):搜索API:字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆