Attention: pas testé.

Source: Magento 2 product count by attribute.