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的更多相关文章

  1. Elasticsearch的CRUD:REST与Java API

    CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...

  2. Elasticsearch 2.3.3 JAVA api说明文档

    原文地址:https://www.blog-china.cn/template\documentHtml\1484101683485.html 翻译作者:@青山常在人不老 加入翻译:cdcnsuper ...

  3. Elasticsearch JAVA api搞定groupBy聚合

    本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合.为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...

  4. Elasticsearch JAVA api轻松搞定groupBy聚合

    本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合. 为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...

  5. ElasticSearch AggregationBuilders java api常用聚会查询

    以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: "mappings": { "pl ...

  6. Elasticsearch java api 常用查询方法QueryBuilder构造举例

    转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...

  7. Elasticsearch Java API的基本使用

    说明 在明确了ES的基本概念和使用方法后,我们来学习如何使用ES的Java API. 本文假设你已经对ES的基本概念已经有了一个比较全面的认识. 客户端 你可以用Java客户端做很多事情: 执行标准的 ...

  8. Elasticsearch(8) --- 聚合查询(Metric聚合)

    Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...

  9. ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程

    前言 在上一篇中介绍了ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解,本篇文章就来讲解下 ElasticSearch 6.x官方Java API的 ...

随机推荐

  1. [GXOI/GZOI2019]旅行者 (最短路)

    题意 给定一个有向图,其中一些顶点为关键点.求这些关键点两两之间最小距离. 题解 考试时没怎么想写了50分暴力走了.以为是什么强连通分量的解法,结果就是个最短路.直接从关键点跑一次最短路dis[0], ...

  2. (知识点3)附加到数字的“.f”的目的是什么?

    默认情况下3.2被视为double; 所以为了强制编译器将其视为float,你需要f在最后编写. float a = 3.2; if ( a == 3.2 ) cout << " ...

  3. 牛股资讯-PT

    智能盯盘:实时监控股票涨跌极速行情:实时推送全球行情海量资讯:实时发布海量信息 股票平台,炒股软件,东方财富网,同花顺,大智慧,益盟操盘手,a股,沪深股市,创业板,交易策略,选股,大盘,牛股,牛市,财 ...

  4. 【转】Fiddler常见用法

    转载自:https://blog.csdn.net/china_jeffery/article/details/82426586 一.会话过滤器 选中[Filters]–> [Use Filte ...

  5. 如何在你的springboot(cloud)项目中引入我的github上的jar仓库呢?

    1. 将此标签内容放到pom.xml仅次于project标签下 <repositories> <repository> <id>github</id> ...

  6. BZOJ 4555:[TJOI2016&HEOI2016]求和(第二类斯特林数+NTT)

    题目链接 \(Description\) 求 \[\sum_{i=0}^n\sum_{j=0}^iS(i,j)2^jj!\]对998244353取模后的结果. \(n<=10^5\) \(Sol ...

  7. In Vitro model验证 | Harnessing single-cell genomics to improve the physiological fidelity of organoid-derived cell types

    Transcriptional benchmarking of in vitro cells to in vivo with single-cell rna-seq - 简介 Harnessing s ...

  8. vue form表单上传文件

    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...

  9. sigmoid与softmax 二分类、多分类的使用

    二分类下,sigmoid.softmax两者的数学公式是等价的,理论上应该是一样的,但实际使用的时候还是sigmoid好 https://www.zhihu.com/question/29524708 ...

  10. Linux下SQL Server安装及数据库迁移

    有客户使用Linux服务器,SQL Server从2017开始已支持Linux,从未用过,今天测试了一下,比较简单,记录一下. Linux:CentOS 7.0 首先更新源: curl -o /etc ...