当前位置:   article > 正文

ES分组和查询_es分组查询

es分组查询

分组

  1. public Map<String, AggsResopnse> operateRecordInfo(PersonOperateRecordRequestEntity personOperateRecordRequest) {
  2. SearchRequest searchRequest = new SearchRequest(PERSON_OPERATE_RECORD_INDEX);
  3. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  4. BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  5. if (null != personOperateRecordRequest.getPersonNumber()) {
  6. boolQueryBuilder.must(QueryBuilders.termQuery(PERSON_NUMBER, personOperateRecordRequest.getPersonNumber()));
  7. }
  8. if (null != personOperateRecordRequest.getOperaterNumber()) {
  9. boolQueryBuilder.must(QueryBuilders.termQuery(OPERATER_NUMBER, personOperateRecordRequest.getOperaterNumber()));
  10. }
  11. searchSourceBuilder.query(boolQueryBuilder);
  12. //分组
  13. List<PersonOperateRecordRequestEntity.AggsModel> aggs = personOperateRecordRequest.getAggs();
  14. if (!CollectionUtils.isEmpty(aggs)) {
  15. int aggSize = 15;
  16. //将分组条件放入数组,目的是为了根据不同的分组条件,方便与动态化构建DSL
  17. ValuesSourceAggregationBuilder[] aggregationBuilders = new ValuesSourceAggregationBuilder[aggs.size()];
  18. for (PersonOperateRecordRequestEntity.AggsModel agg : aggs) {
  19. if (StringUtils.isBlank(agg.getDateGroup())) {
  20. if (null != agg.getAggsSize()) {
  21. aggSize = agg.getAggsSize();
  22. }
  23. //ES类型为字符的分组
  24. aggregationBuilders[agg.getOrder()] = AggregationBuilders.terms(agg.getAggFiled()).field(agg.getAggFiled()).size(aggSize);
  25. } else {
  26. //ES类型未日期的分组
  27. aggregationBuilders[agg.getOrder()] = AggregationBuilders.dateHistogram(agg.getAggFiled())
  28. .field(agg.getAggFiled())
  29. //过滤掉0的
  30. .minDocCount(0)
  31. //显示日期格式化
  32. .format(agg.getFormat())
  33. //日期的起止时间与结束时间
  34. .extendedBounds(new LongBounds(agg.getBoundsStart(), agg.getBoundsEnd()))
  35. //日期分组的间隔
  36. .calendarInterval(new DateHistogramInterval(agg.getDateGroup()));
  37. }
  38. }
  39. /**
  40. * 如果有两个及以上的分组条件,则倒数第二个包住倒数第一个,直到第一个包住第二个
  41. */
  42. for (int i = 0, len = aggregationBuilders.length - 1; i < len; len--) {
  43. aggregationBuilders[len - 1].subAggregation(aggregationBuilders[len]);
  44. }
  45. //最后只需要将第一个进行构建即可
  46. searchSourceBuilder.aggregation(aggregationBuilders[0]);
  47. }
  48. try {
  49. //只获取分组,则把size设为0
  50. searchSourceBuilder.size(0);
  51. searchRequest.source(searchSourceBuilder);
  52. SearchResponse searchResult = esHelper.getClient().search(searchRequest, RequestOptions.DEFAULT);
  53. Aggregations aggResults = searchResult.getAggregations();
  54. //最后将结果封装
  55. Map<String, AggsResopnse> aggsResopnseMap = new HashMap<>((int) (aggs.size()/0.75+1));
  56. for (String s : aggResults.getAsMap().keySet()) {
  57. //MultiBucketsAggregation类可以将聚合的字符字段或者日期字段的对象同时封装
  58. MultiBucketsAggregation aggregation = aggResults.get(s);
  59. for (MultiBucketsAggregation.Bucket bucket : aggregation.getBuckets()) {
  60. aggsResopnseMap.put(bucket.getKeyAsString(), recursion(bucket));
  61. }
  62. }
  63. return aggsResopnseMap;
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. return null;
  67. }
  68. }
  69. /**
  70. * 根据桶进行递归,直到桶没有聚合数据为止,桶里面则放着分完组key
  71. * @param bucket
  72. * @return
  73. */
  74. private AggsResopnse recursion(MultiBucketsAggregation.Bucket bucket) {
  75. Map<String, AggsResopnse> aggsResopnseMap = null;
  76. //桶下的聚合
  77. Aggregations aggregations = bucket.getAggregations();
  78. for (String s : aggregations.getAsMap().keySet()) {
  79. MultiBucketsAggregation aggregation = aggregations.get(s);
  80. aggsResopnseMap = new HashMap<>((int) (aggregation.getBuckets().size()/0.75+1));
  81. for (MultiBucketsAggregation.Bucket bucket1 : aggregation.getBuckets()) {
  82. aggsResopnseMap.put(bucket1.getKeyAsString(), recursion(bucket1));
  83. }
  84. }
  85. //封装
  86. AggsResopnse aggsResopnse = new AggsResopnse();
  87. aggsResopnse.setCount(bucket.getDocCount());
  88. aggsResopnse.setAggs(aggsResopnseMap);
  89. return aggsResopnse;
  90. }

查询

  1. public ESPage<PersonOperateRecordResopnseEntity> selectPersonOperateRecord(PersonOperateRecordRequestEntity personOperateRecordRequest) {
  2. List<PersonOperateRecordResopnseEntity> personOperateRecordResopnseEntities = new ArrayList<>();
  3. SearchRequest searchRequest = new SearchRequest(PERSON_OPERATE_RECORD_INDEX);
  4. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  5. //查询条件
  6. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  7. if (null != personOperateRecordRequest.getPersonNumber()) {
  8. boolQueryBuilder.must(QueryBuilders.termQuery(PERSON_NUMBER, personOperateRecordRequest.getPersonNumber()));
  9. }
  10. if (null != personOperateRecordRequest.getContent()) {
  11. boolQueryBuilder.must(QueryBuilders.termQuery(CONTENT, personOperateRecordRequest.getContent()));
  12. }
  13. if (null != personOperateRecordRequest.getOperaterNumber()) {
  14. boolQueryBuilder.must(QueryBuilders.termQuery(OPERATER_NUMBER, personOperateRecordRequest.getOperaterNumber()));
  15. }
  16. if (null != personOperateRecordRequest.getStartTime()) {
  17. boolQueryBuilder.filter(QueryBuilders.rangeQuery(CREATE_TIME).gte(personOperateRecordRequest.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
  18. }
  19. if (null != personOperateRecordRequest.getEndTime()) {
  20. boolQueryBuilder.filter(QueryBuilders.rangeQuery(CREATE_TIME).lte(personOperateRecordRequest.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
  21. }
  22. searchSourceBuilder.query(boolQueryBuilder);
  23. //排序及分页
  24. if (CollectionUtils.isEmpty(personOperateRecordRequest.getSortModels())) {
  25. SortBuilder<org.elasticsearch.search.sort.FieldSortBuilder> sortBuilder = SortBuilders.fieldSort(UID).order(SortOrder.DESC);
  26. searchSourceBuilder.sort(sortBuilder);
  27. } else {
  28. for (RequestModel.SortModel sortModel : personOperateRecordRequest.getSortModels()) {
  29. SortBuilder<org.elasticsearch.search.sort.FieldSortBuilder> sortBuilder = SortBuilders.fieldSort(sortModel.getFieldSort()).order(SortOrder.fromString(sortModel.getOrder()));
  30. searchSourceBuilder.sort(sortBuilder);
  31. }
  32. }
  33. searchSourceBuilder.size(personOperateRecordRequest.getSize());
  34. if (personOperateRecordRequest.getSearchAfter() != null) {
  35. searchSourceBuilder.searchAfter(personOperateRecordRequest.getSearchAfter());
  36. }
  37. try {
  38. searchSourceBuilder.trackTotalHits(true);
  39. searchRequest.source(searchSourceBuilder);
  40. SearchResponse response = esHelper.getClient().search(searchRequest, RequestOptions.DEFAULT);
  41. TotalHits totalHits = response.getHits().getTotalHits();
  42. SearchHit[] hits = response.getHits().getHits();
  43. for (SearchHit searchHit : hits) {
  44. String result = searchHit.getSourceAsString();
  45. PersonOperateRecordResopnseEntity personOperateRecordResopnse = JsonUtils.jsonToPojo(result, PersonOperateRecordResopnseEntity.class);
  46. personOperateRecordResopnse.setSearchAfter(searchHit.getSortValues());
  47. personOperateRecordResopnseEntities.add(personOperateRecordResopnse);
  48. }
  49. ESPage<PersonOperateRecordResopnseEntity> page = new ESPage<>();
  50. page.setList(personOperateRecordResopnseEntities);
  51. page.setTotal(totalHits.value);
  52. return page;
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. return null;
  56. }
  57. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/68173
推荐阅读
相关标签
  

闽ICP备14008679号