配置OpenSearch集群聚合增强
CSS服务的OpenSearch集群聚合增强能力通过向量化技术和数据聚簇优化,显著提升大规模数据的聚合分析性能,帮助用户在复杂数据场景中实现高效分析和提升业务决策效率。
原理介绍
- 排序键:数据按指定字段(如时间戳)顺序存储,确保相同值或相近值的文档在磁盘上连续分布。
- 聚簇键:作为排序键的前缀子集(如各城市订单数),进一步将数据聚簇,使聚合操作可批量处理连续数据块,而非逐条遍历。
场景 |
描述 |
---|---|
低基字段聚合 |
字段值种类较少,适合分组聚合。例如,统计各城市订单数量时,数据聚簇后可快速定位并批量处理。 |
高基字段聚合 |
字段值种类繁多,适合直方图聚合。例如,按小时统计访问量时,聚簇键可加速区间数据的聚合。 |
低基和高基字段混合聚合 |
先对低基字段分组(如各城市订单),再对高基字段直方图聚合(如时间),通过多级聚簇提升混合查询效率。 |
约束限制
OpenSearch集群仅2.19.0版本支持聚合增强功能。
低基字段聚合
低基字段一般采用分组聚合,在排序的情况下具备较好的数据聚簇性,利于向量化批量处理数据。
- 执行如下命令,设置索引testindex。
PUT testindex { "mappings": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd" }, "city": { "type": "keyword" }, "product": { "type": "keyword" }, "trade": { "type": "double" } } }, "settings": { "index": { "search": { "turbo": { "enabled": "true" } }, "sort": { "field": [ "city", "product" ] }, "cluster": { "field": [ "city", "product" ] } } } }
表2 低基字段聚合参数说明 参数
参数类型
描述
index.search.turbo.enabled
Boolean
是否启用聚合增强能力。使用聚合分析时必须启用聚合增强能力。
取值范围:
- false(默认值):不启用聚合增强。
- true:启用聚合增强。
index.sort.field
Array of strings
指定排序键。排序键用于定义文档排序所依据的字段。
在配置排序键时,可以指定一个或多个字段。当多个字段被指定时,排序将按照字段的顺序依次进行。第一个字段的排序结果将作为主要排序依据,第二个字段则在第一个字段排序结果的基础上进行次级排序,以此类推。
取值范围:必须是索引包含的字段。
index.cluster.field
Array of strings
指定聚簇键。聚簇键用于对文档进行聚簇处理,具有相同键值的文档会被归类到同一聚簇中。
在进行聚合操作时,同一聚簇内的文档能够被批量处理,从而显著提升聚合性能。
约束限制:聚簇键必须是排序键的前缀子集。
取值范围:必须是索引包含的字段。
- 执行如下命令,导入样例数据。
PUT /_bulk { "index": { "_index": "testindex", "_id": "1" } } { "date": "2025-01-01", "city": "cityA", "product": "books", "trade": 3000.0} { "index": { "_index": "testindex", "_id": "2" } } { "date": "2025-01-02", "city": "cityA", "product": "books", "trade": 1000.0} { "index": { "_index": "testindex", "_id": "3" } } { "date": "2025-01-01", "city": "cityA", "product": "bottles", "trade": 100.0} { "index": { "_index": "testindex", "_id": "4" } } { "date": "2025-01-02", "city": "cityA", "product": "bottles", "trade": 300.0} { "index": { "_index": "testindex", "_id": "5" } } { "date": "2025-01-01", "city": "cityB", "product": "books", "trade": 7000.0} { "index": { "_index": "testindex", "_id": "6" } } { "date": "2025-01-02", "city": "cityB", "product": "books", "trade": 1000.0}
- 执行查询命令,实现低基字段聚合查询。
POST testindex/_search { "size": 0, "aggs": { "groupby_city": { "terms": { "field": "city" }, "aggs": { "groupby_product": { "terms": { "field": "product" }, "aggs": { "avg_trade": { "avg": { "field": "trade" } } } } } } } }
高基字段聚合
高基字段一般采用直方图分组聚合,利于处理某个区间内的数据。
- 执行如下命令,设置索引testindex。
PUT testindex { "mappings": { "properties": { "timestamp": { "type": "date", "format": "yyyy-MM-dd" }, "score": { "type": "double" } } }, "settings": { "index": { "search": { "turbo": { "enabled": "true" } }, "sort": { "field": [ "timestamp" ] } } } }
表3 高基字段聚合参数说明 参数
参数类型
描述
index.search.turbo.enabled
Boolean
是否启用聚合增强能力。使用聚合分析时必须启用聚合增强能力。
取值范围:
- false(默认值):不启用聚合增强。
- true:启用聚合增强。
index.sort.field
Array of strings
指定排序键。排序键用于定义文档排序所依据的字段。
在配置排序键时,可以指定一个或多个字段。当多个字段被指定时,排序将按照字段的顺序依次进行。第一个字段的排序结果将作为主要排序依据,第二个字段则在第一个字段排序结果的基础上进行次级排序,以此类推。
取值范围:必须是索引包含的字段。
- 执行如下命令,导入样例数据。
PUT /_bulk { "index": { "_index": "testindex", "_id": "1" } } { "date": "2025-01-01", "score": "12.0"} { "index": { "_index": "testindex", "_id": "2" } } { "date": "2025-01-02","score": "24.0"} { "index": { "_index": "testindex", "_id": "3" } } { "date": "2025-01-01","score": "53.0"} { "index": { "_index": "testindex", "_id": "4" } } { "date": "2025-01-02", "score": "22.0"} { "index": { "_index": "testindex", "_id": "5" } } { "date": "2025-01-01", "score": "99.0"} { "index": { "_index": "testindex", "_id": "6" } } { "date": "2025-01-02","score": "26.0"}
- 执行查询命令,实现高基字段聚合查询。
该聚合任务是对时间字段date做直方图分组,然后求score的平均值。
POST testindex/_search?pretty { "size": 0, "aggs": { "groupbytime": { "date_histogram": { "field": "date", "calendar_interval": "day" }, "aggs": { "avg_score": { "avg": { "field": "score" } } } } } }
低基和高基字段混合聚合
在低基字段和高基字段混合的场景下,一般先对低基字段做分组聚合,然后再对高基字段做直方图聚合。
- 执行如下命令,设置索引testindex。
PUT testindex { "mappings": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd" }, "city": { "type": "keyword" }, "product": { "type": "keyword" }, "trade": { "type": "double" } } }, "settings": { "index": { "search": { "turbo": { "enabled": "true" } }, "sort": { "field": [ "city", "product", "date" ] }, "cluster": { "field": [ "city", "product" ] } } } }
表4 低基和高基字段混合聚合的参数说明 参数
参数类型
描述
index.search.turbo.enabled
Boolean
是否启用聚合增强能力。使用聚合分析时必须启用聚合增强能力。
取值范围:
- false(默认值):不启用聚合增强。
- true:启用聚合增强。
index.sort.field
Array of strings
指定排序键。排序键用于定义文档排序所依据的字段。
在配置排序键时,可以指定一个或多个字段。当多个字段被指定时,排序将按照字段的顺序依次进行。第一个字段的排序结果将作为主要排序依据,第二个字段则在第一个字段排序结果的基础上进行次级排序,以此类推。
约束限制:高基字段必须在排序键中,且高基字段必须在最后一个低基字段后面。
取值范围:必须是索引包含的字段。
index.cluster.field
Array of strings
指定聚簇键。聚簇键用于对文档进行聚簇处理,具有相同键值的文档会被归类到同一聚簇中。
在进行聚合操作时,同一聚簇内的文档能够被批量处理,从而显著提升聚合性能。
约束限制:聚簇键必须是排序键的前缀子集。
取值范围:必须是索引包含的字段。
- 执行如下命令,导入样例数据。
PUT /_bulk { "index": { "_index": "testindex", "_id": "1" } } { "date": "2025-01-01", "city": "cityA", "product": "books", "trade": 3000.0} { "index": { "_index": "testindex", "_id": "2" } } { "date": "2025-01-02", "city": "cityA", "product": "books", "trade": 1000.0} { "index": { "_index": "testindex", "_id": "3" } } { "date": "2025-01-01", "city": "cityA", "product": "bottles", "trade": 100.0} { "index": { "_index": "testindex", "_id": "4" } } { "date": "2025-01-02", "city": "cityA", "product": "bottles", "trade": 300.0} { "index": { "_index": "testindex", "_id": "5" } } { "date": "2025-01-01", "city": "cityB", "product": "books", "trade": 7000.0} { "index": { "_index": "testindex", "_id": "6" } } { "date": "2025-01-02", "city": "cityB", "product": "books", "trade": 1000.0}
- 执行查询命令,实现低基和高基字段混合聚合查询。
POST testindex/_search { "size": 0, "aggs": { "groupby_region": { "terms": { "field": "city" }, "aggs": { "groupby_host": { "terms": { "field": "product" }, "aggs": { "groupby_timestamp": { "date_histogram": { "field": "date", "interval": "day" }, "aggs": { "avg_score": { "avg": { "field": "trade" } } } } } } } } } }