Python バージョンのGAE Search APIで検索インデックスを照会する場合、最初に単語を含むドキュメントがタイトルと一致するアイテムを検索し、次に単語が本文と一致するドキュメントを検索するためのベスト プラクティスは何ですか?
たとえば、次のようになります。
body = """This is the body of the document,
with a set of words"""
my_document = search.Document(
fields=[
search.TextField(name='title', value='A Set Of Words'),
search.TextField(name='body', value=body),
])
可能であればDocument
、上記の形式の s のインデックスで検索を実行し、この優先順位で結果が返されるようにするにはどうすればよいでしょうか。ここで、検索対象のフレーズは変数内にありますqs
。
- ;に
title
一致するドキュメントqs
それから - 本文が
qs
単語と一致するドキュメント。
を使用するのが正しい解決策のようですが、MatchScorer
この検索機能を以前に使用したことがないため、これについては的外れかもしれません。の使用方法はドキュメントからは明らかではありませんがMatchScorer
、それをサブクラス化し、いくつかの関数をオーバーロードしていると思いますが、これはドキュメント化されておらず、コードを詳しく調べていないため、確かなことは言えません。
ここに何か欠けているものがありますか、それともこれは正しい戦略ですか? この種のことが文書化されている場所を見逃しましたか?
わかりやすくするために、望ましい結果のより精巧な例を次に示します。
documents = [
dict(title="Alpha", body="A"), # "Alpha"
dict(title="Beta", body="B Two"), # "Beta"
dict(title="Alpha Two", body="A"), # "Alpha2"
]
for doc in documents:
search.Document(
fields=[
search.TextField(name="title", value=doc.title),
search.TextField(name="body", value=doc.body),
]
)
index.put(doc) # for some search.Index
# Then when we search, we search the Title and Body.
index.search("Alpha")
# returns [Alpha, Alpha2]
# Results where the search is found in the Title are given higher weight.
index.search("Two")
# returns [Alpha2, Beta] -- note Alpha2 has 'Two' in the title.