如何过滤top_hits指标聚合结果[Elasticsearch] [英] How do I filter top_hits metric aggregation result [Elasticsearch]

查看:910
本文介绍了如何过滤top_hits指标聚合结果[Elasticsearch]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按地址分组,然后按日期获取最新地址,然后按状态过滤此结果。

I want to group by address then get latest address by date then filter this results by there status.

ex
  {address: 'A', date: '10-10-1991', status: 'sold'}
  {address: 'A', date: '10-10-2016', status: 'active'}
  {address: 'A', date: '10-10-1981', status: 'sold'}

  {address: 'B', date: '10-10-2016', status: 'sold'}
  {address: 'B', date: '10-10-1771', status: 'sold'}
  {address: 'B', date: '10-10-1991', status: 'active'}

//Getting address with sold status should give me only this record {address: 'B', date: '10-10-2016', status: 'sold'} since most updated one in address A is an active status

此查询采用弹性搜索,但只能按地址分组并获取最新日期。我无法按状态过滤此结果。

I have this query in elastic search but its only upto grouping by address and getting latest date. I CANNOT filter this results by there status.

 {
    "size": 0,
    "aggs": {
        "group": {
            "terms": {
                "field": "address"
            },
            "aggs": {
                "group_docs": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "updated_date": {
                                    "order": "desc"
                                }
                            }]
                    }

                }
            }
        }
    }
}

// This query gives me this records
{address: 'A', date: '10-10-2016', status: 'active'}
{address: 'B', date: '10-10-2016', status: 'sold'}

我想使用Elasticsearch从该结果中获得出售状态

I want to get sold status from that results using elasticsearch

推荐答案

在ES 2.x 中使用可以实现管道聚合。首先,我们需要汇总地址。然后,我们使用两种汇总,一种汇总获取最新日期,另一种汇总获取销售状态的最新日期。然后,我们检查两个日期是否均与存储桶选择器聚合。就是这样。

This is possible with ES 2.x using pipeline aggregations. First we need to aggregate on addresses. Then we use two aggregations, one which gets the latest_date and other which gets latest date for sold status. Then we check if both dates match with bucket selector aggregation. This is how it looks.

{
  "size": 0,
  "aggs": {
    "unique_address": {
      "terms": {
        "field": "address",
        "size": 10
      },
      "aggs": {
        "latest_date": {
          "max": {
            "field": "date"
          }
        },
        "filter_sold": {
          "filter": {
            "term": {
              "status": "sold"
            }
          },
          "aggs": {
            "latest_sold_date": {
              "max": {
                "field": "date"
              }
            }
          }
        },
        "should_we_consider": {
          "bucket_selector": {
            "buckets_path": {
              "my_var1": "latest_date",
              "my_var2": "filter_sold>latest_sold_date"
            },
            "script": "my_var1 == my_var2"
          }
        }
      }
    }
  }
}

希望这会有所帮助!

这篇关于如何过滤top_hits指标聚合结果[Elasticsearch]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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