赞
踩
1. cardinality计数:
虽然快,但计数不准,原因自行百度
2.自定义脚本计数:
- SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
- sourceBuilder.from(esPageRange.getFrom());
- sourceBuilder.size(esPageRange.getSize());
- sourceBuilder.collapse(new CollapseBuilder(collapseField));
- sourceBuilder.aggregation(this.scriptedMetricAggregation("termsCount", collapseField));
- sourceBuilder.query(queryBuilder);
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices(indexArray);
- searchRequest.source(sourceBuilder);
- //索引不存在时,不报错
- searchRequest.indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
- List<T> resultList = new ArrayList<>();
- long total = 0L;
- SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- if (response.getHits().getHits() != null) {
- for (SearchHit hit : response.getHits().getHits()) {
- resultList.add(transHitFunction.apply(hit));
- }
- }
- if (response.getAggregations() != null){
- Aggregations aggregations = response.getAggregations();
- ParsedScriptedMetric scriptedMetric = aggregations.get("termsCount");
- if (scriptedMetric != null && scriptedMetric.aggregation() != null){
- Integer value = (Integer)scriptedMetric.aggregation();
- total = value.longValue();
- }
- }
3.脚本聚合代码:
- public ScriptedMetricAggregationBuilder scriptedMetricAggregation(String aggregationName, String fieldName){
- String initScript = "state.numas=new HashSet();";
- String mapScript = "def array = doc['"+fieldName+"'];\n" +
- " if(array.length>=1){\n" +
- " for (def id : array) {\n" +
- " state.numas.add(id);\n" +
- " }\n" +
- " }";
- String combineScript = "return state.numas";
- String reduceScript = "def set =new HashSet();\n" +
- " for(e in states){\n" +
- " if(!Objects.isNull(e)){\n" +
- " for(key in e){\n" +
- " set.add(key);\n" +
- " }\n" +
- " }\n" +
- " }\n" +
- " return set.size();";
- ScriptedMetricAggregationBuilder scriptedMetricAggregationBuilder = AggregationBuilders.scriptedMetric(aggregationName);
- scriptedMetricAggregationBuilder.initScript(new Script(ScriptType.INLINE, PAINLESS, initScript, Collections.emptyMap()));
- scriptedMetricAggregationBuilder.mapScript(new Script(ScriptType.INLINE, PAINLESS, mapScript, Collections.emptyMap()));
- scriptedMetricAggregationBuilder.combineScript(new Script(ScriptType.INLINE, PAINLESS, combineScript, Collections.emptyMap()));
- scriptedMetricAggregationBuilder.reduceScript(new Script(ScriptType.INLINE, PAINLESS, reduceScript, Collections.emptyMap()));
- return scriptedMetricAggregationBuilder;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。