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. Sliding Window Median

    Description Given an array of n integer, and a moving window(size k), move the window at each iterat ...

  2. 1.创建SpringMVC项目

    1.搭建环境 在Configure下点击Settings 找到Maven修改配置目的是使用自己配置的Maven以及阿里云镜像 开始创建项目 写入自己的公司名,项目名点击下一步 添加信息加快maven配 ...

  3. 部署django到服务器

    部署 服务器环境配置 在本地的虚拟环境中,项目根目录下,执行命令收集所有的包 pip freeze > plist.txt 安装并创建虚拟环境,如已创建则跳过此步 sudo apt-get in ...

  4. 【数论】[素数筛,phi]P3601签到题

    题目描述 给出l,r,要求求出\(\sum_{i = l}^r (i - phi[i]) mod 666623333\) \(1\leq l\leq r\leq 10^{12}\),\(r - l \ ...

  5. cogs 943. [東方S3] 铃仙•优昙华院•稻叶

    二次联通门 : cogs 943. [東方S3] 铃仙•优昙华院•稻叶 /* cogs 943. [東方S3] 铃仙·优昙华院·稻叶 概率dp 貌似做麻烦了 邻接矩阵和链式前向星都用上了... dp[ ...

  6. ES6 fetch方法封装

    // 请求路径 let url = 'http://jsonplaceholder.typicode.com/users' // 传输数据参数 const dataName = { name: &qu ...

  7. Net core学习系列(十)——Net Core配置

    一.前言 选项(Options)模式是对配置(Configuration)的功能的延伸.在12章(ASP.NET Core中的配置二)Configuration中有介绍过该功能(绑定到实体类.绑定至对 ...

  8. el-select获取选中的value和label

    selectGetFn(val) { if (this.multipleFlag) { for (let i = 0; i < GUIDArr.length; i++) { let obj =  ...

  9. convmv 解决GBK 迁移到 UTF-8 ,中文 文件名乱码

    yum install convmv 命令: convmv -f GBK -t UTF-8 -r --nosmart --notest <目标目录> -f from -t to --nos ...

  10. Spring Boot使用Html

    1.引入模板thymeleaf <dependency> <groupId>org.springframework.boot</groupId> <artif ...