Google App Engine搜索API
在GAE Search
API的Python版本中查询搜索索引时,最好的搜索方式是:首先搜索单词与标题匹配的文档,然后再搜索与正文匹配的文档的项目?
例如给出:
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.