11

Google App Engine Search API(https://developers.google.com/appengine/docs/python/search/)を使用しています。すべてのエンティティにインデックスを付けましたが、検索は正常に機能しています。ただし、完全に一致するものを検索した場合にのみ、0の結果が返されます。例えば:

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)

次のスクリプトを実行すると、次のような結果が得られます。

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 Kin""United"、次のように0の結果を返す場合:

search.SearchResults(number_found='0')

このAPIを通常の検索とオートサジェストの両方に使用したいと思います。これを達成するための最良の方法は何でしょうか?

4

1 に答える 1

18

App Engineの全文検索APIは、部分文字列のマッチングをサポートしていません。

ただし、ユーザーが入力するときに検索候補をサポートするには、この動作を自分で行う必要がありました。これが私の解決策です:

""" 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))])

基本的な考え方は、考えられるすべての部分文字列に対して個別のキーワードを手動で生成することです。これは短い文章でのみ実用的ですが、私の目的には最適です。

于 2012-06-09T15:25:59.393 に答える