欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

elasticsearch进行指定字段去重计数,和去重查询(javaapi)

时间:2023-06-18

es7.16.3版本

指定字段去重计数

RestHighLevelClient client = RestHighLevelClientPoolUtil.getClient();SearchRequest request = new SearchRequest(WebServerdocumentSettings.index);// 构建查询器SearchSourceBuilder builder = new SearchSourceBuilder();// 指定字段进行折叠,这个字段相同的多条数据只显示其中一条CollapseBuilder collapseBuilder = new CollapseBuilder("需要去重的字段名");CardinalityAggregationBuilder agg = AggregationBuilders.cardinality("计数自定义名字").field("指定需要计数的字段");// 复合查询器BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();boolQuery.must(QueryBuilders.matchQuery("field","value"); // 组装查询条件builder.collapse(collapseBuilder);builder.sort(WebServerdocumentSettings.Mappings.createTime, SortOrder.DESC);builder.query(boolQuery);builder.aggregation(agg);builder.from(1);builder.size(10);// 获取计数信息ParsedCardinality fileCount = response.getAggregations().get("计数自定义名字");long total = NumberUtil.parseLong(fileCount.getValueAsString());for (SearchHit hit : response.getHits().getHits()) { Map source = hit.getSourceAsMap(); // 转换成实体对象}// 关闭client

获取指定索引总数

@NotNullprivate Long getFieldCounts(String index) throws IOException { RestHighLevelClient client = RestHighLevelClientPoolUtil.getClient(); CountRequest countRequest = new CountRequest(); countRequest.indices(index); CountResponse response = client.count(countRequest, RequestOptions.DEFAULT); RestHighLevelClientPoolUtil.returnClient(client); return response.getCount();}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。