elasticsearch 多重聚合分组并根据聚合值进行排序

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;
    }

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"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}```

你可能感兴趣的:(java)