当前位置:   article > 正文

elasticsearch 多重聚合分组并根据聚合值进行排序_elasticsearch aggs 多字段聚合 排序

elasticsearch aggs 多字段聚合 排序
public List<TungDTO> getByEstateInfoId(Integer estateInfoId) {

        List<TungDTO> tungDTOList = new ArrayList<>();

        BoolQueryBuilder builder = new BoolQueryBuilder()
                .must(QueryBuilders.termQuery(EstateRoomConstant.HOUSE_ID, estateInfoId))
                .must(QueryBuilders.termQuery(EstateRoomConstant.IS_DEL, 0));

        TermsAggregationBuilder termsAggregationBuilder =
                AggregationBuilders
                        .terms("tung_terms")
                        .field(EstateRoomConstant.TUNG)
                        .size(1000)
                        .order(BucketOrder.key(true)) //根据Key 进行升序asc
                        .subAggregation(
                                AggregationBuilders
                                        .terms("unit_terms")
                                        .field(EstateRoomConstant.UNIT)
                                        .size(1000)
                                        .order(BucketOrder.key(true))
                                        .subAggregation(
                                                AggregationBuilders
                                                        .terms("floor_terms")
                                                        .field(EstateRoomConstant.FLOOR)
                                                        .size(1000)
                                                        .order(BucketOrder.key(true))
                                                        .subAggregation(
                                                                AggregationBuilders
                                                                        .terms("rooms_terms")
                                                                        .field(EstateRoomConstant.ROOMS)
                                                                        .size(1000)
                                                                        .order(BucketOrder.key(true))
                                                        )
                                        )
                        );


        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(builder)
                .withPageable(PageRequest.of(0, 1))
                .withSourceFilter(new FetchSourceFilter(new String[]{EstateRoomConstant.HOUSE_ID}, null))
                .addAggregation(termsAggregationBuilder)
                .build();

        log.info("查询{}索引的dsl:{}", IndexAndTypeConstant.DSS_ESTATE_ROOM_INDEX, new SearchSourceBuilder().query(searchQuery.getQuery()).aggregation(termsAggregationBuilder).fetchSource(new String[]{EstateRoomConstant.HOUSE_ID}, null).from(0).size(1));

        AggregatedPage<EstateRoomEntity> aggregatedPage = elasticsearchTemplate.queryForPage(searchQuery, EstateRoomEntity.class);

        Terms tung_terms = aggregatedPage.getAggregations().get("tung_terms");

        if (null != tung_terms && null != tung_terms.getBuckets() && !tung_terms.getBuckets().isEmpty()) {
            tung_terms.getBuckets().forEach(tung_bucket -> {
                TungDTO tungDTO = new TungDTO();
                List<UnitDTO> unitDTOList = new ArrayList<>();
                String tung = tung_bucket.getKeyAsString();
                Terms unit_terms = tung_bucket.getAggregations().get("unit_terms");
                if (null != unit_terms && null != unit_terms.getBuckets() && !unit_terms.getBuckets().isEmpty()) {
                    unit_terms.getBuckets().forEach(unit_bucket -> {
                        UnitDTO unitDTO = new UnitDTO();
                        List<FloorDTO> floorDTOList = new ArrayList<>();
                        String unit = unit_bucket.getKeyAsString();
                        Terms floor_terms = unit_bucket.getAggregations().get("floor_terms");
                        if (null != floor_terms && null != floor_terms.getBuckets() && !floor_terms.getBuckets().isEmpty()) {
                            floor_terms.getBuckets().forEach(floor_bucket -> {
                                FloorDTO floorDTO = new FloorDTO();
                                List<RoomsDTO> roomsDTOList = new ArrayList<>();
                                String floor = floor_bucket.getKeyAsString();
                                Terms rooms_terms = floor_bucket.getAggregations().get("rooms_terms");
                                if (null != rooms_terms && null != rooms_terms.getBuckets() && !rooms_terms.getBuckets().isEmpty()) {
                                    rooms_terms.getBuckets().forEach(rooms_bucket -> {
                                        String rooms = rooms_bucket.getKeyAsString();
                                        RoomsDTO roomsDTO = new RoomsDTO();
                                        roomsDTO.setRooms(rooms);
                                        List<String> roomsList = new ArrayList<>();
                                        if (rooms.contains(",")) {
                                            List<String> list = Arrays.asList(rooms.split(","));
                                            list.forEach(room -> {
                                                room = floor + "0" + room;
                                                roomsList.add(room);
                                            });
                                        } else {
                                            roomsList.add(rooms);
                                        }
                                        roomsDTO.setRoomsList(roomsList);
                                        roomsDTOList.add(roomsDTO);
                                    });
                                }
                                floorDTO.setFloor(floor);
                                floorDTO.setRoomsDTOList(roomsDTOList);
                                floorDTOList.add(floorDTO);
                            });
                        }
                        unitDTO.setUnit(unit);
                        unitDTO.setFloorDTOList(floorDTOList);
                        unitDTOList.add(unitDTO);
                    });
                }
                tungDTO.setTung(tung);
                tungDTO.setUnitDTOList(unitDTOList);
                tungDTOList.add(tungDTO);
            });
        }

        return tungDTOList;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

Kibana查询语句

GET dss_estate_room_index/_search
{
  "from": 0,
  "size": 1,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "houseId": {
              "value": 5050,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "_source": {
    "includes": [
      "houseId"
    ],
    "excludes": []
  },
  "aggregations": {
    "tung_terms": {
      "terms": {
        "field": "tung",
        "size": 1000,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": {
          "_key": "asc"
        }
      },
      "aggregations": {
        "unit_terms": {
          "terms": {
            "field": "unit",
            "size": 1000,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": {
              "_key": "asc"
            }
          },
          "aggregations": {
            "floor_terms": {
              "terms": {
                "field": "floor",
                "size": 1000,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": {
                  "_key": "asc"
                }
              },
              "aggregations": {
                "rooms_terms": {
                  "terms": {
                    "field": "rooms",
                    "size": 1000,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": {
                      "_key": "asc"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}```

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/68113
推荐阅读
相关标签
  

闽ICP备14008679号