【发布时间】:2014-10-14 12:30:41
【问题描述】:
我使用 Elasticsearch 推荐作者(我的 Elasticsearch 文档代表书籍,带有标题、摘要和作者 ID 列表)。
用户使用一些文本(例如Georgia 或Paris)查询我的索引,我需要在作者级别汇总每本书的分数(意思是:推荐一位撰写有关巴黎的作者)。
我从一个简单的聚合开始,但是,在实验上(交叉验证),最好在每个用户最多 4 本书之后停止聚合每个用户的分数。这样一来,我们就没有一个拥有 200 本书的作者可以“支配”结果。让我用伪代码解释一下:
# the aggregated score of each author
Map<Author, Double> author_scores = new Map()
# the number of books (hits) that contributed to each author
Map<Author, Integer> author_cnt = new Map()
# iterate ES query results
for Document doc in hits:
# stop aggregating if more that 4 books from this author have already been found
if (author_cnt.get(doc.author_id) < 4):
author_scores.increment_by(doc.author_id, doc.score)
author_cnt.increment_by(doc.author_id, 1)
the_result = author_scores.sort_map_by_value(reverse=true)
到目前为止,我已经在自定义应用程序代码中实现了上述聚合,但我想知道是否可以使用 Elasticsearch 的查询 DSL 或org.elasticsearch.search.aggregations.Aggregator 接口重写它。
【问题讨论】:
-
为什么要在数到 4 后停止? “停止”是什么意思?如果 ES 达到 4 本书,你希望 ES 不使用 CPU/内存?
-
@AndreiStefan 我相应地编辑了问题。
-
我还是不明白为什么要限制 4 本书。如果它是关于结果的“支配”(通过评分),我相信可以在查询中相应地操纵评分。如果我没记错的话,自定义 function_score 可以对某个函数的得分设置上限。因此,如果您的查询仅对 4 本书感兴趣,则此“4”将被视为上限,无论文档获得多少“评分”,它都会以“4”为最高。让我知道这是否是您的意图,也许可以分享更多您真正想做的事情(整个故事)。
标签: elasticsearch recommendation-engine