Question about grouping to get the total number of filtered buckets in a bucket

this is my source data

[
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 3587575,
      "date" : "2021-02-24T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 75848633,
      "date" : "2021-01-20T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 25308769,
      "date" : "2021-01-19T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 25308769,
      "date" : "2021-01-19T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 74256954,
      "date" : "2021-01-19T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 65945090,
      "date" : "2021-01-20T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 74256954
      "date" : "2021-01-19T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 2374066,
      "date" : "2021-01-20T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 64505236,
      "date" : "2021-01-19T00:00:00"
  },
  {
      "buyer_id_std" : 35444337,
      "seller_id_std" : 78976932,
      "date" : "2021-01-19T00:00:00"
  }
]

and this is my query statement

GET trade/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "buyer_id_std": 35444337
          }
        },
        {
          "range": {
            "date": {
              "gte": "2020-09-06T00:00:00Z",
              "lte": "2021-09-06T00:00:00Z"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "group_by_partner": {
      "terms": {
        "field": "seller_id_std",
        "size": 200000
      },
      "aggs": {
        "min_date": {
          "min": {
            "field": "date"
          }
        },
        "max_date": {
          "max": {
            "field": "date"
          }
        },
        "mdate": {
          "bucket_selector": {
            "buckets_path": {"md": "max_date"},
            "script": "params.md > 1601164800000L"
          }
        },
        "bucket_field": {
          "bucket_sort": {
            "from": 0,
            "size": 2
          }
        }
      }
    },
    "sum_partner": {
      "stats_bucket": {
        "buckets_path": "group_by_partner>_count"
      }
    }
  },
  "size": 0,
  "track_total_hits": true
}

Results

{
  "took" : 46,
  "timed_out" : false,
  "_shards" : {
    "total" : 61,
    "successful" : 61,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_partner" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 74256954,
          "doc_count" : 155,
          "max_date" : {
            "value" : 1.6308864E12,
            "value_as_string" : "2021-09-06T00:00:00.000Z"
          },
          "min_date" : {
            "value" : 1.6020288E12,
            "value_as_string" : "2020-10-07T00:00:00.000Z"
          },
          "count" : {
            "value" : 1
          }
        },
        {
          "key" : 2374066,
          "doc_count" : 108,
          "max_date" : {
            "value" : 1.6308E12,
            "value_as_string" : "2021-09-05T00:00:00.000Z"
          },
          "min_date" : {
            "value" : 1.6073856E12,
            "value_as_string" : "2020-12-08T00:00:00.000Z"
          }
        }
      ]
    },
    "sum_partner" : {
      "count" : 2,
      "min" : 108.0,
      "max" : 155.0,
      "avg" : 131.5,
      "sum" : 263.0
    }
  }
}

The result I want is to be able to count the number of buckets, as above, I need the total number of buckets to be 8 and not 2.
You can't use

"count": {"cardinality": {"field": "seller_id_std"}},

Because the count here is the total number of buckets and I need the total number of buckets after filtering, which is the total number after filtering in the following code

"mdate": {
    "bucket_selector": {
    "buckets_path": {"md": "max_date"},
    "script": "params.md > 1601164800000L"
    }
}

Please help me. thank you very much!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.