Combining aggregations

Hi community,

I try to combine query aggregations. For example, if I have one main query (title:bike) and 3 aggregation filters (brand:giant, color:red and size:M), I want to get all the possible combinations:
number of bike that are giant and red and M
number of bike that are red and not giant,and not M
and so on...
I tried to generate a query with all the combinations, but it is quite a big query and I am not sure it is optimal from a performance point of view. Do you think there is a better way to do that with ES?

Thank you,

FYI, the generated query is :

"query" : {
"query_string" : {
"query" : "title:bike"
}
},
"aggregations" : {
"0" : {
"filter" : {
"bool" : {
"must_not" : [ {
"query_string" : {
"query" : "color:red"
}
}, {
"query_string" : {
"query" : "size:M"
}
}, {
"query_string" : {
"query" : "brand:giant"
}
} ]
}
}
},
"1" : {
"filter" : {
"bool" : {
"must" : {
"query_string" : {
"query" : "color:red"
}
},
"must_not" : [ {
"query_string" : {
"query" : "size:M"
}
}, {
"query_string" : {
"query" : "brand:giant"
}
} ]
}
}
},
"2" : {
"filter" : {
"bool" : {
"must" : {
"query_string" : {
"query" : "size:M"
}
},
"must_not" : [ {
"query_string" : {
"query" : "color:red"
}
}, {
"query_string" : {
"query" : "brand:giant"
}
} ]
}
}
},
"3" : {
"filter" : {
"bool" : {
"must" : [ {
"query_string" : {
"query" : "color:red"
}
}, {
"query_string" : {
"query" : "size:M"
}
} ],
"must_not" : {
"query_string" : {
"query" : "brand:giant"
}
}
}
}
},
"4" : {
"filter" : {
"bool" : {
"must" : {
"query_string" : {
"query" : "brand:giant"
}
},
"must_not" : [ {
"query_string" : {
"query" : "color:red"
}
}, {
"query_string" : {
"query" : "size:M"
}
} ]
}
}
},
"5" : {
"filter" : {
"bool" : {
"must" : [ {
"query_string" : {
"query" : "color:red"
}
}, {
"query_string" : {
"query" : "brand:giant"
}
} ],
"must_not" : {
"query_string" : {
"query" : "size:M"
}
}
}
}
},
"6" : {
"filter" : {
"bool" : {
"must" : [ {
"query_string" : {
"query" : "size:M"
}
}, {
"query_string" : {
"query" : "brand:giant"
}
} ],
"must_not" : {
"query_string" : {
"query" : "color:red"
}
}
}
}
},
"7" : {
"filter" : {
"bool" : {
"must" : [ {
"query_string" : {
"query" : "color:red"
}
}, {
"query_string" : {
"query" : "size:M"
}
}, {
"query_string" : {
"query" : "brand:giant"
}
} ]
}
}
}
}
}

Hi all,

The solution does't 'look' very optimal. But after some test, it seems that it is not too bad. I think Elasticsearch uses the cache quite well and avoid recalculating the filter each time it need it.
But another solution (less verbose) would be welcomed if someone has a better idea :slight_smile:

Thanks,

See https://github.com/elastic/elasticsearch/pull/7020#issuecomment-53087546

Cheers
Mark