How do I filter top_hits metric aggregation result [Elasticsearch]

前端 未结 1 1358
谎友^
谎友^ 2021-01-03 11:30

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: \'so         


        
相关标签:
1条回答
  • 2021-01-03 12:10

    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"
              }
            }
          }
        }
      }
    }
    

    Hope this helps!!

    0 讨论(0)
提交回复
热议问题