当前位置:   article > 正文

elasticsearch collapse查询计数

elasticsearch collapse

1. cardinality计数:

虽然快,但计数不准,原因自行百度

 

2.自定义脚本计数:

  1. SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
  2. sourceBuilder.from(esPageRange.getFrom());
  3. sourceBuilder.size(esPageRange.getSize());
  4. sourceBuilder.collapse(new CollapseBuilder(collapseField));
  5. sourceBuilder.aggregation(this.scriptedMetricAggregation("termsCount", collapseField));
  6. sourceBuilder.query(queryBuilder);
  7. SearchRequest searchRequest = new SearchRequest();
  8. searchRequest.indices(indexArray);
  9. searchRequest.source(sourceBuilder);
  10. //索引不存在时,不报错
  11. searchRequest.indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
  12. List<T> resultList = new ArrayList<>();
  13. long total = 0L;
  14. SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  15. if (response.getHits().getHits() != null) {
  16. for (SearchHit hit : response.getHits().getHits()) {
  17. resultList.add(transHitFunction.apply(hit));
  18. }
  19. }
  20. if (response.getAggregations() != null){
  21. Aggregations aggregations = response.getAggregations();
  22. ParsedScriptedMetric scriptedMetric = aggregations.get("termsCount");
  23. if (scriptedMetric != null && scriptedMetric.aggregation() != null){
  24. Integer value = (Integer)scriptedMetric.aggregation();
  25. total = value.longValue();
  26. }
  27. }

3.脚本聚合代码:

  1. public ScriptedMetricAggregationBuilder scriptedMetricAggregation(String aggregationName, String fieldName){
  2. String initScript = "state.numas=new HashSet();";
  3. String mapScript = "def array = doc['"+fieldName+"'];\n" +
  4. " if(array.length>=1){\n" +
  5. " for (def id : array) {\n" +
  6. " state.numas.add(id);\n" +
  7. " }\n" +
  8. " }";
  9. String combineScript = "return state.numas";
  10. String reduceScript = "def set =new HashSet();\n" +
  11. " for(e in states){\n" +
  12. " if(!Objects.isNull(e)){\n" +
  13. " for(key in e){\n" +
  14. " set.add(key);\n" +
  15. " }\n" +
  16. " }\n" +
  17. " }\n" +
  18. " return set.size();";
  19. ScriptedMetricAggregationBuilder scriptedMetricAggregationBuilder = AggregationBuilders.scriptedMetric(aggregationName);
  20. scriptedMetricAggregationBuilder.initScript(new Script(ScriptType.INLINE, PAINLESS, initScript, Collections.emptyMap()));
  21. scriptedMetricAggregationBuilder.mapScript(new Script(ScriptType.INLINE, PAINLESS, mapScript, Collections.emptyMap()));
  22. scriptedMetricAggregationBuilder.combineScript(new Script(ScriptType.INLINE, PAINLESS, combineScript, Collections.emptyMap()));
  23. scriptedMetricAggregationBuilder.reduceScript(new Script(ScriptType.INLINE, PAINLESS, reduceScript, Collections.emptyMap()));
  24. return scriptedMetricAggregationBuilder;
  25. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/799070
推荐阅读
相关标签
  

闽ICP备14008679号