ElasticSearch中如何进行高级的聚合分析操作?

2026-05-20 23:211阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计647个文字,预计阅读时间需要3分钟。

ElasticSearch中如何进行高级的聚合分析操作?

聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等同于SQL中的GROUP BY和聚合函数。在Elasticsearch中,您可以通过执行搜索并使用`hits`(命令中的结果)同时返回搜索结果和聚合结果,将一个响应聚合起来。

聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等于SQL GROUPBY和SQL聚合函数。在Elasticsearch中, 您有执行搜索返回hits (命中结果),并且同时返回聚合结果,把一个响应中的所有hits (命中结果)分隔开的能力。这是非常强大且有效的,您可以执行查询和多个聚合,并且在一次使用中得到各自的(任何一个的)返回结果,使用次简洁和简化的API来避免网络往返。

聚合

GET /bank/_search { "query": { "match": { "address": "mill" } }, "aggs": { ## 聚合 "ageAgg": { ## 聚合名字 "terms": { "field": "age", ## 聚合属性 "size": 10 } } } }

返回结果

ElasticSearch中如何进行高级的聚合分析操作?

GET /bank/_search { "query": { "match": { "address": "mill" } }, "aggs": { ## 聚合 "ageAgg": { ## 聚合名字 "terms": { "field": "age", ## 聚合属性 "size": 10 } }, "ageAvg":{ 查询平均值 "avg": { "field": "age" ##年龄的 } } }, "size": 0 ##只看 聚合结果 }

案例

查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资

GET /bank/_search { "query": { "match_all": {} }, "aggs": { "ageAgg": { "terms": { "field": "age", "size": 100 }, "aggs": { "genderAgg": { "terms": { "field": "gender.keyword" }, "aggs": { "ageAvg": { "avg": { "field": "balance" } } } }, "ageBalabceAvg":{ "avg": { "field": "balance" } } } } } }

Mapping映射

GET /bank/_mapping 查询类型 PUT /my-index { "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, ## keyword 精确匹配 非全文检索 "name": { "type": "text" } ## text 全文检索 非精确匹配 } } }

不能直接修改映射 采用数据迁移

PUT /newbank { "mappings": { "properties": { //具体规则 "account_number": { "type": "long" }, "address": { "type": "text" }, "age": { "type": "integer" } "gender": { "type": "keyword" }, "lastname": { "type": "keyword", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } _reindex迁移 POST _reindex { "source": { //迁移元数据 "index": "bank", "type": "account" }, "dest": { //迁移新数据 "index": "newbank" } }

本文共计647个文字,预计阅读时间需要3分钟。

ElasticSearch中如何进行高级的聚合分析操作?

聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等同于SQL中的GROUP BY和聚合函数。在Elasticsearch中,您可以通过执行搜索并使用`hits`(命令中的结果)同时返回搜索结果和聚合结果,将一个响应聚合起来。

聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等于SQL GROUPBY和SQL聚合函数。在Elasticsearch中, 您有执行搜索返回hits (命中结果),并且同时返回聚合结果,把一个响应中的所有hits (命中结果)分隔开的能力。这是非常强大且有效的,您可以执行查询和多个聚合,并且在一次使用中得到各自的(任何一个的)返回结果,使用次简洁和简化的API来避免网络往返。

聚合

GET /bank/_search { "query": { "match": { "address": "mill" } }, "aggs": { ## 聚合 "ageAgg": { ## 聚合名字 "terms": { "field": "age", ## 聚合属性 "size": 10 } } } }

返回结果

ElasticSearch中如何进行高级的聚合分析操作?

GET /bank/_search { "query": { "match": { "address": "mill" } }, "aggs": { ## 聚合 "ageAgg": { ## 聚合名字 "terms": { "field": "age", ## 聚合属性 "size": 10 } }, "ageAvg":{ 查询平均值 "avg": { "field": "age" ##年龄的 } } }, "size": 0 ##只看 聚合结果 }

案例

查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资

GET /bank/_search { "query": { "match_all": {} }, "aggs": { "ageAgg": { "terms": { "field": "age", "size": 100 }, "aggs": { "genderAgg": { "terms": { "field": "gender.keyword" }, "aggs": { "ageAvg": { "avg": { "field": "balance" } } } }, "ageBalabceAvg":{ "avg": { "field": "balance" } } } } } }

Mapping映射

GET /bank/_mapping 查询类型 PUT /my-index { "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, ## keyword 精确匹配 非全文检索 "name": { "type": "text" } ## text 全文检索 非精确匹配 } } }

不能直接修改映射 采用数据迁移

PUT /newbank { "mappings": { "properties": { //具体规则 "account_number": { "type": "long" }, "address": { "type": "text" }, "age": { "type": "integer" } "gender": { "type": "keyword" }, "lastname": { "type": "keyword", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } _reindex迁移 POST _reindex { "source": { //迁移元数据 "index": "bank", "type": "account" }, "dest": { //迁移新数据 "index": "newbank" } }