# Uniqueness in filter aggregations

**URL:** <https://discuss.elastic.co/t/uniqueness-in-filter-aggregations/303272>\
**Category:** Elasticsearch\
**Created:** [April 26, 2022, 11:20am UTC](https://discuss.elastic.co/t/uniqueness-in-filter-aggregations/303272 "2022-04-26T11:20:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucass](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@Lucass](https://discuss.elastic.co/u/Lucass)\
**Post date:** [April 26, 2022, 11:20am UTC](https://discuss.elastic.co/t/uniqueness-in-filter-aggregations/303272/1 "2022-04-26T11:20:08Z")

</div>

Hi,

kindly ask you ideas to solve following: imagine having users filling personal surveys, eg. filling what they usually smoke. Each user can decide one or more possibilities from the checklist survey. On results, I am doing filter aggregations as follows:

```auto
POST users/_search
{
  "aggs": {        
     "e0b5ca053d9a61a415c43586cf07cccb7f9793a7": {
        "filters": {
            "filters": {
                "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_cigarettes": {
                    "nested": {
                        "path": "surveys",
                        "query": {
                            "term": {
                                "surveys.26.answers.whatExactlyDoYouSmoke.cigarettes": true
                            }
                        }
                    }
                },
                "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_cigars": {
                    "nested": {
                        "path": "surveys",
                        "query": {
                            "term": {
                                "surveys.26.answers.whatExactlyDoYouSmoke.cigars": true
                            }
                        }
                    }
                },
                "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_pipe": {
                    "nested": {
                        "path": "surveys",
                        "query": {
                            "term": {
                                "surveys.26.answers.whatExactlyDoYouSmoke.pipe": true
                            }
                        }
                    }
                },
               "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_electroniccigarettes": {
                    "nested": {
                        "path": "surveys",
                        "query": {
                            "term": {
                                "surveys.26.answers.whatExactlyDoYouSmoke.electroniccigarettes": true
                            }
                        }
                    }
                }
            },
            "other_bucket_key": "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::_others"
           
        }
    }
  }
}

```

Imagine user who smokes cigarettes and cigars - as a result, he is included in two aggregations.

What I need to achieve is a kind of uniqueness (userIDs) over all aggregations, meaning although user checks two options (cigarettes and cigars), once he falls into one aggregation (eg. cigars), I need to exclude him from the other (cigarettes), see the result:

```auto
"aggregations" : {
    "e0b5ca053d9a61a415c43586cf07cccb7f9793a7" : {
      "meta" : { },
      "buckets" : {
        "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_cigarettes" : {
          "doc_count" : 1
        },
        "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_cigars" : {
          "doc_count" : 1 // SAME USER AS IN PREVIOUS AGG. REQUIRED TO BE REMOVED
        },
        "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_electroniccigarettes" : {
          "doc_count" : 0
        },
        "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::surveys.26.answers.whatExactlyDoYouSmoke_has_pipe" : {
          "doc_count" : 0
        },
        "e0b5ca053d9a61a415c43586cf07cccb7f9793a7::_other" : {
          "doc_count" : 4
        }
      }
    }
  }

```

It is important to achieve that in one request, not via splitting and adding exclude of userIDs (buckets) from previous aggregations. I also need to maintain other\_buckets key.

Any ideas? Scripting? Pipelines? Construct negations in each aggregation built of previous ones?

I am a newbie so I would welcome answers for dummies;) Thx.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 27, 2022, 8:28am UTC](https://discuss.elastic.co/t/uniqueness-in-filter-aggregations/303272/2 "2022-04-27T08:28:39Z")

</div>

Hey,

sounds like the [cardinality aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-aggregations-metrics-cardinality-aggregation.html) could help you in this case.

--Alex

---

<div class="post-metadata">

**Author:** ![Lucass](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@Lucass](https://discuss.elastic.co/u/Lucass)\
**Post date:** [April 27, 2022, 10:12am UTC](https://discuss.elastic.co/t/uniqueness-in-filter-aggregations/303272/3 "2022-04-27T10:12:41Z")

</div>

Thx for the reponse. Cardinality was something I tried already - but possible did not understand the usage in this specific case when mapping of the fields split into each aggregation looks as follows:

```auto
...
"whatExactlyDoYouSmoke":{
   "properties":{
      "cigarettes":{
         "type":"boolean"
      },
      "cigars":{
         "type":"boolean"
      },
      "electronicCigarettes":{
         "type":"boolean"
      },
      "pipe":{
         "type":"boolean"
      }
   }
},
...

```

Could you point me out a little where exactly I should put that cardinality rule? And should I use scripting to precompute the value for cardinality according the example here [Cardinality aggregation | Elasticsearch Guide [8.1] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-aggregations-metrics-cardinality-aggregation.html#_script_4), or anyhow directly?

---

<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:** [May 25, 2022, 10:13am UTC](https://discuss.elastic.co/t/uniqueness-in-filter-aggregations/303272/4 "2022-05-25T10:13:33Z")

</div>

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