运行结果

统计每个学员的总成绩

这个是索引库使用通配符

优先在本地查询

只在本地节点中查询

只在指定id的节点里面进行查询

查询指定分片的数据

参考代码ESTestAggregation.java

package com.dajiangtai.djt_spider.elasticsearch;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.junit.Before;
import org.junit.Test;
/**
* Aggregation 操作
*
* @author 大讲台
*
*/
public class ESTestAggregation {
private TransportClient client; @Before
public void test0() throws UnknownHostException { // 开启client.transport.sniff功能,探测集群所有节点
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "escluster")
.put("client.transport.sniff", true).build();
// on startup
// 获取TransportClient
client = TransportClient
.builder()
.settings(settings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("master"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("slave1"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("slave2"), 9300));
}
/**
* Aggregation 分组统计相同年龄学员个数
* @throws Exception
*/
@Test
public void test1() throws Exception {
SearchRequestBuilder builder = client.prepareSearch("djt1");
builder.setTypes("user")
.setQuery(QueryBuilders.matchAllQuery())
//按年龄分组聚合统计
.addAggregation(AggregationBuilders.terms("by_age").field("age").size(0))
; SearchResponse searchResponse = builder.get();
//获取分组信息
Terms terms = searchResponse.getAggregations().get("by_age");
List<Bucket> buckets = terms.getBuckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.getKey()+":"+bucket.getDocCount());
}
} /**
* Aggregation 分组统计每个学员的总成绩
* @throws Exception
*/
@Test
public void test2() throws Exception {
SearchRequestBuilder builder = client.prepareSearch("djt2");
builder.setTypes("user")
.setQuery(QueryBuilders.matchAllQuery())
//按姓名分组聚合统计
.addAggregation(AggregationBuilders.terms("by_name")
.field("name")
.subAggregation(AggregationBuilders.sum("sum_score")
.field("score"))
.size(0))
;
SearchResponse searchResponse = builder.get();
//获取分组信息
Terms terms = searchResponse.getAggregations().get("by_name");
List<Bucket> buckets = terms.getBuckets();
for (Bucket bucket : buckets) {
Sum sum = bucket.getAggregations().get("sum_score");
System.out.println(bucket.getKey()+":"+sum.getValue());
}
} /**
* 支持多索引和多类型查询
* @throws Exception
*/
@Test
public void test3() throws Exception {
SearchRequestBuilder builder
= client//.prepareSearch("djt1","djt2")//可以指定多个索引库
.prepareSearch("djt*")//索引库可以使用通配符
.setTypes("user");//支持多个类型,但不支持通配符 SearchResponse searchResponse = builder.get(); SearchHits hits = searchResponse.getHits();
SearchHit[] hits2 = hits.getHits();
for (SearchHit searchHit : hits2) {
System.out.println(searchHit.getSourceAsString());
}
}
/**
* 分片查询方式
* @throws Exception
*/
@Test
public void test4() throws Exception {
SearchRequestBuilder
builder = client.prepareSearch("djt3")
.setTypes("user")
//.setPreference("_local")
//.setPreference("_only_local")
//.setPreference("_primary")
//.setPreference("_replica")
//.setPreference("_primary_first")
//.setPreference("_replica_first")
//.setPreference("_only_node:crKxtA2fRTG1UZdPN8QtaA")
//.setPreference("_prefer_node:nJL_MqcsSle6gY7iujoAlw")
.setPreference("_shards:3")
;
SearchResponse searchResponse = builder.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits2 = hits.getHits();
for (SearchHit searchHit : hits2) {
System.out.println(searchHit.getSourceAsString());
}
}
/**
* 极速查询:通过路由插入数据(同一类别数据在一个分片)
* @throws Exception
*/
@Test
public void test5() throws Exception {
Acount acount = new Acount("13602546655","tom1","male",16);
Acount acount2 = new Acount("13602546655","tom2","male",17);
Acount acount3 = new Acount("13602546655","tom3","male",18);
Acount acount4 = new Acount("18903762536","john1","male",28);
Acount acount5 = new Acount("18903762536","john2","male",29);
Acount acount6 = new Acount("18903762536","john3","male",30);
List<Acount> list = new ArrayList<Acount>();
list.add(acount);
list.add(acount2);
list.add(acount3);
list.add(acount4);
list.add(acount5);
list.add(acount6); BulkProcessor bulkProcessor = BulkProcessor.builder(
client,
new BulkProcessor.Listener() { public void beforeBulk(long executionId, BulkRequest request) {
// TODO Auto-generated method stub
System.out.println(request.numberOfActions());
} public void afterBulk(long executionId, BulkRequest request,
Throwable failure) {
// TODO Auto-generated method stub
System.out.println(failure.getMessage());
} public void afterBulk(long executionId, BulkRequest request,
BulkResponse response) {
// TODO Auto-generated method stub
System.out.println(response.hasFailures());
}
})
.setBulkActions(1000) // 每个批次的最大数量
.setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB))// 每个批次的最大字节数
.setFlushInterval(TimeValue.timeValueSeconds(5))// 每批提交时间间隔
.setConcurrentRequests(1) //设置多少个并发处理线程
//可以允许用户自定义当一个或者多个bulk请求失败后,该执行如何操作
.setBackoffPolicy(
BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
.build();
for (Acount a : list) {
ObjectMapper mapper = new ObjectMapper(); byte[] json = mapper.writeValueAsBytes(a);
bulkProcessor.add(new IndexRequest("djt3", "user")
.routing(a.getPhone().substring(0, 3))
.source(json));
} //阻塞至所有的请求线程处理完毕后,断开连接资源
bulkProcessor.awaitClose(3, TimeUnit.MINUTES);
client.close();
}
/**
* 极速查询:通过路由极速查询,也可以通过分片shards查询演示
*
* @throws Exception
*/
@Test
public void test6() throws Exception {
SearchRequestBuilder builder = client.prepareSearch("djt3")//可以指定多个索引库
.setTypes("user");//支持多个类型,但不支持通配符
builder.setQuery(QueryBuilders.matchAllQuery())
.setRouting("13602546655".substring(0, 3))
//.setRouting("18903762536".substring(0, 3))
;
SearchResponse searchResponse = builder.get(); SearchHits hits = searchResponse.getHits();
SearchHit[] hits2 = hits.getHits();
for (SearchHit searchHit : hits2) {
System.out.println(searchHit.getSourceAsString());
}
}
}

elastisSearch-aggregations的更多相关文章

  1. hive的Query和Insert,Group by,Aggregations(聚合)操作

    1.Query (1)分区查询 在查询的过程中,采用那个分区来查询是通过系统自动的决定,但是必须是在分区列上基于where子查询. SELECT page_views.* FROM page_view ...

  2. ElasticSearch 的 聚合(Aggregations)

    Elasticsearch有一个功能叫做 聚合(aggregations) ,它允许你在数据上生成复杂的分析统计.它很像SQL中的 GROUP BY 但是功能更强大. Aggregations种类分为 ...

  3. aggregation 详解2(metrics aggregations)

    概述 权值聚合类型从需要聚合的文档中取一个值(value)来计算文档的相应权值(比如该值在这些文档中的max.sum等). 用于计算的值(value)可以是文档的字段(field),也可以是脚本(sc ...

  4. aggregation 详解4(pipeline aggregations)

    概述 管道聚合处理的对象是其它聚合的输出(桶或者桶的某些权值),而不是直接针对文档. 管道聚合的作用是为输出增加一些有用信息. 管道聚合大致分为两类: parent 此类聚合的"输入&quo ...

  5. aggregations 详解1(概述)

    aggregation分类 aggregations —— 聚合,提供了一种基于查询条件来对数据进行分桶.计算的方法.有点类似于 SQL 中的 group by 再加一些函数方法的操作. 聚合可以嵌套 ...

  6. Elasticsearch aggregations API

    聚合能力 Aggregation API 类似 SQL 中的 GROUP BY 语句,可以以某个字段来进行分组. Aggregation API 支持分级分组,多级的分组过程是由外到里的. Aggre ...

  7. Spark学习之路(十一)—— Spark SQL 聚合函数 Aggregations

    一.简单聚合 1.1 数据准备 // 需要导入spark sql内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSess ...

  8. Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations

    一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...

  9. 使用 ElasticSearch Aggregations 进行统计分析(转)

    https://blog.csdn.net/cs729298/article/details/68926969 ElasticSearch 的特点随处可见:基于 Lucene 的分布式搜索引擎,友好的 ...

  10. Elasticsearch系列(二)--query、filter、aggregations

    本文基于ES6.4版本,我也是出于学习阶段,对学习内容做个记录,如果文中有错误,请指出. 实验数据: index:book type:novel mappings: { "mappings& ...

随机推荐

  1. Python中的import

    模块(module):用来从逻辑(实现一个功能)上组织Python代码(变量.函数.类),本质就是*.py文件.文件是物理上组织方式"module_name.py",模块是逻辑上组 ...

  2. 【error】no type named ‘type’ in ‘class std::result_of<void

    Q: std::thread fs_module(fs_process, prob_orig, fb_sz, line_num, probp, plabel, std::ref(confidence_ ...

  3. Softmax回归介绍

    把输入值当成幂指数求值,再正则化这些结果值.这个幂运算表示,更大的证据对应更大的假设模型(hypothesis)里面的乘数权重值.反之,拥有更少的证据意味着在假设模型里面拥有更小的乘数系数.假设模型里 ...

  4. pta 奇数值结点链表&&单链表结点删除

    本题要求实现两个函数,分别将读入的数据存储为单链表.将链表中奇数值的结点重新组成一个新的链表.链表结点定义如下: struct ListNode { int data; ListNode *next; ...

  5. Knight Tournament 合并区间

    Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...

  6. test20181017 B君的第一题

    题意 分析 考场做法 对p的幂打表发现,我们一定可以把x和y的二进制位从低到高依次调整成0. 具体而言,从0次幂开始每两个分为一组a,b,那么0,a,b,a+b组合中的一种可以将x,y的对应二进制位都 ...

  7. 用pthon来写个跳板机

    用pthon来写个跳板机   1.需求 程序一:1.后台管理- 堡垒机上创建用户和密码(堡垒机root封装的类,UserProfile表)- .bashrc /usr/bin/python3 /dat ...

  8. Thrift 个人实战--初次体验Thrift(转)

    前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码. 不过Thrift的实现, 简单使用离实际生产环境还 ...

  9. MySQL--自增列持久化问题

    ====================================================================== 自增列持久化问题 5.5/5.6/5.7三个版本中,MyS ...

  10. SQL Server系统表介绍与使用

    关于SQL Server数据库的一切信息都保存在它的系统表格里.我怀疑你是否花过比较多的时间来检查系统表格,因为你总是忙于用户表格.但是,你可能需要偶尔做一点不同寻常的事,例如数据库所有的触发器.你可 ...