elasticsearch 常见查询及聚合的JAVA API
ES 常见查询
(1)根据ID 进行单个查询
GetResponse response = client.prepareGet("accounts", "person", "1").setOperationThreaded(false).get();
相对于sql 的 select * from accounts.person where id=1 ;
(2)分页查询所有记录
QueryBuilder qb=new MatchAllQueryBuilder();
SearchResponse response= client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFrom(0)
.setSize(100).get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
response.getHits()是所有命中记录 相较于sql select * from accounts.person limit 100;
(3)根据多条件组合与查询
QueryBuilder qb=QueryBuilders.boolQuery().must(QueryBuilders.termQuery("title","JAVA开发工程师")).must(QueryBuilders.termQuery("age",30)) ; SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFrom(0)
.setSize(100);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
must 就像sql里的and 相较于sql select * from accounts.person where title='JAVA开发工程师' and age=30
(4)多条件或查询
QueryBuilder qb=QueryBuilders.termQuery("user","kimchy14");
QueryBuilder qb1=QueryBuilders.termQuery("user","kimchy15"); SortBuilder sortBuilder=SortBuilders.fieldSort("age");
sortBuilder.order(SortOrder.DESC);
QueryBuilder s=QueryBuilders.boolQuery().should(qb).should(qb1);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).addSort(sortBuilder).setFrom(0)
.setSize(100);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
should 就像sql里的or SortBuilder 的作用不言而喻就是用来排序 以上代码相较于sql select * from accounts.person where user='kimchy14' or user='kimchy15' ;
(5)范围查询
// RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
// RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gt(30 );
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gte(30 );
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0)
.setSize(100);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
范围查询rangeQuery.from(30,true)方法是大于30 后面的参数是是否包含 为true的话就是大于等于30 to就相当于小于 如果也有包含参数为true的话就是小于等于 gt 是大于 gte是大于等于 lt是小于 lte是小于等于 第一句的builder就相当于 select * from accounts.person where age >=30 and age<=30;
(6)包含查询
List<String> strs=new ArrayList<>();
strs.add("kimchy14");
strs.add("kimchy15");
strs.add("kimchy16");
QueryBuilder qb=QueryBuilders.termsQuery("user",strs); SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFetchSource("age",null).setFrom(0)
.setSize(100);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
包含查询使用termsQuery 可以传列表 也可以传多个参数 或者数组 setFetchSource有两个参数 第一个参数是包含哪些参数 第二个参数是排除哪些参数 以上这段代码就相当于sql select age from accounts.person where user in ('kimchy14','kimchy15','kimchy16');
(7)专门按id进行的包含查询
QueryBuilder qb=QueryBuilders.idsQuery(0+""); SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFetchSource("age",null).setFrom(0)
.setSize(100);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
(8)按通配符查询
QueryBuilder qb = QueryBuilders.wildcardQuery("user", "k*hy17*");
//Fuzziness fuzziness=Fuzziness.fromEdits(2); // QueryBuilder qb = QueryBuilders.fuzzyQuery("user","mchy2").fuzziness(fuzziness);
//QueryBuilder qb = QueryBuilders.prefixQuery("user", "kimchy2");
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFetchSource("user",null).setFrom(0)
.setSize(100);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
通配符查询像我们sql里的like 但是还不一样 like的百分号可以加到前后 elasticsearch技术解析与实战中有一句话 是这么说的 为了避免极端缓慢的通配符查询 通配符索引词不应该以一个通配符开头 通配符查询应该避免以通配符开头
常见统计 统计分为指标 和 桶 桶就是我们统计的样本 指标就是我们平时所查的count sum 与sql不一样的是 我们还可以将统计的样本拿到 就是response.getHits
(9)统计count
AggregationBuilder termsBuilder = AggregationBuilders.count("ageCount").field("age"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
ValueCount valueCount= response.getAggregations().get("ageCount");
long value=valueCount.getValue();
这段代码就相当于 sql select count(age) ageCount form accounts.person where age >=30 and age<=30
(10)查询最大值
AggregationBuilder termsBuilder = AggregationBuilders.max("max").field("age"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
Max valueCount= response.getAggregations().get("max");
double value=valueCount.getValue();
(11)统计总和
AggregationBuilder termsBuilder = AggregationBuilders.sum("sum").field("age"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
Sum valueCount= response.getAggregations().get("sum");
double value=valueCount.getValue();
(12)平均数
AggregationBuilder termsBuilder = AggregationBuilders.avg("avg").field("age"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
Avg valueCount= response.getAggregations().get("avg");
double value=valueCount.getValue();
(13)统计样本基本指标
AggregationBuilder termsBuilder = AggregationBuilders.stats("stats").field("age"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get();
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits.getHits()){
logger.log(Level.INFO , hit.getSourceAsString());
}
Stats valueCount= response.getAggregations().get("stats");
logger.log(Level.INFO,"max"+valueCount.getMaxAsString());
logger.log(Level.INFO,"avg"+valueCount.getAvgAsString());
logger.log(Level.INFO,"sum"+valueCount.getSumAsString());
logger.log(Level.INFO,"min"+valueCount.getMinAsString());
logger.log(Level.INFO,"count"+valueCount.getCount());
分组统计 相当于group by 后拿各组指标进行统计
(14)分组求各组数据
AggregationBuilder termsBuilder = AggregationBuilders.terms("by_age").field("age");
AggregationBuilder sumBuilder=AggregationBuilders.sum("ageSum").field("age");
AggregationBuilder avgBuilder=AggregationBuilders.avg("ageAvg").field("age");
AggregationBuilder countBuilder=AggregationBuilders.count("ageCount").field("age"); termsBuilder.subAggregation(sumBuilder).subAggregation(avgBuilder).subAggregation(countBuilder);
//TermsAggregationBuilder all = AggregationBuilders.terms("age").field("age");
//all.subAggregation(termsBuilder);
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(36,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFetchSource(null,"gender").setFrom(0).setSize(100).addAggregation(termsBuilder);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get(); Aggregations terms= response.getAggregations();
for (Aggregation a:terms){
LongTerms teamSum= (LongTerms)a;
for(LongTerms.Bucket bucket:teamSum.getBuckets()){
logger.info(bucket.getKeyAsString()+" "+bucket.getDocCount()+" "+((Sum)bucket.getAggregations().asMap().get("ageSum")).getValue()+" "+((Avg)bucket.getAggregations().asMap().get("ageAvg")).getValue()+" "+((ValueCount)bucket.getAggregations().asMap().get("ageCount")).getValue()); }
}
第一行 termsBuilder 就相当于根据年龄对数据进行分组 group by 后面对sumBuilder avgBuilder countBuilder等就是在组内 求和 求平均数 求数量
(15)多分组求各组数据
TermsAggregationBuilder all = AggregationBuilders.terms("by_gender").field("gender");
AggregationBuilder age = AggregationBuilders.terms("by_age").field("age");
AggregationBuilder sumBuilder=AggregationBuilders.sum("ageSum").field("age");
//AggregationBuilder avgBuilder=AggregationBuilders.avg("ageAvg").field("age");
// AggregationBuilder countBuilder=AggregationBuilders.count("ageCount").field("age");
all.subAggregation(age.subAggregation(sumBuilder));
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(32,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(rangeQueryBuilder).addAggregation(all);
logger.log(Level.INFO,sv.toString());
SearchResponse response= sv.get(); Aggregations terms= response.getAggregations();
for (Aggregation a:terms){
StringTerms stringTerms= (StringTerms)a;
for(StringTerms.Bucket bucket:stringTerms.getBuckets()){
// logger.info(bucket.getKeyAsString());
Aggregation aggs=bucket.getAggregations().getAsMap().get("by_age");
LongTerms terms1= (LongTerms)aggs;
for (LongTerms.Bucket bu:terms1.getBuckets()){
logger.info(bucket.getKeyAsString()+" "+bu.getKeyAsString()+" "+bu.getDocCount()+" "+((Sum)bu.getAggregations().asMap().get("ageSum")).getValue());
} }
}
每增加一个分组指标就需要多加一个termsBuilder 其他等一切跟普通分组一样 每次拿到
以上就是我总结的基本的查询 聚合 等常见功能 其他等诸如 求各组前多少数据是用topHits 这些基本够我们日常操作了 。
最后我们总结下 精确查询用term 组合查询用bool 范围用range and查询用must or查询用should not查询用must not 常见的接收聚合返回结果的类型 ValueCount AVG SUM MAX MIN 按照英文意义就可以理解 分组聚合查询时候还需要根据实际情况看是返回那种terms
elasticsearch 常见查询及聚合的JAVA API的更多相关文章
- Elasticsearch的CRUD:REST与Java API
CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...
- Elasticsearch 2.3.3 JAVA api说明文档
原文地址:https://www.blog-china.cn/template\documentHtml\1484101683485.html 翻译作者:@青山常在人不老 加入翻译:cdcnsuper ...
- Elasticsearch JAVA api搞定groupBy聚合
本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合.为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...
- Elasticsearch JAVA api轻松搞定groupBy聚合
本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合. 为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...
- ElasticSearch AggregationBuilders java api常用聚会查询
以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: "mappings": { "pl ...
- Elasticsearch java api 常用查询方法QueryBuilder构造举例
转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...
- Elasticsearch Java API的基本使用
说明 在明确了ES的基本概念和使用方法后,我们来学习如何使用ES的Java API. 本文假设你已经对ES的基本概念已经有了一个比较全面的认识. 客户端 你可以用Java客户端做很多事情: 执行标准的 ...
- Elasticsearch(8) --- 聚合查询(Metric聚合)
Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...
- ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程
前言 在上一篇中介绍了ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解,本篇文章就来讲解下 ElasticSearch 6.x官方Java API的 ...
随机推荐
- [Javascript] Working with Static Properties on a Class
Classes are syntactic sugar over functions and functions are also referred to as "callable" ...
- win10系统绑定本地IP和mac地址
第一步:找到自己的IP和mac地址 1.按着win键+R键,输入cmd(大小写都一样) 2.命令: ipconfig /all #查看所有地址 然后按“回车键” 3.这样 ...
- ava js 测试框架基本试用
随着js 越来越强大,日常使用中关于js 的问题也就越突出了,我们需要关注的点也就不能只像以前那样 只编写简单的功能实现,我们同时也需要关注js 的健壮性,测试就是其中一个比较重要的环节,以下 是av ...
- 如何在你的springboot(cloud)项目中引入我的github上的jar仓库呢?
1. 将此标签内容放到pom.xml仅次于project标签下 <repositories> <repository> <id>github</id> ...
- Problem 2 旅行计划 (travelling .cpp)———2019.10.6
lth tql,lzpclxf tql Orz Problem 2 旅行计划 (travelling.cpp)[题目描述]小 Z 打算趁着暑假,开启他的旅行计划.但与其他同学不同的是,小 Z 旅行时并 ...
- 【CF55D】Beautiful numbers
[CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...
- cocos Uniforms值的赋值
cocos Uniforms值的赋值 举个void Renderer::drawBatchedTriangles()的渲染: //Start drawing verties in batch for( ...
- 安装ubuntu 16.04版本时搭建环境参考的文章
重新编译的命令:make all ZIP_DEBUGINFO_FILES=0 DISABLE_HOTSPOT_OS_VERSION_CHECK=ok 解决ubuntu中vi不能正常使用方向键与退格键的 ...
- js之select三级联动
效果图如下: 代码逻辑梳理:层层递进,比如选择了课程后,将对应的课程id保存,然后点击选择章时自动触发对应的时间,根据这个课程ID获取其下面的章信息.其它的如节等,同理. 代码说明:如下代码不规范,可 ...
- 经典算法(三) 单链表 反转 & 是否相交/成环 & 求交点 等
参考文章: 判断链表是否相交:http://treemanfm.iteye.com/blog/2044196 一.单链表反转 链表节点 public class Node { private int ...