java操作elasticsearch实现组合桶聚合
1、terms分组查询
//分组聚合
@Test
public void test40() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//以年龄分组,组名为:terms
AggregationBuilder builder = AggregationBuilders.terms("terms").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet();
Terms terms = response.getAggregations().get("terms");
for(Terms.Bucket term:terms.getBuckets()) {
System.out.println(term.getKey()+" "+term.getDocCount());
}
}
2、terms query聚合查询
//filter聚合
@Test
public void test41() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//以年龄分组,并查询年龄为20的有多少人
QueryBuilder queryBuilder = QueryBuilders.termQuery("age", 20);
AggregationBuilder builder = AggregationBuilders.filter("filter",queryBuilder);
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet();
Filter filter = response.getAggregations().get("filter");
System.out.println(filter.getDocCount());
}
3、filters聚合
//filters聚合:指定多个过滤条件
@Test
public void test42() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//查询heijiu changge中各有多少个文档
QueryBuilder queryBuilder = QueryBuilders.termQuery("interests", "hejiu");
QueryBuilder queryBuilder2 = QueryBuilders.termQuery("interests", "changge"); AggregationBuilder builder = AggregationBuilders.filters("filters",queryBuilder,queryBuilder2);
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet();
Aggregation filter = response.getAggregations().get("filters");
System.out.println(filter.toString());
}
4、range聚合
//range聚合:范围分组过滤条件
@Test
public void test43() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//统计年龄在50以下的,年龄在25至50之间,年龄在25以上的人数
AggregationBuilder builder = AggregationBuilders
.range("range")
.field("age")
.addUnboundedTo(50)
.addRange(25,50)
.addUnboundedFrom(25);
SearchResponse response = client.prepareSearch("lib3").addAggregation(builder).execute().actionGet();
Aggregation filter = response.getAggregations().get("range");
System.out.println(filter.toString());
}
5、missing聚合
//missing聚合:为空聚合统计
@Test
public void test44() throws UnknownHostException{
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//统计价格为空的个数
AggregationBuilder builder = AggregationBuilders.missing("missing").field("price");
SearchResponse response = client.prepareSearch("lib4").addAggregation(builder).execute().actionGet();
Aggregation filter = response.getAggregations().get("missing");
System.out.println(filter.toString());
}
下一篇博客本人将书写java操作elasticsearch实现集群管理。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/
技术在于交流!
java操作elasticsearch实现组合桶聚合的更多相关文章
- java操作elasticsearch实现query String
1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...
- java操作elasticsearch实现聚合查询
1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- java操作elasticsearch实现查询删除和查询所有
后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...
- java操作elasticsearch实现批量添加数据(bulk)
java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...
- java操作elasticsearch实现基本的增删改查操作
一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticse ...
- Java操作ElasticSearch之创建客户端连接
Java操作ElasticSearch之创建客户端连接 3 发布时间:『 2017-09-11 17:02』 博客类别:elasticsearch 阅读(3157) Java操作ElasticSe ...
- java 操作elasticsearch之搭建测试项目环境
在创建项目之前请确认maven是否安装好,在此我是以环境都搭建好的情况下进行示范,现在以eclipse开发工具为例,具体操作如下: 1.创建maven项目 File - new -other 2.在p ...
随机推荐
- HttpClients+Jsoup抓取笔趣阁小说,并保存到本地TXT文件
前言 首先先介绍一下Jsoup:(摘自官网) jsoup is a Java library for working with real-world HTML. It provides a very ...
- Host '127.0.0.1' is not allowed to connect to this MySQL server
错误:Host '127.0.0.1' is not allowed to connect to this MySQL server 一般原因: MySQL数据库的配置文件my.i ...
- element UI table 过滤 筛选问题
一.问提描述 使用elementUI table 官方筛选案例,发现筛选不是服务器端筛选,而是浏览器端对每一页进行单独筛选. 如何在服务器端筛选? 二.查询Element UI 官网table组 ...
- springMVC_11拦截器实现登录
一. 思路 controller实现核对用户名和密码,如果核对正确则保存到session中并且跳转到主页 系统中包含诸多界面,部分界面不需要登录即可进行访问,通过拦截器实现判断是否是不需要登录的界 ...
- word转PDF,PDF转Image,使用oppenOffice注意事项等
最近在电子合同等项目中需要把word或者pdf转换成image,用到了openOffice把word转换pdf,以及把pdf转换成图片 感谢小伙伴张国清花费了三天时间来实现了此功能.下面我将把具体的步 ...
- 定时任务Task
使用注解@EnableScheduling开启定时任务,会自动扫描 定义@Component作为组件被容器扫描 对于EnableScheduling是注解在启动类上,很多开关配置都会再启动类中进行设置 ...
- Children’s Queue(hdu1297+递推)
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- STL中的Set用法(详+转)
set是STL中一种标准关联容器(vector,list,string,deque都是序列容器,而set,multiset,map,multimap是标准关联容器),它底层使用平衡的搜索树——红黑树实 ...
- elementUI vue 编辑中的input的验证残留清除
当使用编辑的时候, 假如上次的验证没通过, 报红了, 下次再点击编辑的时候还会报红,因此要清除验证残留, 方式有两种: this.$refs["from"].resetFields ...
- cf1121F. Compress String(后缀自动机)
题意 题目链接 Sol 居然出个SAM板子也是没谁了233 #include<bits/stdc++.h> #define Pair pair<int, int> #defin ...