Is there a way in elastic search to retrieve the different filter permutations?

Viewed 24

Suppose the documents have two attributes, gender (male or female) and hair_color (blonde or brunette). And we want to get the number of documents for all these permutations and, importantly, the number of documents for the isolated attributes. For example, here we would like to have the number of documents for the following cases:

  1. Male
  2. Female
  3. Blonde
  4. Brunette
  5. Male blonde
  6. Male Brunette
  7. Female blonde
  8. Female brunette

The goal is to have something like the following table:

gender hair_color doc_count
male - 23
male blonde 10
male brunette 11
female - 81
female blonde 55
female brunette 1
- brunette 70
- blonde 14

Note that there may be cases where one of the attributes is not defined (so brunette is not necessarily equal to male brunette + female brunette).

Is there any query that can return this to us in a clear way?

I'm new to elasticsearch, so please excuse me if the question is trivial.

I've tried filters, first doing an aggregation by gender (so we get 1-2), and then an aggregation by hair_color + gender (so we get 3-8). But for a larger number of attributes (we could add age), it gets too complicated, and it's a bit obscure from a programmatic point of view.

1 Answers

Yes, terms aggregations can do exactly that

{
  "size": 0,
  "aggs": {
    "gender": {
      "terms": {
        "field": "gender",
        "size": 10,
        "missing": "no_gender"
      },
      "aggs": {
        "hair": {
          "terms": {
            "field": "hair_color",
            "size": 10,
            "missing": "no_color"
          }
        }
      }
    }
  }
}

Another option is to leverage the composite aggregation, whose result is easier to process because it's a single bucket array, with all possible combinations:

{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "gender": {
              "terms": {
                "field": "gender",
                "missing": "no_gender"
              }
            }
          },
          {
            "color": {
              "terms": {
                "field": "hair_color",
                "missing": "no_color"
              }
            }
          },
          {
            "age": {
              "terms": {
                "field": "age",
                "missing": "no_age"
              }
            }
          }
        ]
      }
    }
  }
}
Related