过滤器聚合编辑

一种单桶聚合,用于将文档集缩小到与 查询 匹配的文档。

示例

resp = client.search(
    index="sales",
    size="0",
    filter_path="aggregations",
    body={
        "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
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",
    body={
        "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
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",
    body={
        "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
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",
    body={
        "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
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",
    body={
        "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
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" } }
      }
    }
  }
}