//设置分页
     searchSourceBuilder.from(1);
     searchSourceBuilder.size(5);
     //排序
     searchSourceBuilder.sort("age", SortOrder.DESC);
     searchSourceBuilder.sort("id", SortOrder.DESC);

     //数据过滤创建 SearchSourceBuilder条件构造
     SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
     //指定需要返回或者排除的字段
     String[] includes = {"id", "name"};
     String[] excludes = {};
     searchSourceBuilder.fetchSource(includes, excludes);

     //精确查找 term
     TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("address.keyword", "三国演义小乔");

     //Range 范围查找
     RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
     rangeQueryBuilder.gte(18);
     rangeQueryBuilder.lt(23);
     //match 匹配关键词分词查找
     MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("address", "王者打野");
     matchQueryBuilder.operator(Operator.OR);

     //MultiMatch 多字段查找
     MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("王者辅助","address", "desc");
     multiMatchQuery.operator(Operator.OR);

     //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"));

     //模糊查询 Fuzzy
     FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "张").fuzziness(Fuzziness.ONE);

     //聚合查询
     AggregationBuilder sumBuilder = AggregationBuilders.sum(termName).field("COLLECT_PEOPLE_MONEY");

     //对salary统计:个数、平均、最高、最低、总和
     StatsAggregationBuilder statsBuilder = AggregationBuilders.stats(termName).field("COLLECT_PEOPLE_MONEY");

     //等值查询
     searchSourceBuilder.query(QueryBuilders.termQuery(groupColumn, "6"));

     searchSourceBuilder.aggregation(statsBuilder);
     searchRequest.source(searchSourceBuilder);

     //cardinate对搜索结果去重统计
     CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality("jobCardinate").field("job.keyword");
     searchSourceBuilder.aggregation(cardinalityAggregationBuilder);

     //限定聚合范围
     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);

     // Salary Range分桶,可以自己定义 key
     RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range("salary_range")
     .field("salary")
     .addUnboundedTo(10000)
     .addRange(10000, 20000)
     .addUnboundedFrom(">20000", 20000);

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

  

 

参考链接:Java 操作RestHighLevelClient查询详解-CSDN博客

posted on 2024-10-30 18:15  茫无所知  阅读(6)  评论(0编辑  收藏  举报