title: Further filtering of aggregations
body: <p>I have a question regarding aggregation in elastic search. I have a document like the following:</p>
  
  <pre><code>{
    "_index": "products",
    "_type": "product",
    "_id": "ID-12345",
    "_score": 1,
    "_source": {
      "created_at": "2017-08-04T17:56:44.592Z",
      "updated_at": "2017-08-04T17:56:44.592Z",
      "product_information": {
        "sku": "12345",
        "name": "Product Name",
        "price": 25,
        "brand": "Brand Name",
        "url": "URL"
      },
      "product_detail": {
        "description": "Product description text here.",
        "string_facets": [
          {
            "facet_name": "Colour",
            "facet_value": "Grey"
          },
          {
            "facet_name": "Category",
            "facet_value": "Linen"
          },
          {
            "facet_name": "Category",
            "facet_value": "Throws &amp; Blanket"
          },
          {
            "facet_name": "Keyword",
            "facet_value": "Contemporary"
          },
          {
            "facet_name": "Keyword",
            "facet_value": "Sophisticated"
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>I am storing product information such as Colour, Material, Category and Keywords within the <code>product_detail.string_facets</code> field. I'd like to use this for aggregation to get Colour/Material/Category/Keyword suggestions but as separate buckets. I.e, there is a separate bucket for each of those string_facet types as defined in <code>product_detail.string_facets.facet_name</code>.</p>
  
  <p>This is the query I have at the moment which is returning data, but not as I expect. First the query (this was just to try and get Colours):</p>
  
  <pre><code>{
    "from": 0,
    "size": 12,
    "query": {
      "bool": {
        "should": [
          {
            "multi_match": {
              "query": "Rug",
              "fields": ["product_information.name", "product_detail.string_facets.facet_value"]
            }
          },
          {
            "multi_match": {
              "query": "Blue",
              "fields": ["product_information.name", "product_detail.string_facets.facet_name"]
            }
          }
        ],
        "minimum_should_match": "100%"
      }
    },
    "aggs": {
      "suggestions": {
        "filter": { "term": { "product_detail.string_facets.facet_name.keyword": "Colour" }},
        "aggs": {
          "colours": {
            "terms": {
              "field": "product_detail.string_facets.facet_value.keyword",
              "size": 10
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>This is giving me output like the following: </p>
  
  <pre><code>"aggregations": {
      "suggestions": {
        "doc_count": 21,
        "colours": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 23,
          "buckets": [
            {
              "key": "Rug",
              "doc_count": 21
            },
            {
              "key": "Blue",
              "doc_count": 18
            },
            {
              "key": "Bold",
              "doc_count": 7
            },
            {
              "key": "Modern",
              "doc_count": 6
            },
            {
              "key": "Multi-Coloured",
              "doc_count": 5
            },
            {
              "key": "Contemporary",
              "doc_count": 4
            },
            {
              "key": "Traditional",
              "doc_count": 4
            },
            {
              "key": "White",
              "doc_count": 4
            },
            {
              "key": "Luxurious",
              "doc_count": 3
            },
            {
              "key": "Minimal",
              "doc_count": 3
            }
          ]
        }
      }
    }
  </code></pre>
  
  <p>It has given me the results of all <code>facet_name</code> rather those of <code>facet_type</code> Colour as I thought it would.</p>
  
  <p>Any help would be greatly appreciated. Elasticsearch seems very powerful but the documentation is quite daunting!</p>