Elasticsearch5.0 Java Api -- 聚合查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Elasticsearch5.0 Java Api -- 聚合查询的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch5.0 Java Api -- 聚合查询
Elasticsearch5.0 Java Api -- 检索索引
Elasticsearch5.0 Java Api -- 更新索引
Elasticsearch5.0 Java Api -- 常用DSL查询