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);
}
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);
}
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("------------------------------------");
}
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("------------------------------------");
}
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);
}
Aggregations