# Elasticsearch Cardinality aggregation with condition

**URL:** https://discuss.elastic.co/t/elasticsearch-cardinality-aggregation-with-condition/260275
**Category:** Elasticsearch
**Created:** [January 6, 2021, 6:35am UTC](https://discuss.elastic.co/t/elasticsearch-cardinality-aggregation-with-condition/260275 "2021-01-06T06:35:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rohitc.11](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@rohitc.11](https://discuss.elastic.co/u/rohitc.11)
#### Post date: [January 6, 2021, 6:35am UTC](https://discuss.elastic.co/t/elasticsearch-cardinality-aggregation-with-condition/260275/1 "2021-01-06T06:35:04Z")

</div>

I want to do **cardinality aggregation** with condition.

I want to count all the unique sellers whose sell sum should be greater than 100 for product list.

Is there any way to do this using cardinality or anything else?

I have tried with **Bucket selector** and **Cardinality** following query but it did not worked.

**Mappings**

```auto
{
    "mappings": {
        "properties": {
            "product_id": {
                "type": "long"
            },
            "seller_id": {
                "type": "long"
            },
            "sell": {
                "type": "double"
            }
        }
    }
}

```

**Sample Documents**

```auto
[
    {
        "product_id": 1,
        "seller_id": 1,
        "sell": 70
    },
    {
        "product_id": 1,
        "seller_id": 1,
        "sell": 40
    },
    {
        "product_id": 1,
        "seller_id": 2,
        "sell": 10
    },
    {
        "product_id": 2,
        "seller_id": 1,
        "sell": 20
    },
    {
        "product_id": 2,
        "seller_id": 2,
        "sell": 120
    },
    {
        "product_id": 2,
        "seller_id": 3,
        "sell": 90
    },
    {
        "product_id": 2,
        "seller_id": 3,
        "sell": 20
    }
]

```

**Query**

```auto
{
  "size": 0,
  "aggregations": {
    "products": {
      "terms": {
        "field": "product_id"
      },
      "aggregations": {
        "seller_count": {
          "cardinality": {
            "field": "seller_id"
          },
          "aggregations": {
            "total_sell": {
              "sum": {
                "field": "sell"
              }
            },
            "sell_bucket_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "totalSell": "total_sell"
                },
                "script": {
                  "source": "params.totalSell > 100"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

**Expected Sample Result**

```auto
{
    "aggregations": {
        "products": {
            "buckets": [
                {
                    "key": 1, // product_id
                    "seller_count": {
                        "value": 1 // 1 Seller is present whose sell sum is greater than 100
                    }
                },
                {
                    "key": 2, // product_id
                    "seller_count": {
                        "value": 2 // 2 Seller is present whose sell sum is greater than 100
                    }
                }
            ]
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 3, 2021, 6:35am UTC](https://discuss.elastic.co/t/elasticsearch-cardinality-aggregation-with-condition/260275/2 "2021-02-03T06:35:05Z")

</div>

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