Elasticsearch:过滤热门匹配 [英] Elasticsearch: filter top hits aggregation

查看:89
本文介绍了Elasticsearch:过滤热门匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个带有大量用户评论的Elasticsearch索引:

Say I have an Elasticsearch index with bunch of users' comments:

{ "name": "chris", "date": "2016-01-01", "msg": "hi, foo"}
{ "name": "chris", "date": "2016-01-05", "msg": "bye, bar"}
{ "name": "aaron", "date": "2016-01-10", "msg": "who's bar"}
{ "name": "aaron", "date": "2016-01-15", "msg": "not foo"}

首先,我想为每个用户找到最新评论。我可以使用 top_hits 聚合来做到这一点:

First, I want to find the lastest comment for each user. I can do that with the top_hits aggregation:

"aggs": {
    "name": {
      "terms": { "field": "name" },
      "aggs": {
        "latest_comment": {
          "top_hits": {
            "sort": [ {"date": { "order": "desc" } } ],
            "size": 1
            }
          }
        }
      }
    }
  }

有效地给了我以下内容:

Which effectively gives me the following:

{ "name": "chris", "date": "2016-01-05", "msg": "bye, bar"}
{ "name": "aaron", "date": "2016-01-15", "msg": "not foo"}

但是我现在如何过滤这些结果呢?而且要非常清楚,我想过滤之后 top_hits 聚合选择了最新的匹配,而不是之前。

But how can I filter those results now?? And to be super clear, I want to filter after the top_hits aggregation has picked the latest hits, not before.

谢谢。

推荐答案

我有确切的问题。经过大量搜索之后的结果是:

I had the exact question. The result after a lot of search was this:

如果您要基于数字指标过滤热门匹配结果,则可以使用管道聚合(例如存储桶选择器)。这种方式可以在Elasticsearch中实现SQL HAVING。在这种情况下,一个非常有用的答案可以找到在Elasticsearch中实现HAVING

If you want to filter the top hits results based on a numeric metric, you can use pipeline aggregations like bucket selector. This way is somehow implementing a SQL HAVING in elasticsearch. a very helpful answer for this case can be find implementing HAVING in elasticsearch

但是,如果要过滤的指标不是数字,则无法(至少直到v 6.2.4之前)在elasticsearch方面执行此操作。

But if your metric to filter is not numeric there is no way (at least until v 6.2.4) to do that in elasticsearch side.

在这种情况下,如@ismail所说,您需要在客户端使用软件来完成此操作。

In this case as @ismail said you need to do that in client-side by your software.

这篇关于Elasticsearch:过滤热门匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆