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:
- Male
- Female
- Blonde
- Brunette
- Male blonde
- Male Brunette
- Female blonde
- 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.