Search in sources :

Example 1 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest method havingSearch.

/**
 * @Author pancm
 * @Description having
 * @Date  2020/8/21
 * @Param []
 * @return void
 */
private static void havingSearch() throws IOException {
    String index = "";
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.indices(index);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
    String alias_name = "nas_ip_address_group";
    String group_name = "nas_ip_address";
    String query_name = "acct_start_time";
    String query_type = "gte,lte";
    String query_name_value = "2020-08-05 13:25:55,2020-08-20 13:26:55";
    String[] query_types = query_type.split(",");
    String[] query_name_values = query_name_value.split(",");
    for (int i = 0; i < query_types.length; i++) {
        if ("gte".equals(query_types[i])) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(query_name).gte(query_name_values[i]));
        }
        if ("lte".equals(query_types[i])) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(query_name).lte(query_name_values[i]));
        }
    }
    AggregationBuilder aggregationBuilder = AggregationBuilders.terms(alias_name).field(group_name).size(Integer.MAX_VALUE);
    // 声明BucketPath,用于后面的bucket筛选
    Map<String, String> bucketsPathsMap = new HashMap<>(8);
    bucketsPathsMap.put("groupCount", "_count");
    // 设置脚本
    Script script = new Script("params.groupCount >= 1000");
    // 构建bucket选择器
    BucketSelectorPipelineAggregationBuilder bs = PipelineAggregatorBuilders.bucketSelector("having", bucketsPathsMap, script);
    aggregationBuilder.subAggregation(bs);
    sourceBuilder.aggregation(aggregationBuilder);
    // 不需要解释
    sourceBuilder.explain(false);
    // 不需要原始数据
    sourceBuilder.fetchSource(false);
    // 不需要版本号
    sourceBuilder.version(false);
    sourceBuilder.query(boolQueryBuilder);
    searchRequest.source(sourceBuilder);
    System.out.println(sourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 查询条数
    long count = searchResponse.getHits().getHits().length;
    Aggregations aggregations = searchResponse.getAggregations();
    // agg(aggregations);
    Map<String, Object> map = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    agg(list, aggregations);
    // System.out.println(map);
    System.out.println(list);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Script(org.elasticsearch.script.Script) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) CardinalityAggregationBuilder(org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest method groupbySearch.

/**
 * @Author pancm
 * @Description 多个聚合条件测试
 * SQL: select age, name, count(*) as count1 from student group by age, name;
 * @Date  2019/7/3
 * @Param []
 * @return void
 */
private static void groupbySearch() throws IOException {
    String buk = "group";
    AggregationBuilder aggregation = AggregationBuilders.terms("age").field("age");
    AggregationBuilder aggregation2 = AggregationBuilders.terms("name").field("name");
    // 根据创建时间按天分组
    // AggregationBuilder aggregation3 = AggregationBuilders.dateHistogram("createtm")
    // .field("createtm")
    // .format("yyyy-MM-dd")
    // .dateHistogramInterval(DateHistogramInterval.DAY);
    // 
    // aggregation2.subAggregation(aggregation3);
    aggregation.subAggregation(aggregation2);
    agg(aggregation, buk);
}
Also used : AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) CardinalityAggregationBuilder(org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)

Example 3 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest2 method test.

private static void test() throws IOException {
    // TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("ecid").field("ecid").size(99999);
    // List<FieldSortBuilder> fieldSorts=new ArrayList<>();
    // fieldSorts.add(new FieldSortBuilder("sum_field").order(SortOrder.DESC));
    // termsBuilder.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", fieldSorts).from(6000).size(10));
    // 
    AggregationBuilder aggregation = AggregationBuilders.terms("ecid").field("ecid");
    List<FieldSortBuilder> fieldSorts = new ArrayList<>();
    fieldSorts.add(new FieldSortBuilder("id_").order(SortOrder.DESC));
    aggregation.subAggregation(new BucketSortPipelineAggregationBuilder("id", fieldSorts).from(0).size(10));
    SearchResponse searchResponse = search(aggregation);
    // 获取聚合结果
    Aggregations aggregations = searchResponse.getAggregations();
    Map<String, Object> map = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    agg(map, list, aggregations);
    logger.info("聚合查询结果:" + list);
    logger.info("------------------------------------");
}
Also used : BucketSortPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketsort.BucketSortPipelineAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) BucketSortPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketsort.BucketSortPipelineAggregationBuilder) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest2 method test2.

private static void test2() throws IOException {
    AggregationBuilder aggregation = AggregationBuilders.filters("ecid", QueryBuilders.termQuery("id", 1));
    SearchResponse searchResponse = search(aggregation);
    // 获取聚合结果
    Aggregations aggregations = searchResponse.getAggregations();
    Map<String, Object> map = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    agg(map, list, aggregations);
    logger.info("test2聚合查询结果:" + list);
    logger.info("------------------------------------");
}
Also used : AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) BucketSortPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketsort.BucketSortPipelineAggregationBuilder) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest method avgSearch.

/**
 * @Author pancm
 * @Description 平均聚合查询测试用例
 * @Date  2019/4/1
 * @Param []
 * @return void
 */
private static void avgSearch() throws IOException {
    String buk = "t_grade_avg";
    // 直接求平均数
    AggregationBuilder aggregation = AggregationBuilders.avg(buk).field("grade");
    logger.info("求班级的平均分数:");
    agg(aggregation, buk);
}
Also used : AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) CardinalityAggregationBuilder(org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)

Aggregations

AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)39 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)28 Nonnull (javax.annotation.Nonnull)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)10 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)10 SumAggregationBuilder (org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder)8 CardinalityAggregationBuilder (org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder)8 ArrayList (java.util.ArrayList)7 RangeAggregationBuilder (org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder)7 DateRangeAggregationBuilder (org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder)7 Map (java.util.Map)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 GeoGridAggregationBuilder (org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder)6 DateHistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)6 HistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder)6 PercentilesAggregationBuilder (org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder)6 ExtendedStatsAggregationBuilder (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder)6 BucketSelectorPipelineAggregationBuilder (org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)6 HashMap (java.util.HashMap)5