@Data
public class SearchResult {
/**
* 查到的所商品信息
*/
private List<SkuEsModel> products;
private Integer pageNum;//当前页面
private Long total;//总记录数
private Integer totalPages;//总页码
private List<CatalogVo> catalogs;//当前查到的结果涉及的所有分类
private List<BrandVo> brands;//当前查到的结果涉及的品牌
private List<AttrVo> attrs;//当前查到的结果涉及的属性
@Data
public static class BrandVo{
private Long brandId;
private String brandName;
private String brandImg;
}
@Data
public static class AttrVo{
private Long attrId;
private String attrName;
private List<String> attrValue;
}
@Data
public static class CatalogVo{
private Long catalogId;
private String catalogName;
}
}
private SearchResult buildSearchResult(SearchResponse response,SearchParam param)
"hits" : { "total" : { "value" : 4, "relation" : "eq" }
//5.分页信息-页码
long total = hits.getTotalHits().value;
result.setPageNum(param.getPageNum());
//5.分页信息-总页码
int totalPages= (int) (total%EsConstant.PRODUCT_PAGESIZE==0?total/EsConstant.PRODUCT_PAGESIZE:total/EsConstant.PRODUCT_PAGESIZE+1);
result.setTotalPages(totalPages);
//5.分页信息-总记录数
result.setTotal(total);
List<SkuEsModel> esModels=new ArrayList<>();
SearchHits hits = response.getHits();
if (hits.getHits()!=null&&hits.getHits().length>0){
for (SearchHit hit : hits.getHits()) {
String sourceAsString = hit.getSourceAsString();
SkuEsModel skuEsModel = JSON.parseObject(sourceAsString, SkuEsModel.class);
if(!StringUtils.isEmpty(param.getKeyword())){
HighlightField skuTitle = hit.getHighlightFields().get("skuTitle");
String string = skuTitle.getFragments()[0].string();
skuEsModel.setSkuTitle(string);
}
esModels.add(skuEsModel);
}
}
result.setProducts(esModels);
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();
//得到属性的id
long attrId = bucket.getKeyAsNumber().longValue();
// 得到属性的名字
String attrName = ((ParsedStringTerms) bucket.getAggregations().get("attr_name_agg")).getBuckets().get(0).getKeyAsString();
//得到属性的所有值
List<String> attrValue = ((ParsedStringTerms) bucket.getAggregations().get("attr_value_agg")).getBuckets().stream().map(item -> {
String keyAsString = item.getKeyAsString();
return keyAsString;
}).collect(Collectors.toList());
attrVo.setAttrId(attrId);
attrVo.setAttrName(attrName);
attrVo.setAttrValue(attrValue);
attrVos.add(attrVo);
}
result.setAttrs(attrVos);
ArrayList<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();
//3.得到品牌的图片
String brand_img = ((ParsedStringTerms) bucket.getAggregations().get("brand_img_agg")).getBuckets().get(0).getKeyAsString();
//2.得到品牌的名
String brand_name_agg = ((ParsedStringTerms) bucket.getAggregations().get("brand_name_agg")).getBuckets().get(0).getKeyAsString();
brandVo.setBrandId(brandId);
brandVo.setBrandImg(brand_img);
brandVo.setBrandName(brand_name_agg);
brandVos.add(brandVo);
}
result.setBrands(brandVos);
Aggregations aggregations = response.getAggregations();
ParsedLongTerms catalog_agg = aggregations.get("catalog_agg");
List<? extends Terms.Bucket> buckets = catalog_agg.getBuckets();
List<SearchResult.CatalogVo> catalogVos=new ArrayList<>();
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);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有