Filter Aggregationedit

Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.

Example:

response = client.search(
  index: 'sales',
  size: 0,
  body: {
    aggregations: {
      t_shirts: {
        filter: {
          term: {
            type: 't-shirt'
          }
        },
        aggregations: {
          avg_price: {
            avg: {
              field: 'price'
            }
          }
        }
      }
    }
  }
)
puts response
res, err := es.Search(
	es.Search.WithIndex("sales"),
	es.Search.WithBody(strings.NewReader(`{
	  "aggs": {
	    "t_shirts": {
	      "filter": {
	        "term": {
	          "type": "t-shirt"
	        }
	      },
	      "aggs": {
	        "avg_price": {
	          "avg": {
	            "field": "price"
	          }
	        }
	      }
	    }
	  }
	}`)),
	es.Search.WithSize(0),
	es.Search.WithPretty(),
)
fmt.Println(res, err)
POST /sales/_search?size=0
{
  "aggs": {
    "t_shirts": {
      "filter": { "term": { "type": "t-shirt" } },
      "aggs": {
        "avg_price": { "avg": { "field": "price" } }
      }
    }
  }
}

In the above example, we calculate the average price of all the products that are of type t-shirt.

Response:

{
  ...
  "aggregations": {
    "t_shirts": {
      "doc_count": 3,
      "avg_price": { "value": 128.33333333333334 }
    }
  }
}