/**
* 构建结果数据
* @param response
* @return
*/
private SearchResult buildSearchResult(SearchParam param,SearchResponse response) {
SearchResult result = new SearchResult();
SearchHits hits = response.getHits();
// SearchHit[] subHits = hits.getHits();
List<SkuEsModel> esModels= new ArrayList<>();
if(hits.getHits() != null && hits.getHits().length > 0){
for(SearchHit hit : hits.getHits()) {
String sourceAsString = hit.getSourceAsString();
SkuEsModel esModel = JSON.parseObject(sourceAsString, SkuEsModel.class);
esModels.add(esModel);
}
// skuEsModels = Arrays.asList(subHits).stream().map(subHit -> {
// String sourceAsString = subHit.getSourceAsString();
// SkuEsModel skuEsModel = JSON.parseObject(sourceAsString, SkuEsModel.class);
// if (!StringUtils.isEmpty(param.getKeyword())) {
// HighlightField skuTitle = subHit.getHighlightFields().get("skuTitle");
// String skuTitleHighLight = skuTitle.getFragments()[0].string();
// skuEsModel.setSkuTitle(skuTitleHighLight);
// }
// return skuEsModel;
// }).collect(Collectors.toList());
}
//1.返回所查询到的所有商品
result.setProducts(esModels);
//2.当前所有商品所涉及到的所有属性信息
List<SearchResult.AttrVo> attrVos = new ArrayList<>();
ParsedNested attr_agg = response.getAggregations().get("attr_agg");
ParsedLongTerms attr_id_agg = attr_agg.getAggregations().get("attr_id_agg");
for(Terms.Bucket bucket : attr_id_agg.getBuckets()) {
SearchResult.AttrVo attrVo = new SearchResult.AttrVo();
// 1、得到属性的id
long attrId = bucket.getKeyAsNumber().longValue();
// 2、得到属性的名字
String attrName = ((ParsedStringTerms) bucket.getAggregations().get("attr_name_agg")).getBuckets().get(0).getKeyAsString();
// 3、得到属性所有的值
List<String> attrValues = ((ParsedStringTerms) bucket.getAggregations().get("attr_value_agg")).getBuckets().stream().map(item -> {
String keyAsString = ((Terms.Bucket) item).getKeyAsString();
return keyAsString;
}).collect(Collectors.toList());
attrVo.setAttrId(attrId);
attrVo.setAttrName(attrName);
attrVo.setAttrValue(attrValues);
attrVos.add(attrVo);
}
result.setAttrs(attrVos);
//3.当前所有商品所涉及到的所有品牌信息
List<SearchResult.BrandVo> brandVos = new ArrayList<>();
ParsedLongTerms brand_agg = response.getAggregations().get("brand_agg");
for(Terms.Bucket bucket : brand_agg.getBuckets()) {
SearchResult.BrandVo brandVo = new SearchResult.BrandVo();
// 1、得到品牌的id
long brandId = bucket.getKeyAsNumber().longValue();
// 2、得到品牌的名
String brandName = ((ParsedStringTerms) bucket.getAggregations().get("brand_name_agg")).getBuckets().get(0).getKeyAsString();
// 3、得到品牌的图片
String brandImg = ((ParsedStringTerms) bucket.getAggregations().get("brand_img_agg")).getBuckets().get(0).getKeyAsString();
brandVo.setBrandId(brandId);
brandVo.setBrandName(brandName);
brandVo.setBrandImg(brandImg);
brandVos.add(brandVo);
}
result.setBrands(brandVos);
//4.当前所有商品所涉及到的所有分类信息
ParsedLongTerms catalog_agg = response.getAggregations().get("catalog_agg");
List<SearchResult.CatalogVo> catalogVos = new ArrayList<>();
List<? extends Terms.Bucket> buckets = catalog_agg.getBuckets();
for(Terms.Bucket bucket : buckets) {
SearchResult.CatalogVo catalogVo = new SearchResult.CatalogVo();
//得到分类id
String keyAsString = bucket.getKeyAsString();
catalogVo.setCatalogId(Long.parseLong(keyAsString));
//得到分类名
ParsedStringTerms catalog_name_agg = bucket.getAggregations().get("catalog_name_agg");
String catalog_name = catalog_name_agg.getBuckets().get(0).getKeyAsString();
catalogVo.setCatalogName(catalog_name);
catalogVos.add(catalogVo);
}
result.setCatalogs(catalogVos);
//=========以上从聚合信息中获取===========
//5.分页信息-页码
result.setPageNum(param.getPageNum());
//5.分页信息-总记录数
long total = hits.getTotalHits().value;
result.setTotal(total);
//5.分页信息-总页码
int totalPage=(int)total%EsConstant.PRODUCT_PAGESIZE == 0?(int)total/EsConstant.PRODUCT_PAGESIZE:(int)(total/EsConstant.PRODUCT_PAGESIZE+1);
result.setTotalPages(totalPage);
return null;
}