过滤器聚合
编辑过滤器聚合
编辑一个单一的桶聚合,将文档集缩小到与查询匹配的文档。
示例
resp = client.search( index="sales", size="0", filter_path="aggregations", aggs={ "avg_price": { "avg": { "field": "price" } }, "t_shirts": { "filter": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } }, ) print(resp)
response = client.search( index: 'sales', size: 0, filter_path: 'aggregations', body: { aggregations: { avg_price: { avg: { field: 'price' } }, t_shirts: { filter: { term: { type: 't-shirt' } }, aggregations: { avg_price: { avg: { field: 'price' } } } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, filter_path: "aggregations", aggs: { avg_price: { avg: { field: "price", }, }, t_shirts: { filter: { term: { type: "t-shirt", }, }, aggs: { avg_price: { avg: { field: "price", }, }, }, }, }, }); console.log(response);
POST /sales/_search?size=0&filter_path=aggregations { "aggs": { "avg_price": { "avg": { "field": "price" } }, "t_shirts": { "filter": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
之前的示例计算了所有销售额的平均价格以及所有 T 恤销售额的平均价格。
响应
{ "aggregations": { "avg_price": { "value": 140.71428571428572 }, "t_shirts": { "doc_count": 3, "avg_price": { "value": 128.33333333333334 } } } }
使用顶层 query
来限制所有聚合
编辑要限制搜索中所有聚合运行所依据的文档,请使用顶层 query
。这比带有子聚合的单个 filter
聚合更快。
例如,使用这个
resp = client.search( index="sales", size="0", filter_path="aggregations", query={ "term": { "type": "t-shirt" } }, aggs={ "avg_price": { "avg": { "field": "price" } } }, ) print(resp)
response = client.search( index: 'sales', size: 0, filter_path: 'aggregations', body: { query: { term: { type: 't-shirt' } }, aggregations: { avg_price: { avg: { field: 'price' } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, filter_path: "aggregations", query: { term: { type: "t-shirt", }, }, aggs: { avg_price: { avg: { field: "price", }, }, }, }); console.log(response);
POST /sales/_search?size=0&filter_path=aggregations { "query": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } }
而不是这个
resp = client.search( index="sales", size="0", filter_path="aggregations", aggs={ "t_shirts": { "filter": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } }, ) print(resp)
response = client.search( index: 'sales', size: 0, filter_path: 'aggregations', body: { aggregations: { t_shirts: { filter: { term: { type: 't-shirt' } }, aggregations: { avg_price: { avg: { field: 'price' } } } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, filter_path: "aggregations", aggs: { t_shirts: { filter: { term: { type: "t-shirt", }, }, aggs: { avg_price: { avg: { field: "price", }, }, }, }, }, }); console.log(response);
POST /sales/_search?size=0&filter_path=aggregations { "aggs": { "t_shirts": { "filter": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
使用 filters
聚合进行多重过滤
编辑要使用多个过滤器对文档进行分组,请使用filters
聚合。这比多个 filter
聚合更快。
例如,使用这个
resp = client.search( index="sales", size="0", filter_path="aggregations", aggs={ "f": { "filters": { "filters": { "hats": { "term": { "type": "hat" } }, "t_shirts": { "term": { "type": "t-shirt" } } } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } }, ) print(resp)
response = client.search( index: 'sales', size: 0, filter_path: 'aggregations', body: { aggregations: { f: { filters: { filters: { hats: { term: { type: 'hat' } }, t_shirts: { term: { type: 't-shirt' } } } }, aggregations: { avg_price: { avg: { field: 'price' } } } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, filter_path: "aggregations", aggs: { f: { filters: { filters: { hats: { term: { type: "hat", }, }, t_shirts: { term: { type: "t-shirt", }, }, }, }, aggs: { avg_price: { avg: { field: "price", }, }, }, }, }, }); console.log(response);
POST /sales/_search?size=0&filter_path=aggregations { "aggs": { "f": { "filters": { "filters": { "hats": { "term": { "type": "hat" } }, "t_shirts": { "term": { "type": "t-shirt" } } } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
而不是这个
resp = client.search( index="sales", size="0", filter_path="aggregations", aggs={ "hats": { "filter": { "term": { "type": "hat" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } }, "t_shirts": { "filter": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } }, ) print(resp)
response = client.search( index: 'sales', size: 0, filter_path: 'aggregations', body: { aggregations: { hats: { filter: { term: { type: 'hat' } }, aggregations: { avg_price: { avg: { field: 'price' } } } }, t_shirts: { filter: { term: { type: 't-shirt' } }, aggregations: { avg_price: { avg: { field: 'price' } } } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, filter_path: "aggregations", aggs: { hats: { filter: { term: { type: "hat", }, }, aggs: { avg_price: { avg: { field: "price", }, }, }, }, t_shirts: { filter: { term: { type: "t-shirt", }, }, aggs: { avg_price: { avg: { field: "price", }, }, }, }, }, }); console.log(response);
POST /sales/_search?size=0&filter_path=aggregations { "aggs": { "hats": { "filter": { "term": { "type": "hat" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } }, "t_shirts": { "filter": { "term": { "type": "t-shirt" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }