Aggregations: How to get number of combinations


Let's say we have a simple ES index having two fields: "name" (string) and "expired" (boolean) in one nested object "products" (array). Each product name can be mentioned only once in each document.

Here's the example data:

{doc_id: 1, products: [{name: "apple", expired:true}, {name: "banana", expired:true}, {name: "orange", expired:true}]}
{doc_id: 2, products: [{name: "apple", expired:false}, {name: "banana", expired:true}, {name: "orange", expired:true}]}
{doc_id: 3, products: [{name: "apple", expired:false}, {name: "banana", expired:true}, {name: "orange", expired:true}]}
{doc_id: 4, products: [{name: "apple", expired:false}, {name: "banana", expired:false}, {name: "orange", expired:false}]}
  1. How to get doc_count for all product combinations?
    Expected result:
{products: [{name: "apple", expired:true}, {name: "banana", expired:true}, {name: "orange", expired:true}], doc_count: 1}
{products: [{name: "apple", expired:false}, {name: "banana", expired:true}, {name: "orange", expired:true}], doc_count: 2}
{products: [{name: "apple", expired:false}, {name: "banana", expired:false}, {name: "orange", expired:false}], doc_count: 1}
  1. How to get doc_count for selected product combinations?
    Expected result for filter "banana" AND "orange":
{products: [{name: "banana", expired:true}, {name: "orange", expired:true}], doc_count: 3}
{products: [{name: "banana", expired:false}, {name: "orange", expired:false}], doc_count: 1}
1 Like

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