ES操作RestHighLevelClient手冊

茫无所知發表於2024-10-30
    //設定分頁
     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部落格

相關文章