首页 > 其他分享 >elasticsearch之metric聚合

elasticsearch之metric聚合

时间:2022-12-27 12:33:39浏览次数:72  
标签:index 聚合 field agg metric request age elasticsearch response

1、背景

此篇文章简单的记录一下 elasticsearchmetric聚合操作。比如求 平均值、最大值、最小值、求和、总计、去重总计等。

2、准备数据

2.1 准备mapping

PUT /index_person
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
      },
      "name": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "class":{
        "type": "text",
        "fielddata": true
      },
      "province":{
        "type": "keyword"
      }
    }
  }
}

2.2 准备数据

PUT /index_person/_bulk
{"index":{"_id":1}}
{"id":1, "name":"张三","age":18,"class":"大一班","province":"湖北"}
{"index":{"_id":2}}
{"id":2, "name":"李四","age":19,"class":"大一班","province":"湖北"}
{"index":{"_id":3}}
{"id":3, "name":"王武","age":20,"class":"大二班","province":"北京"}
{"index":{"_id":4}}
{"id":4, "name":"赵六","age":21,"class":"大三班技术班","province":"北京"}
{"index":{"_id":5}}
{"id":5, "name":"钱七","age":22,"class":"大三班","province":"湖北"}

3、metric聚合

3.1 max 平均值

3.1.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "max": {
        "field": "age",
        "missing": 10
      }
    }
  }
}


POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "max": {
        "script": {
          "lang": "painless",
          "source": """
            doc.age
          """
        }
      }
    }
  }
}


POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "max": {
        "field": "age", 
        "script": {
          "lang": "painless",
          "source": """
            _value * params.a
          """,
          "params": {
            "a": 2
          }
        }
      }
    }
  }
}

3.1.2 java代码

@Test
@DisplayName("最大值聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.max(max ->
                                    // 聚合的字段
                                    max.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

@Test
@DisplayName("脚本聚合")
public void test02() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.max(max ->
                                    max.script(script ->
                                            script.inline(inline ->
                                                    inline.lang(ScriptLanguage.Painless)
                                                            // 脚本表达式
                                                            .source("doc.age")
                                            )
                                    )
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

@Test
@DisplayName("值脚本聚合")
public void test03() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.max(max ->
                                    // 指定参与聚合的字段
                                    max.field("age")
                                            .script(script ->
                                                    script.inline(inline ->
                                                            inline.lang(ScriptLanguage.Painless)
                                                                    // 脚本表达式
                                                                    .source("_value * params.plus")
                                                                    // 参数
                                                                    .params("plus", JsonData.of(2))
                                                    )
                                            )
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.2 min最小值

3.2.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "min": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.2.2 java

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "min": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.3 min最小值

3.3.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "avg": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.3.2 java

@Test
@DisplayName("平均值聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.avg(avg ->
                                    // 聚合的字段
                                    avg.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.4 min最小值

3.4.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "sum": {
        "field": "age",
        "missing": 10
      }
    }
  }
}

3.4.2 java

@Test
@DisplayName("求和聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.sum(sum ->
                                    // 聚合的字段
                                    sum.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.5 count(*)

3.5.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "value_count": {
        "field": "province",
        "missing": 10
      }
    }
  }
}

3.5.2 java

@Test
@DisplayName("count(*)聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.valueCount(valueCount ->
                                    // 聚合的字段
                                    valueCount.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.6 count(distinct)

3.6.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "cardinality": {
        "field": "province",
        "missing": 10
      }
    }
  }
}

3.6.2 java

@Test
@DisplayName("count(distinct)聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.cardinality(cardinality ->
                                    // 聚合的字段
                                    cardinality.field("province")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.7 stat (max,min,avg,count,sum)

3.7.1 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "agg_01": {
      "stats": {
        "field": "avg",
        "missing": 10
      }
    }
  }
}

3.7.2 java

@Test
@DisplayName("stat聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .aggregations("agg_01", agg ->
                            agg.stats(stats ->
                                    // 聚合的字段
                                    stats.field("age")
                                            // 如果聚合的文档缺失这个字段,则给10
                                            .missing(10)
                            )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.8 聚合后返回每个聚合涉及的文档

3.8.1 需求

根据 province进行terms聚合,然后获取每个terms聚合 age最大的那个文档。

3.8.2 dsl

POST /index_person/_search
{
  "size": 0,
  "query": {
    "range": {
      "age": {
        "gte": 10
      }
    }
  },
  "aggs": {
    "agg_01": {
      "terms": {
        "field": "province"
      },
      "aggs": {
        "agg_02": {
          "top_hits": {
            "from": 0,
            "size": 1,
            "sort": [
              {
                "age": {"order": "desc"}
              }
            ],
            "_source": {
              "includes": ["id","age","name"]
            }
          }
        }
      }
    }
  }
}

3.8.3 java

@Test
@DisplayName("top hits 聚合")
public void test01() throws IOException {
    SearchRequest request = SearchRequest.of(searchRequest ->
            searchRequest.index("index_person")
                    .size(0)
                    .query(query -> query.range(range -> range.field("age").gt(JsonData.of(10))))
                    .aggregations("agg_01", agg ->
                            agg.terms(terms ->
                                            terms.field("province")
                                    )
                                    .aggregations("agg_02", subAgg ->
                                            subAgg.topHits(topHits ->
                                                    topHits.from(0)
                                                            .size(1)
                                                            .sort(sort -> sort.field(field -> field.field("age").order(SortOrder.Desc)))
                                                            .source(source -> source.filter(filter -> filter.includes(Arrays.asList("id", "age", "name"))))
                                            )
                                    )
                    )
    );
    System.out.println("request: " + request);
    SearchResponse<String> response = client.search(request, String.class);
    System.out.println("response: " + response);
}

3.8.4 运行结果

运行结果

4、完整代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/es/es8-api/src/main/java/com/huan/es8/aggregations/metric

5、参考文档

1、https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-metrics-max-aggregation.html

标签:index,聚合,field,agg,metric,request,age,elasticsearch,response
From: https://www.cnblogs.com/huan1993/p/17007807.html

相关文章

  • Elasticsearch查询及聚合类DSL语句宝典
    作者:京东科技纪海雨前言随着使用es场景的增多,工作当中避免不了去使用es进行数据的存储,在数据存储到es当中以后就需要使用DSL语句进行数据的查询、聚合等操作,DSL对SE的意......
  • Elasticsearch查询及聚合类DSL语句宝典
    作者:京东科技纪海雨前言随着使用es场景的增多,工作当中避免不了去使用es进行数据的存储,在数据存储到es当中以后就需要使用DSL语句进行数据的查询、聚合等操作,DSL对SE的意义......
  • elasticsearch基础入门
    本文由简悦SimpRead转码,原文地址blog.csdn.net小伙伴们,你们好呀,我是老寇,跟我一起学习es7.6.2注:请点击我,获取源码目录一、引入依赖配置pom.xml二、配置app......
  • Jaeger&ElasticSearch存储链路追踪数据
    前言Jaeger的allinone镜像下是用内存存储(或是临时文件格式存储),容器重启,数据丢失。生产环境下更多是存储到es或是cassandra,这样对于查询或是系统扩展是比较方便的。此......
  • elasticsearch中SearchApi的详解
     ​搜索流程当一个搜索请求被发送到某个节点时,这个节点就变成了协调节点。 这个节点的任务是广播查询请求到所有相关分片并将它们的响应整合成全局排序后的结果集合,这个结......
  • Rest操作ES(5)- 聚合查询
    1.数据聚合聚合(aggregations)可以让我们极其方便的实现对数据的统计、分析、运算。例如:什么品牌的手机最受欢迎?这些手机的平均价格、最高价格、最低价格?这些手机每月的......
  • Elasticsearch全文检索引擎复习笔记
    Elasticsearch全文检索引擎复习笔记Elasticsearch是一个基于Lucene的搜索引擎。它提供了一个分布式、多租户的全文搜索引擎,能够为应用程序提供实时的、结构化和非结构......
  • elasticsearch 安装8.5.3
    下载地址:https://www.elastic.co/cn/downloads/支持:https://www.elastic.co/cn/support/matrix#elastrcsearch.yml需要注意xpack安全配置修改为:falseingest.geoip.d......
  • ElasticSearch系列---【Es的快速入门文档】
    Es的快速入门文档1.对比数据库理解ElasticSearch是面向文档型数据库,一条数据在这里就是一个文档。 注意:从ElasticSearch6.X开始,一个Index下只能包含一个Type,因此,在ElasticS......
  • (二)elasticsearch 源码目录
    在阅读源码之前,我们先来看看整个项目的结构:(同(一)elasticsearch编译和启动,我们使用版本7.4.0作为示例).ci,持续集成配置.github,里面有PULL_REQUEST_TEMPLATE.md,是关于pr......