当前位置:   article > 正文

Java 操作RestHighLevelClient查询详解

resthighlevelclient

根据我之前文章对 ES命令的查询使用,测试索引的文档数据前面文章有提到的。下面我们就通过 RestHighLevelClient来进行查询。

参考官方AP文档: Search APIs | Java REST Client [7.17] | Elastic

一、高级查询Query DSL

Elasticsearch高级查询Query DSL: Elasticsearch高级查询Query DSL_Charge8的博客-CSDN博客

1、查询接口的步骤

一个查询接口的基本步骤如下:

  1. 1、创建 SearchRequest搜索请求
  2. 创建 SearchRequest 搜索请求,如果不带参数,表示查询所有索引
  3. 2、创建 SearchSourceBuilder条件构造
  4. 创建 SearchSourceBuilder条件构造,构建搜索的条件。
  5. 添加大部分查询参数到 SearchSourceBuilder,还可以接收 QueryBuilders构建的查询参数。
  6. 3、将 SearchSourceBuilder 添加到 SearchRequest中
  7. 4、执行查询
  8. 5、解析查询结果

其中,第二步和第五步最为关键。

下面直接上代码,语法使用就不解释了。

2、查询所有match_all

使用 match_all,默认只会返回 10条数据。

2.1 全量查询

  1. @Test
  2. public void testMatchAll() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求
  5. SearchRequest searchRequest = new SearchRequest();
  6. searchRequest.indices("db_idx4");//指定要查询的索引
  7. //2.创建 SearchSourceBuilder条件构造。builder模式这里就先不简写了
  8. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  9. MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
  10. searchSourceBuilder.query(matchAllQueryBuilder);
  11. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  12. searchRequest.source(searchSourceBuilder);
  13. //4.执行查询
  14. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  15. //5.解析查询结果
  16. System.out.println(searchResponse);
  17. System.out.println("花费的时长:" + searchResponse.getTook());
  18. SearchHits hits = searchResponse.getHits();
  19. System.out.println(hits.getTotalHits());
  20. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  21. hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
  22. }

2.2 分页查询

  • size 关键字:指定查询结果中返回指定条数。 默认返回值10条。
  • from 关键字:用来指定起始返回位置,和size关键字连用可实现分页效果
  1. @Test
  2. public void testMatchAllPage() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求
  5. SearchRequest searchRequest = new SearchRequest();
  6. searchRequest.indices("db_idx4");//指定要查询的索引
  7. //2.创建 SearchSourceBuilder条件构造。
  8. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
  9. .query(QueryBuilders.matchAllQuery());
  10. //设置分页
  11. searchSourceBuilder.from(1);
  12. searchSourceBuilder.size(5);
  13. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  14. searchRequest.source(searchSourceBuilder);
  15. //4.执行查询
  16. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  17. //5.解析查询结果
  18. System.out.println("花费的时长:" + searchResponse.getTook());
  19. SearchHits hits = searchResponse.getHits();
  20. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  21. hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
  22. }

3、数据排序

  1. @Test
  2. public void testSortByAge() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求
  5. SearchRequest searchRequest = new SearchRequest();
  6. searchRequest.indices("db_idx4");//指定要查询的索引
  7. //2.创建 SearchSourceBuilder条件构造。
  8. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
  9. .query(QueryBuilders.matchAllQuery());
  10. //年龄倒序
  11. searchSourceBuilder.sort("age", SortOrder.DESC);
  12. searchSourceBuilder.sort("id", SortOrder.DESC);
  13. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  14. searchRequest.source(searchSourceBuilder);
  15. //4.执行查询
  16. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  17. //5.解析查询结果
  18. System.out.println("花费的时长:" + searchResponse.getTook());
  19. SearchHits hits = searchResponse.getHits();
  20. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  21. hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
  22. }

4、数据过滤

数据过滤就是 返回我们指定的字段。

  1. @Test
  2. public void testSource() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求
  5. SearchRequest searchRequest = new SearchRequest();
  6. searchRequest.indices("db_idx4");//指定要查询的索引
  7. //2.创建 SearchSourceBuilder条件构造。
  8. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
  9. .query(QueryBuilders.matchAllQuery());
  10. //指定需要返回或者排除的字段
  11. String[] includes = {
  12. "id", "name"};
  13. String[] excludes = {
  14. };
  15. searchSourceBuilder.fetchSource(includes, excludes);
  16. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  17. searchRequest.source(searchSourceBuilder);
  18. //4.执行查询
  19. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  20. //5.解析查询结果
  21. System.out.println("花费的时长:" + searchResponse.getTook());
  22. SearchHits hits = searchResponse.getHits();
  23. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  24. hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
  25. }

5、match 查找

match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找。

  1. @Test
  2. public void testMatch() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("db_idx4");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //match 查找
  9. MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("address", "王者打野");
  10. matchQueryBuilder.operator(Operator.OR);
  11. searchSourceBuilder.query(matchQueryBuilder);
  12. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  13. searchRequest.source(searchSourceBuilder);
  14. //4.执行查询
  15. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  16. //5.解析查询结果
  17. System.out.println("花费的时长:" + searchResponse.getTook());
  18. SearchHits hits = searchResponse.getHits();
  19. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  20. hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
  21. }

6、多字段查询multi_match

multi_match 关键字:可以根据字段类型,决定是否使用分词查询,得分最高的在前面。

  1. @Test
  2. public void testMultiMatch() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("db_idx4");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //MultiMatch 查找
  9. MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("王者辅助","address", "desc");
  10. multiMatchQuery.operator(Operator.OR);
  11. searchSourceBuilder.query(multiMatchQuery);
  12. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  13. searchRequest.source(searchSourceBuilder);
  14. //4.执行查询
  15. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  16. //5.解析查询结果
  17. System.out.println("花费的时长:" + searchResponse.getTook());
  18. SearchHits hits = searchResponse.getHits();
  19. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  20. hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
  21. }

7、精确查询Term

  1. //2.创建 SearchSourceBuilder条件构造。
  2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  3. //Term 查找
  4. //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", 23);
  5. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("address.keyword", "三国演义小乔");
  6. searchSourceBuilder.query(termQueryBuilder);

8、范围查询range

  1. //2.创建 SearchSourceBuilder条件构造。
  2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  3. //Range 查找
  4. RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
  5. rangeQueryBuilder.gte(18);
  6. rangeQueryBuilder.lt(23);
  7. searchSourceBuilder.query(rangeQueryBuilder);

9、日期查询range

  1. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  2. SearchRequest searchRequest = new SearchRequest("product");
  3. //2.创建 SearchSourceBuilder条件构造。
  4. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  5. //日期Range 查找
  6. RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("date"); //date字段
  7. rangeQueryBuilder.lt("now‐2y");
  8. searchSourceBuilder.query(rangeQueryBuilder);

10、多个id查询

  1. //2.创建 SearchSourceBuilder条件构造。
  2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  3. //多个id 查找
  4. IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery();
  5. idsQueryBuilder.addIds("2", "5", "1111");
  6. searchSourceBuilder.query(idsQueryBuilder);

11、高亮查询

  1. @Test
  2. public void testHighlight() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("db_idx4");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //Term 查找
  9. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("address", "王者");
  10. searchSourceBuilder.query(termQueryBuilder);
  11. //自定义高亮 查找
  12. HighlightBuilder highlightBuilder = new HighlightBuilder();
  13. highlightBuilder.preTags("<font color='red'>");
  14. highlightBuilder.postTags("</font>");
  15. highlightBuilder.field("address");
  16. highlightBuilder.requireFieldMatch(false); //多字段时,需要设置为false
  17. highlightBuilder.field("desc");
  18. searchSourceBuilder.highlighter(highlightBuilder);
  19. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  20. searchRequest.source(searchSourceBuilder);
  21. //4.执行查询
  22. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  23. //5.解析查询结果
  24. System.out.println("花费的时长:" + searchResponse.getTook());
  25. SearchHits hits = searchResponse.getHits();
  26. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  27. hits.forEach(p -> {
  28. System.out.println("文档原生信息:" + p.getSourceAsString());
  29. System.out.println("高亮信息:" + p.getHighlightFields());
  30. });
  31. }

12、布尔查询Bool

  1. //2.创建 SearchSourceBuilder条件构造。
  2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  3. //Bool查找
  4. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  5. // and
  6. //boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gte(20));
  7. //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "0"));
  8. // or
  9. boolQueryBuilder.should(QueryBuilders.rangeQuery("age").gte(20));
  10. boolQueryBuilder.should(QueryBuilders.matchQuery("sex", "0"));
  11. searchSourceBuilder.query(boolQueryBuilder);

13、模糊查询

  1. //2.创建 SearchSourceBuilder条件构造。
  2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  3. //Fuzzy 查找
  4. FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "张").fuzziness(Fuzziness.ONE);
  5. searchSourceBuilder.query(fuzzyQueryBuilder);

二、聚合操作

ElasticSearch聚合操作: ElasticSearch聚合操作_Charge8的博客-CSDN博客_elasticsearch聚合size

1、Metric Aggregation

Metric Aggregation 一些数学运算,可以对文档字段进行统计分析。

1.1 查询员工的最低最高和平均工资

  1. @Test
  2. public void testAgg1() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //查询员工的最低最高和平均工资
  9. MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("maxSalary").field("salary");
  10. MinAggregationBuilder minAggregationBuilder = AggregationBuilders.min("minSalary").field("salary");
  11. AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avgSalary").field("salary");
  12. searchSourceBuilder.aggregation(maxAggregationBuilder);
  13. searchSourceBuilder.aggregation(minAggregationBuilder);
  14. searchSourceBuilder.aggregation(avgAggregationBuilder);
  15. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  16. searchRequest.source(searchSourceBuilder);
  17. //4.执行查询
  18. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  19. //5.解析查询结果
  20. System.out.println(searchResponse);
  21. System.out.println("花费的时长:" + searchResponse.getTook());
  22. Aggregations aggregations = searchResponse.getAggregations();
  23. System.out.println("aggregations:" + aggregations);
  24. ParsedMax maxSalary = aggregations.get("maxSalary");
  25. ParsedMin minSalary = aggregations.get("minSalary");
  26. ParsedAvg avgSalary = aggregations.get("avgSalary");
  27. System.out.println("maxSalary:" + maxSalary);
  28. System.out.println("最低工资" + maxSalary.getValue());
  29. System.out.println("最高工资" + minSalary.getValue());
  30. System.out.println("平均工资" + avgSalary.getValue());
  31. }

1.2 对salary进行统计

  1. @Test
  2. public void testAgg2() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //对salary进行统计
  9. StatsAggregationBuilder statsAggregationBuilder = AggregationBuilders.stats("statSalary").field("salary");
  10. searchSourceBuilder.aggregation(statsAggregationBuilder);
  11. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  12. searchRequest.source(searchSourceBuilder);
  13. //4.执行查询
  14. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  15. //5.解析查询结果
  16. System.out.println("花费的时长:" + searchResponse.getTook());
  17. Aggregations aggregations = searchResponse.getAggregations();
  18. System.out.println("aggregations:" + aggregations);
  19. ParsedStats statSalary = aggregations.get("statSalary");
  20. System.out.println("统计个数" + statSalary.getCount());
  21. System.out.println("平均工资" + statSalary.getAvg());
  22. System.out.println("最高工资" + statSalary.getMaxAsString()); //可以转String
  23. System.out.println("最低工资" + statSalary.getMin());
  24. System.out.println("工资之和" + statSalary.getSum());
  25. }

1.3 cardinate对搜索结果去重统计

  1. @Test
  2. public void testAgg3() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //cardinate对搜索结果去重统计
  9. CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality("jobCardinate").field("job.keyword");
  10. searchSourceBuilder.aggregation(cardinalityAggregationBuilder);
  11. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  12. searchRequest.source(searchSourceBuilder);
  13. //4.执行查询
  14. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  15. //5.解析查询结果
  16. System.out.println("花费的时长:" + searchResponse.getTook());
  17. Aggregations aggregations = searchResponse.getAggregations();
  18. System.out.println("aggregations:" + aggregations);
  19. ParsedCardinality cardinality = aggregations.get("jobCardinate");
  20. System.out.println("字段:" + cardinality.getName());
  21. System.out.println("不重复的个数" + cardinality.getValue());
  22. }

2、Bucket Aggregation

Bucket Aggregation:按照一定的规则,将文档分配到不同的桶中,每一个桶关联一个 key,从而达到分类的目的。类比Mysql中的group by操作。

2.1 获取 job的分类信息

  1. @Test
  2. public void testAgg4() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. //获取 job的分类信息
  9. TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobGroup").field("job.keyword");
  10. searchSourceBuilder.aggregation(termsAggregationBuilder);
  11. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  12. searchRequest.source(searchSourceBuilder);
  13. //4.执行查询
  14. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  15. //5.解析查询结果
  16. System.out.println("花费的时长:" + searchResponse.getTook());
  17. Aggregations aggregations = searchResponse.getAggregations();
  18. System.out.println("aggregations:" + aggregations);
  19. ParsedTerms jobGroup = aggregations.get("jobGroup");
  20. System.out.println("字段:" + jobGroup.getName());
  21. List<? extends Terms.Bucket> buckets = jobGroup.getBuckets();
  22. for (Terms.Bucket bucket : buckets) {
  23. System.out.println("getKey:" + bucket.getKey());
  24. System.out.println("getKeyAsString:" + bucket.getKeyAsString());
  25. System.out.println("getDocCount:" + bucket.getDocCount());
  26. }
  27. }

2.2 限定聚合范围

  1. @Test
  2. public void testAgg5() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("salary").gte(10000);
  9. searchSourceBuilder.query(rangeQueryBuilder);
  10. //获取 job的分类信息
  11. TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobGroup")
  12. .field("job.keyword")
  13. .size(10)
  14. .order(BucketOrder.aggregation("_count", true));
  15. searchSourceBuilder.aggregation(termsAggregationBuilder);
  16. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  17. searchRequest.source(searchSourceBuilder);
  18. //4.执行查询
  19. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  20. //5.解析查询结果
  21. System.out.println("花费的时长:" + searchResponse.getTook());
  22. SearchHits hits = searchResponse.getHits();
  23. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  24. //聚合信息
  25. Aggregations aggregations = searchResponse.getAggregations();
  26. System.out.println("aggregations:" + aggregations);
  27. ParsedTerms jobGroup = aggregations.get("jobGroup");
  28. System.out.println("字段:" + jobGroup.getName());
  29. List<? extends Terms.Bucket> buckets = jobGroup.getBuckets();
  30. for (Terms.Bucket bucket : buckets) {
  31. System.out.println("getKey:" + bucket.getKey());
  32. System.out.println("getKeyAsString:" + bucket.getKeyAsString());
  33. System.out.println("getDocCount:" + bucket.getDocCount());
  34. }
  35. }

2.3 Range 示例:按照工资的 Range 分桶

  1. @Test
  2. public void testAgg6() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. // Salary Range分桶,可以自己定义 key
  9. RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range("salary_range")
  10. .field("salary")
  11. .addUnboundedTo(10000)
  12. .addRange(10000, 20000)
  13. .addUnboundedFrom(">20000", 20000);
  14. searchSourceBuilder.aggregation(rangeAggregationBuilder);
  15. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  16. searchRequest.source(searchSourceBuilder);
  17. //4.执行查询
  18. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  19. //5.解析查询结果
  20. System.out.println("花费的时长:" + searchResponse.getTook());
  21. SearchHits hits = searchResponse.getHits();
  22. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  23. //聚合信息
  24. Aggregations aggregations = searchResponse.getAggregations();
  25. System.out.println("aggregations:" + aggregations);
  26. ParsedRange salaryRange = aggregations.get("salary_range"); //注意类型
  27. System.out.println("字段:" + salaryRange.getName());
  28. List<? extends Range.Bucket> buckets = salaryRange.getBuckets();
  29. for (Range.Bucket bucket : buckets) {
  30. System.out.println("getKey:" + bucket.getKey());
  31. System.out.println("getKeyAsString:" + bucket.getKeyAsString());
  32. System.out.println("getDocCount:" + bucket.getDocCount());
  33. System.out.println("getFromAsString:" + bucket.getFromAsString());
  34. System.out.println("getToAsString:" + bucket.getToAsString());
  35. }
  36. }

2.4 Histogram示例:按照工资的间隔分桶

  1. // Histogram示例:按照工资的间隔分桶
  2. HistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.histogram("salary_histrogram")
  3. .field("salary")
  4. .interval(5000)
  5. .extendedBounds(0, 100000);
  6. searchSourceBuilder.aggregation(histogramAggregationBuilder);

.5 date_histogram 根据年月日做统计和上面类似,可自定义时间格式

2.6 top_hits示例:

top_hits应用场景:当获取分桶后,桶内最匹配的顶部文档列表。

  1. @Test
  2. public void testAgg8() throws IOException {
  3. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  4. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  5. SearchRequest searchRequest = new SearchRequest("employees");
  6. //2.创建 SearchSourceBuilder条件构造。
  7. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  8. // top_hits示例:指定size,不同工种中,年纪最大的3个员工的具体信息
  9. TopHitsAggregationBuilder topHitsAggregationBuilder = AggregationBuilders.topHits("old_employee")
  10. .size(3)
  11. .sort("age", SortOrder.DESC);
  12. TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobs")
  13. .field("job.keyword")
  14. .subAggregation(topHitsAggregationBuilder); //嵌套聚合
  15. searchSourceBuilder.aggregation(termsAggregationBuilder);
  16. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  17. searchRequest.source(searchSourceBuilder);
  18. //4.执行查询
  19. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  20. //5.解析查询结果
  21. System.out.println("花费的时长:" + searchResponse.getTook());
  22. SearchHits hits = searchResponse.getHits();
  23. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  24. //聚合信息
  25. Aggregations aggregations = searchResponse.getAggregations();
  26. System.out.println("aggregations:" + aggregations);
  27. ParsedTerms jobs = aggregations.get("jobs"); //注意类型
  28. System.out.println("字段:" + jobs.getName());
  29. List<? extends Terms.Bucket> buckets = jobs.getBuckets();
  30. for (Terms.Bucket bucket : buckets) {
  31. System.out.println("getKey:" + bucket.getKey());
  32. System.out.println("getKeyAsString:" + bucket.getKeyAsString());
  33. System.out.println("getDocCount:" + bucket.getDocCount());
  34. //嵌套的信息
  35. ParsedTopHits oldEmployee = bucket.getAggregations().get("old_employee");
  36. SearchHits employeeHits = oldEmployee.getHits();
  37. System.out.println(" hits.getTotalHits().value:" + hits.getTotalHits().value);
  38. for (SearchHit employeeHit : employeeHits) {
  39. System.out.println(" employeeHit.getSourceAsString():" + employeeHit.getSourceAsString());
  40. }
  41. }
  42. }

3、Pipeline Aggregation

Pipeline Aggregation:支持对聚合分析的结果,再次进行聚合分析。

3.1 min_bucket示例:最小值

在员工数最多的工种里,找出平均工资最低的工种

  1. public void testAgg9() throws IOException {
  2. RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
  3. //1.创建 SearchRequest搜索请求,并指定要查询的索引
  4. SearchRequest searchRequest = new SearchRequest("employees");
  5. //2.创建 SearchSourceBuilder条件构造。
  6. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  7. // min_bucket示例:平均工资最低的工种
  8. AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_salary").field("salary");
  9. TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobs")
  10. .field("job.keyword")
  11. .size(10)
  12. .subAggregation(avgAggregationBuilder); //嵌套聚合
  13. searchSourceBuilder.aggregation(termsAggregationBuilder);
  14. // 添加 bucket pipeline(min_bucket)
  15. searchSourceBuilder.aggregation(new MinBucketPipelineAggregationBuilder("min_salary_by_job", "jobs>avg_salary"));
  16. //3.将 SearchSourceBuilder 添加到 SearchRequest中
  17. searchRequest.source(searchSourceBuilder);
  18. //4.执行查询
  19. SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  20. //5.解析查询结果
  21. System.out.println("花费的时长:" + searchResponse.getTook());
  22. SearchHits hits = searchResponse.getHits();
  23. System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
  24. //聚合信息
  25. Aggregations aggregations = searchResponse.getAggregations();
  26. System.out.println("aggregations:" + aggregations);
  27. ParsedTerms jobs = aggregations.get("jobs"); //注意类型
  28. System.out.println("字段:" + jobs.getName());
  29. List<? extends Terms.Bucket> buckets = jobs.getBuckets();
  30. for (Terms.Bucket bucket : buckets) {
  31. System.out.println("getKey:" + bucket.getKey());
  32. System.out.println("getKeyAsString:" + bucket.getKeyAsString());
  33. System.out.println("getDocCount:" + bucket.getDocCount());
  34. //嵌套的信息
  35. ParsedAvg avgSalary = bucket.getAggregations().get("avg_salary");
  36. System.out.println(" hits.getTotalHits().value:" + avgSalary.getValue());
  37. }
  38. }

3.2 Stats示例:统计分析

  1. //2.创建 SearchSourceBuilder条件构造。
  2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  3. // Stats示例:统计分析:平均工资的统计分析
  4. AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_salary").field("salary");
  5. TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobs")
  6. .field("job.keyword")
  7. .size(10)
  8. .subAggregation(avgAggregationBuilder); //嵌套聚合
  9. searchSourceBuilder.aggregation(termsAggregationBuilder);
  10. // 添加 bucket pipeline(stats_bucket)
  11. searchSourceBuilder.aggregation(new StatsBucketPipelineAggregationBuilder("stats_salary_by_job", "jobs>avg_salary"));

到此,Java 操作 RestHighLevelClient查询基本就 ok了。

总结如下:

  • 写好 ES语句很关键,然后通过 客户端编写出来就简单了。
  • 创建 SearchSourceBuilder条件构造,构建搜索的条件很重要,根据业务构造r条件变化多端。
  • 不管是 Query DSL还是 聚合操作,通过 ES语句的结果来编写 SearchResponse的解析查询结果就很容易了,注意里边的类型。

– 求知若饥,虚心若愚。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/68676
推荐阅读
相关标签
  

闽ICP备14008679号