在线文字转语音网站:无界智能 aiwjzn.com

Elasticsearch聚合查询

Elasticsearch是一种开源的分布式搜索和分析引擎,提供了丰富的聚合查询功能。以下是一些常见的聚合查询和示例: 1. 条件过滤聚合查询(Filter Aggregation):根据指定的过滤条件对数据进行聚合,仅返回符合条件的结果。 json GET /products/_search { "size": 0, "aggs": { "filtered_data": { "filter": { "term": { "category": "electronics" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } 上述示例会聚合返回所有类别为"electronics"的产品的平均价格。 2. 范围聚合查询(Range Aggregation):根据指定的数值范围对数据进行聚合。 json GET /products/_search { "size": 0, "aggs": { "price_ranges": { "range": { "field": "price", "ranges": [ { "to": 100 }, { "from": 100, "to": 500 }, { "from": 500 } ] } } } } 上述示例会聚合返回价格在不同范围内的产品数量。 3. 直方图聚合查询(Histogram Aggregation):根据指定的数值间隔对数据进行聚合。 json GET /products/_search { "size": 0, "aggs": { "price_histogram": { "histogram": { "field": "price", "interval": 100 } } } } 上述示例会聚合返回以100为间隔的价格直方图。 4. 日期直方图聚合查询(Date Histogram Aggregation):根据指定的日期间隔对数据进行聚合。 json GET /products/_search { "size": 0, "aggs": { "date_histogram": { "date_histogram": { "field": "date", "calendar_interval": "month" } } } } 上述示例会聚合返回每个月的产品数。 5. 分组聚合查询(Terms Aggregation):根据指定的字段进行分组,并对每个分组进行聚合计算。 json GET /products/_search { "size": 0, "aggs": { "group_by_category": { "terms": { "field": "category" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } 上述示例会聚合返回按照类别分组的产品数量,并计算每个类别的平均价格。 这只是演示了一小部分Elasticsearch聚合查询的功能,实际上还有很多其他聚合查询类型和可用参数供选择和使用。具体的表结构和样例数据需要根据应用场景而定。