在Python版本的GAE搜索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
索引中执行搜索,并按以下优先级返回结果,其中要搜索的短语在变量qs
中:
- 标题与
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.