在GAE 搜索 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
正文与单词匹配的文档。
似乎正确的解决方案是使用 a 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.