当前位置 : 主页 > 编程语言 > java >

ElasticSearch--aggregations聚合分析

来源:互联网 收集:自由互联 发布时间:2023-03-22
聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等于SQL GROUPBY和SQL聚合函数。在Elasticsearch中, 您有执行搜索返回hits (命中结果),并且同时返回聚合结果,把一个响

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

聚合

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

返回结果 image.png

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" } } } } } }

image.png

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" } }
上一篇:ElasticSearch-分词器 analyze-ik
下一篇:没有了
网友评论