明确返回的结果对象数据
结果对象
@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);