赞
踩
根据我之前文章对 ES命令的查询使用,测试索引的文档数据前面文章有提到的。下面我们就通过 RestHighLevelClient来进行查询。
参考官方AP文档: Search APIs | Java REST Client [7.17] | Elastic
Elasticsearch高级查询Query DSL: Elasticsearch高级查询Query DSL_Charge8的博客-CSDN博客
一个查询接口的基本步骤如下:
- 1、创建 SearchRequest搜索请求
- 创建 SearchRequest 搜索请求,如果不带参数,表示查询所有索引
- 2、创建 SearchSourceBuilder条件构造
- 创建 SearchSourceBuilder条件构造,构建搜索的条件。
- 添加大部分查询参数到 SearchSourceBuilder,还可以接收 QueryBuilders构建的查询参数。
- 3、将 SearchSourceBuilder 添加到 SearchRequest中
- 4、执行查询
- 5、解析查询结果
其中,第二步和第五步最为关键。
下面直接上代码,语法使用就不解释了。
使用 match_all,默认只会返回 10条数据。
- @Test
- public void testMatchAll() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("db_idx4");//指定要查询的索引
-
- //2.创建 SearchSourceBuilder条件构造。builder模式这里就先不简写了
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
- searchSourceBuilder.query(matchAllQueryBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println(searchResponse);
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println(hits.getTotalHits());
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
- }
- @Test
- public void testMatchAllPage() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("db_idx4");//指定要查询的索引
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
- .query(QueryBuilders.matchAllQuery());
- //设置分页
- searchSourceBuilder.from(1);
- searchSourceBuilder.size(5);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
- }
- @Test
- public void testSortByAge() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("db_idx4");//指定要查询的索引
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
- .query(QueryBuilders.matchAllQuery());
- //年龄倒序
- searchSourceBuilder.sort("age", SortOrder.DESC);
- searchSourceBuilder.sort("id", SortOrder.DESC);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
- }
数据过滤就是 返回我们指定的字段。
- @Test
- public void testSource() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("db_idx4");//指定要查询的索引
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
- .query(QueryBuilders.matchAllQuery());
- //指定需要返回或者排除的字段
- String[] includes = {
- "id", "name"};
- String[] excludes = {
- };
- searchSourceBuilder.fetchSource(includes, excludes);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
- }
match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找。
- @Test
- public void testMatch() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("db_idx4");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //match 查找
- MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("address", "王者打野");
- matchQueryBuilder.operator(Operator.OR);
- searchSourceBuilder.query(matchQueryBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
- }
multi_match 关键字:可以根据字段类型,决定是否使用分词查询,得分最高的在前面。
- @Test
- public void testMultiMatch() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("db_idx4");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //MultiMatch 查找
- MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("王者辅助","address", "desc");
- multiMatchQuery.operator(Operator.OR);
- searchSourceBuilder.query(multiMatchQuery);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> System.out.println("文档原生信息:" + p.getSourceAsString()));
- }
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //Term 查找
- //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", 23);
- TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("address.keyword", "三国演义小乔");
- searchSourceBuilder.query(termQueryBuilder);
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //Range 查找
- RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
- rangeQueryBuilder.gte(18);
- rangeQueryBuilder.lt(23);
- searchSourceBuilder.query(rangeQueryBuilder);
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("product");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //日期Range 查找
- RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("date"); //date字段
- rangeQueryBuilder.lt("now‐2y");
- searchSourceBuilder.query(rangeQueryBuilder);
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //多个id 查找
- IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery();
- idsQueryBuilder.addIds("2", "5", "1111");
- searchSourceBuilder.query(idsQueryBuilder);
- @Test
- public void testHighlight() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("db_idx4");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //Term 查找
- TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("address", "王者");
- searchSourceBuilder.query(termQueryBuilder);
- //自定义高亮 查找
- HighlightBuilder highlightBuilder = new HighlightBuilder();
- highlightBuilder.preTags("<font color='red'>");
- highlightBuilder.postTags("</font>");
- highlightBuilder.field("address");
- highlightBuilder.requireFieldMatch(false); //多字段时,需要设置为false
- highlightBuilder.field("desc");
- searchSourceBuilder.highlighter(highlightBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- hits.forEach(p -> {
-
- System.out.println("文档原生信息:" + p.getSourceAsString());
- System.out.println("高亮信息:" + p.getHighlightFields());
- });
- }
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //Bool查找
- BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
- // and
- //boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gte(20));
- //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "0"));
-
- // or
- boolQueryBuilder.should(QueryBuilders.rangeQuery("age").gte(20));
- boolQueryBuilder.should(QueryBuilders.matchQuery("sex", "0"));
- searchSourceBuilder.query(boolQueryBuilder);
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //Fuzzy 查找
- FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "张").fuzziness(Fuzziness.ONE);
-
- searchSourceBuilder.query(fuzzyQueryBuilder);
ElasticSearch聚合操作: ElasticSearch聚合操作_Charge8的博客-CSDN博客_elasticsearch聚合size
Metric Aggregation 一些数学运算,可以对文档字段进行统计分析。
- @Test
- public void testAgg1() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //查询员工的最低最高和平均工资
- MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("maxSalary").field("salary");
- MinAggregationBuilder minAggregationBuilder = AggregationBuilders.min("minSalary").field("salary");
- AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avgSalary").field("salary");
-
- searchSourceBuilder.aggregation(maxAggregationBuilder);
- searchSourceBuilder.aggregation(minAggregationBuilder);
- searchSourceBuilder.aggregation(avgAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println(searchResponse);
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedMax maxSalary = aggregations.get("maxSalary");
- ParsedMin minSalary = aggregations.get("minSalary");
- ParsedAvg avgSalary = aggregations.get("avgSalary");
- System.out.println("maxSalary:" + maxSalary);
- System.out.println("最低工资" + maxSalary.getValue());
- System.out.println("最高工资" + minSalary.getValue());
- System.out.println("平均工资" + avgSalary.getValue());
-
- }
- @Test
- public void testAgg2() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //对salary进行统计
- StatsAggregationBuilder statsAggregationBuilder = AggregationBuilders.stats("statSalary").field("salary");
-
- searchSourceBuilder.aggregation(statsAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedStats statSalary = aggregations.get("statSalary");
- System.out.println("统计个数" + statSalary.getCount());
- System.out.println("平均工资" + statSalary.getAvg());
- System.out.println("最高工资" + statSalary.getMaxAsString()); //可以转String
- System.out.println("最低工资" + statSalary.getMin());
- System.out.println("工资之和" + statSalary.getSum());
- }
- @Test
- public void testAgg3() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //cardinate对搜索结果去重统计
- CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality("jobCardinate").field("job.keyword");
- searchSourceBuilder.aggregation(cardinalityAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
-
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedCardinality cardinality = aggregations.get("jobCardinate");
- System.out.println("字段:" + cardinality.getName());
- System.out.println("不重复的个数" + cardinality.getValue());
- }
Bucket Aggregation:按照一定的规则,将文档分配到不同的桶中,每一个桶关联一个 key,从而达到分类的目的。类比Mysql中的group by操作。
- @Test
- public void testAgg4() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //获取 job的分类信息
- TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobGroup").field("job.keyword");
- searchSourceBuilder.aggregation(termsAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedTerms jobGroup = aggregations.get("jobGroup");
- System.out.println("字段:" + jobGroup.getName());
- List<? extends Terms.Bucket> buckets = jobGroup.getBuckets();
- for (Terms.Bucket bucket : buckets) {
-
- System.out.println("getKey:" + bucket.getKey());
- System.out.println("getKeyAsString:" + bucket.getKeyAsString());
- System.out.println("getDocCount:" + bucket.getDocCount());
- }
-
- }
- @Test
- public void testAgg5() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("salary").gte(10000);
- searchSourceBuilder.query(rangeQueryBuilder);
-
- //获取 job的分类信息
- TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobGroup")
- .field("job.keyword")
- .size(10)
- .order(BucketOrder.aggregation("_count", true));
- searchSourceBuilder.aggregation(termsAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- //聚合信息
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedTerms jobGroup = aggregations.get("jobGroup");
- System.out.println("字段:" + jobGroup.getName());
- List<? extends Terms.Bucket> buckets = jobGroup.getBuckets();
- for (Terms.Bucket bucket : buckets) {
-
- System.out.println("getKey:" + bucket.getKey());
- System.out.println("getKeyAsString:" + bucket.getKeyAsString());
- System.out.println("getDocCount:" + bucket.getDocCount());
- }
- }
- @Test
- public void testAgg6() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
-
- // Salary Range分桶,可以自己定义 key
- RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range("salary_range")
- .field("salary")
- .addUnboundedTo(10000)
- .addRange(10000, 20000)
- .addUnboundedFrom(">20000", 20000);
- searchSourceBuilder.aggregation(rangeAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- //聚合信息
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedRange salaryRange = aggregations.get("salary_range"); //注意类型
- System.out.println("字段:" + salaryRange.getName());
- List<? extends Range.Bucket> buckets = salaryRange.getBuckets();
- for (Range.Bucket bucket : buckets) {
-
- System.out.println("getKey:" + bucket.getKey());
- System.out.println("getKeyAsString:" + bucket.getKeyAsString());
- System.out.println("getDocCount:" + bucket.getDocCount());
- System.out.println("getFromAsString:" + bucket.getFromAsString());
- System.out.println("getToAsString:" + bucket.getToAsString());
- }
- }
- // Histogram示例:按照工资的间隔分桶
- HistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.histogram("salary_histrogram")
- .field("salary")
- .interval(5000)
- .extendedBounds(0, 100000);
- searchSourceBuilder.aggregation(histogramAggregationBuilder);
top_hits应用场景:当获取分桶后,桶内最匹配的顶部文档列表。
- @Test
- public void testAgg8() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
-
- // top_hits示例:指定size,不同工种中,年纪最大的3个员工的具体信息
- TopHitsAggregationBuilder topHitsAggregationBuilder = AggregationBuilders.topHits("old_employee")
- .size(3)
- .sort("age", SortOrder.DESC);
-
- TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobs")
- .field("job.keyword")
- .subAggregation(topHitsAggregationBuilder); //嵌套聚合
- searchSourceBuilder.aggregation(termsAggregationBuilder);
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- //聚合信息
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedTerms jobs = aggregations.get("jobs"); //注意类型
- System.out.println("字段:" + jobs.getName());
- List<? extends Terms.Bucket> buckets = jobs.getBuckets();
- for (Terms.Bucket bucket : buckets) {
-
- System.out.println("getKey:" + bucket.getKey());
- System.out.println("getKeyAsString:" + bucket.getKeyAsString());
- System.out.println("getDocCount:" + bucket.getDocCount());
- //嵌套的信息
- ParsedTopHits oldEmployee = bucket.getAggregations().get("old_employee");
- SearchHits employeeHits = oldEmployee.getHits();
- System.out.println(" hits.getTotalHits().value:" + hits.getTotalHits().value);
- for (SearchHit employeeHit : employeeHits) {
-
- System.out.println(" employeeHit.getSourceAsString():" + employeeHit.getSourceAsString());
-
- }
- }
- }
Pipeline Aggregation:支持对聚合分析的结果,再次进行聚合分析。
在员工数最多的工种里,找出平均工资最低的工种
- public void testAgg9() throws IOException {
-
- RestHighLevelClient restHighLevelClient = ESUtil.getRestHighLevelClient();
-
- //1.创建 SearchRequest搜索请求,并指定要查询的索引
- SearchRequest searchRequest = new SearchRequest("employees");
-
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
-
- // min_bucket示例:平均工资最低的工种
- AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_salary").field("salary");
-
- TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobs")
- .field("job.keyword")
- .size(10)
- .subAggregation(avgAggregationBuilder); //嵌套聚合
- searchSourceBuilder.aggregation(termsAggregationBuilder);
-
- // 添加 bucket pipeline(min_bucket)
- searchSourceBuilder.aggregation(new MinBucketPipelineAggregationBuilder("min_salary_by_job", "jobs>avg_salary"));
-
- //3.将 SearchSourceBuilder 添加到 SearchRequest中
- searchRequest.source(searchSourceBuilder);
-
- //4.执行查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
-
- //5.解析查询结果
- System.out.println("花费的时长:" + searchResponse.getTook());
- SearchHits hits = searchResponse.getHits();
- System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
- //聚合信息
- Aggregations aggregations = searchResponse.getAggregations();
- System.out.println("aggregations:" + aggregations);
- ParsedTerms jobs = aggregations.get("jobs"); //注意类型
- System.out.println("字段:" + jobs.getName());
- List<? extends Terms.Bucket> buckets = jobs.getBuckets();
- for (Terms.Bucket bucket : buckets) {
-
- System.out.println("getKey:" + bucket.getKey());
- System.out.println("getKeyAsString:" + bucket.getKeyAsString());
- System.out.println("getDocCount:" + bucket.getDocCount());
- //嵌套的信息
- ParsedAvg avgSalary = bucket.getAggregations().get("avg_salary");
- System.out.println(" hits.getTotalHits().value:" + avgSalary.getValue());
- }
- }
- //2.创建 SearchSourceBuilder条件构造。
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
-
- // Stats示例:统计分析:平均工资的统计分析
- AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_salary").field("salary");
-
- TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("jobs")
- .field("job.keyword")
- .size(10)
- .subAggregation(avgAggregationBuilder); //嵌套聚合
- searchSourceBuilder.aggregation(termsAggregationBuilder);
-
- // 添加 bucket pipeline(stats_bucket)
- searchSourceBuilder.aggregation(new StatsBucketPipelineAggregationBuilder("stats_salary_by_job", "jobs>avg_salary"));
到此,Java 操作 RestHighLevelClient查询基本就 ok了。
总结如下:
– 求知若饥,虚心若愚。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。