Elasticsearch aggregation on a field with dynamic properties

Given the following mapping where variants are a nested type and options is a flattened type:

{
  "doc_type" : "product",
  "id" : 1,
  "variants" : [
    {
      "options" : {
        "Size" : "XS",
      },
      "price" : 1,
    },

    {
      "options" : {
        "Size" : "S",
        "Material": "Wool"
      },
      "price" : 6.99,
    },
  ]
} 

I want to run an aggregation that produces data in the following format:

{
  "variants.options.Size": {
    "buckets" : [
      {
        "key" : "XS",
        "doc_count" : 1
      },
      {
        "key" : "S",
        "doc_count" : 1
      },
    ],
  },
  "variants.options.Material": {
    "buckets" : [
      {
        "key" : "Wool",
        "doc_count" : 1
      }
    ],
  },
} 

I could very easily do something like:

"aggs": {
    "variants.options.Size": {
      "terms": {
        "field": "variants.options.Size"
      }
    },
    "variants.options.Material": {
      "terms": {
        "field": "variants.options.Material"
      }
    }
  }

The caveat here is that we're using the flattened type for options because the fields in options are dynamic and so there is no way for me to know before hand that we want to aggregate on Size and Material.

Essentially, I want to tell Elasticsearch that it should aggregate on whatever keys it finds under options. Is there a way to do this?

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