# How to aggregate on bitwise operator individual flag values?

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-on-bitwise-operator-individual-flag-values/234588>\
**Category:** Elasticsearch\
**Created:** [May 27, 2020, 4:36pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-bitwise-operator-individual-flag-values/234588 "2020-05-27T16:36:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![randomuser](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@randomuser](https://discuss.elastic.co/u/randomuser)\
**Post date:** [May 27, 2020, 4:36pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-bitwise-operator-individual-flag-values/234588/1 "2020-05-27T16:36:10Z")

</div>

We have **cities** where each has some sporting **places** , and some places are present in multiple cities:

> London: Multiplex, Fivestar  
> Berlin: Multiplex, Bestgym  
> Paris: Multiplex, Bestgym, Fivestar

We have **places** where each place has some content to offer. For example:

> Bestgym: gym, bar  
> Fivestar: water pool, bar  
> Multiplex: gym, bar, football, table tennis

In the field **contents** we store an integer which represents a combination of different flags which we resolve with a bitwise operator (a good read on this approach here: [How we built a reversible recommendation system using Elasticsearch](https://medium.com/kaliop/how-we-built-a-reversible-recommendation-system-using-elasticsearch-d34a82b4893e)). For example:

> 1: gym  
> 2: bar  
> 4: football  
> 8: table tennis  
> 16: water pool

Following this logic, on document level for each place we store a number:

> placeA: 3 (1+2)  
> placeB: 18 (16+2)  
> placeC: 15 (1+2+4+8)

Using [bitwise operaton](https://en.wikipedia.org/wiki/Bitwise_operation) we can easily resolve which **contents** each **place** has. We can even easily filter out which **cities** have a _ **certain** _ **content**. For example, if we wanted all places in a specific city which contain _gym, bar and football_ (1+2+4=7), it would look like this:

```
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "city": "London"
              }
            },
            {
              "script": {
                "script": {
                  "lang": "expression",
                  "source": "7 & doc['contents'].value"
                }
              }
            }
          ]
        }
      }
    }
  }

```

**The Problem:**  
We want to get _ **all** _ **contents** a **city** has to offer. If I were to do a simple aggregation on top of the upper query, it would return all possible permutations of flag sums, e.g.:  
`1,2,3,7,14,15,18,28,31`

What I want is, get bucket results which represent the flag number of the **contents** , e.g.  
`2,4,16`

Any ideas how this would be achievable?

---

<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:** [June 24, 2020, 4:36pm UTC](https://discuss.elastic.co/t/how-to-aggregate-on-bitwise-operator-individual-flag-values/234588/2 "2020-06-24T16:36:12Z")

</div>

Closed on June 24, 2020, 4:36pm UTC

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