SearchProductService { public SearchProductConditionBO getSearchProductCondition(String keyword, Collection<String> fields) { NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); if (StringUtils.hasText(keyword)) { nativeSearchQueryBuilder.withQuery(QueryBuilders.multiMatchQuery(keyword, "name", "sellPoint", "categoryName")); } else { nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery()); } if (fields.contains("category")) { nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms("cids").field("cid")); } return elasticsearchTemplate.query(nativeSearchQueryBuilder.build(), response -> { SearchProductConditionBO result = new SearchProductConditionBO(); Aggregation categoryIdsAggregation = response.getAggregations().get("cids"); if (categoryIdsAggregation != null) { result.setCids(new ArrayList<>()); for (LongTerms.Bucket bucket : (((LongTerms) categoryIdsAggregation).getBuckets())) { result.getCids().add(bucket.getKeyAsNumber().intValue()); } } return result; }); }  PageResult<SearchProductBO> pageSearchProduct(SearchProductPageQueryBO pageQueryBO); void saveSearchProduct(SearchProductSaveBO saveBO); SearchProductConditionBO getSearchProductCondition(String keyword, Collection<String> fields);  }
@Test public void testGetSearchCondition() { SearchProductConditionBO conditionBO = searchProductService.getSearchProductCondition("商品", Collections.singletonList("category")); System.out.println(conditionBO); }