谷歌应用引擎搜索API

6
在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中:
  1. 标题与qs匹配的文档;然后
  2. 正文匹配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.
1个回答

3

自定义评分是我们优先考虑的功能请求之一。我们希望尽快找到一种好的方式来实现这种功能。

在您的情况下,您当然可以通过进行两个单独的查询来达到所需的结果:第一个查询针对“标题”进行字段限制,第二个查询则针对“正文”进行限制。


谢谢Alan。我考虑过你建议的双重查询,但是接下来就必须要对结果进行交集操作(以避免重复),并且跟踪游标会变得有点棘手。我希望我忽略了一些简单的东西。在一个好的解决方法被找到之前,我会把这个答案作为正确的答案。干杯! - Brian M. Hunt
1
@Alan - 嗨,有关于这个的任何消息吗? - Bogdan Goie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接