Elasticsearch5.0 Java Api(七) -- 聚合查询

  测试聚合查询功能

 1 package com.juyun.test;
 2
 3 import java.net.InetAddress;
 4 import java.util.List;
 5
 6 import org.elasticsearch.action.search.SearchResponse;
 7 import org.elasticsearch.client.Client;
 8 import org.elasticsearch.common.settings.Settings;
 9 import org.elasticsearch.common.transport.InetSocketTransportAddress;
10 import org.elasticsearch.index.query.QueryBuilder;
11 import org.elasticsearch.index.query.QueryBuilders;
12 import org.elasticsearch.search.SearchHit;
13 import org.elasticsearch.search.SearchHits;
14 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
15 import org.elasticsearch.search.aggregations.AggregationBuilder;
16 import org.elasticsearch.search.aggregations.AggregationBuilders;
17 import org.elasticsearch.search.aggregations.bucket.children.Children;
18 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
19 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
20 import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
21 import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
22 import org.elasticsearch.transport.client.PreBuiltTransportClient;
23
24 public class ElasticSearchAggregation {
25
26     private static Client client;
27
28     /**
29      * 聚合查询
30      * @param args
31      */
32     public static void main(String[] args) {
33
34         try {
35             // 设置集群名称
36             Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
37             // 创建client
38             client = new PreBuiltTransportClient(settings)
39                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.157"), 9300));
40
41             long startTime=System.currentTimeMillis(); // 获取开始时间
42
43             agg("flow", "data");
44             //childQuery("newindex","http");
45
46             long endTime=System.currentTimeMillis(); //获取结束时间
47             System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
48
49
50             // 关闭client
51             client.close();
52
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }
57
58     // 计算inbyte和outbyte的平均值
59     public static void agg(String indexName, String typeName) {
60         SearchResponse response = client
61                                 .prepareSearch(indexName)
62                                 .setTypes(typeName)
63                                 .addAggregation(AggregationBuilders.avg("avgOfInbyte").field("inbyte"))
64                                 .addAggregation(AggregationBuilders.avg("avgOfOutbyte").field("outbyte"))
65                                 .addAggregation(AggregationBuilders.sum("sumOfInbyte").field("inbyte"))
66                                 .addAggregation(AggregationBuilders.sum("sumOfOutbyte").field("outbyte"))
67                                 .get();
68
69         InternalAvg avgOfInbyte  = response.getAggregations().get("avgOfInbyte");
70         InternalAvg avgOfOutbyte  = response.getAggregations().get("avgOfOutbyte");
71         InternalSum sumOfInbyte = response.getAggregations().get("sumOfInbyte");
72         InternalSum sumOfOutbyte = response.getAggregations().get("sumOfOutbyte");
73         System.out.println("inbyte的平均值是:"+avgOfInbyte.getValue());
74         System.out.println("outbyte的平均值是:"+avgOfOutbyte.getValue());
75         System.out.println("inbyte的总值是:"+sumOfInbyte.getValue());
76         System.out.println("outbyte的总值是:"+sumOfOutbyte.getValue());
77     }
78 }

ElasticSearchAggregation

时间: 2024-10-18 11:05:28

Elasticsearch5.0 Java Api(七) -- 聚合查询的相关文章

Elasticsearch5.0 Java Api(一) -- 插入索引

Elasticsearch5.X官方手册 Elasticsearch5.X官方手册中文版 Elasticsearch5.X Java Api官方手册 导入maven依赖 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.0.0</version> </depend

Elasticsearch5.0 Java Api(八) -- 常用DSL查询

测试常用的DSL组合查询功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 5 import org.elasticsearch.action.search.SearchResponse; 6 import org.elasticsearch.client.Client; 7 import org.elasticsearch.common.settings.Settings; 8 import org.elasticse

Elasticsearch5.0 Java Api(四) -- 检索索引

测试检索索引库的功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 5 import org.elasticsearch.client.Client; 6 import org.elasticsearch.common.settings.Settings; 7 import org.elasticsearch.common.transport.InetSocketTransportAddress; 8 import or

Elasticsearch5.0 Java Api(三) -- 删除索引

测试删除索引的功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 6 import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; 7 import org.elasticsearch.action.admin.indices.create.CreateInd

Elasticsearch5.0 Java Api(二) -- 更新索引

elasticsearch提供了多种更新索引的方式,这里简单介绍其中五种 1 package com.juyun.test; 2 3 import java.io.IOException; 4 import java.net.InetAddress; 5 import java.util.concurrent.ExecutionException; 6 7 import org.elasticsearch.action.index.IndexRequest; 8 import org.elast

Elasticsearch5.0 Java Api(六) -- 批量导入索引

将计算机本地的.json格式文件中的数据,批量导入到索引库中 1 package com.juyun.test; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.net.InetAddress; 9 imp

Elasticsearch5.0 Java Api(五) -- 批量导出索引

从索引库中将检索到的数据导出到外部文件,文件格式为.json 1 package com.juyun.test; 2 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 import java.net.InetAddress; 8 import java.net.UnknownHostException; 9 10

elasticsearch-5.x JAVA API(002)

elasticsearch-5.x JAVA API 第二部分:集群健康与段合并 1. 集群健康 首先定义一个类来存储集群健康信息 package com.zw.elasticsearch.cluster; public class ClusterHealth { // 集群名 private String clusterName; // 集群中节点数目 private int number_of_node; // 活跃总分片数目 private int number_of_activeShar

Dynamics CRM 2015/2016 Web API:聚合查询

各位小伙伴们,今天是博主2016年发的第一篇文章,首先祝大家新年快乐,工资Double,哈哈.今天我们来看一个比较重要的Feature--使用Web API执行FetchXML查询!对的,各位,你们没有听错,使用Web API执行FetchXML查询.在过去我们做这样的事情可是要花九牛二虎之力哟,拼接大量的SOAP消息体而且还容易出错.现在好了,我们自己可以在URL里面加上我们想要执行的FetchXML即可,系统则会乖乖的把我们想要的数据返回回来,是不是很美好呢! 言归正传,Web API的数据