黑马程序员技术交流社区
标题:
啦啦啦,我是卖报的小行家【项目一day12总结】
[打印本页]
作者:
赵永旗
时间:
2019-8-14 15:25
标题:
啦啦啦,我是卖报的小行家【项目一day12总结】
//1.1关键字搜索
if (searchMap.get("keywords")!=null){
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name",searchMap.get("keywords"));
boolQueryBuilder.must(matchQueryBuilder);
}
//1.2商品过滤分类
if(searchMap.get("category")!=null){
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("categoryName",searchMap.get("category"));
boolQueryBuilder.filter(termQueryBuilder);
}
//1.3品牌过滤分类
if (searchMap.get("brand")!=null){
TermQueryBuilder termQueryBuilder =QueryBuilders.termQuery("brandName",searchMap.get("brand"));
boolQueryBuilder.filter(termQueryBuilder);
}
//1.4规格过滤
for (String key:searchMap.keySet()){
if (key.startsWith("spec.")){
TermQueryBuilder termQueryBuilder =QueryBuilders.termQuery(key+".keyword",searchMap.get(key));
boolQueryBuilder.filter(termQueryBuilder);
}
}
//1.5价格过滤
if (searchMap.get("price")!=null){
String[] price = searchMap.get("price").split("-");
if (!price[0].equals("0")){
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price").gte(price[0] + "00");
boolQueryBuilder.filter(rangeQueryBuilder);
}
}
searchSourceBuilder.query(boolQueryBuilder);
//分页
Integer pageNo = Integer.parseInt(searchMap.get("pageNo"));
Integer pageSize = 20;
int fromIndex= (pageNo-1)*pageNo;
searchSourceBuilder.from(fromIndex);
searchSourceBuilder.size(pageSize);
//排序
String sort = searchMap.get("sort");
String sortOrder = searchMap.get("sortOrder");
if (!"".equals(sort)){
searchSourceBuilder.sort(sort, SortOrder.valueOf(sortOrder));
}
//高亮显示
HighlightBuilder highlightBuilder =new HighlightBuilder();
highlightBuilder.field("name").preTags("<font style='color:red'>").postTags("</font>");
searchSourceBuilder.highlighter(highlightBuilder);
searchRequest.source(searchSourceBuilder);
//聚合查询(商品分类)
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("sku_category").field("categoryName");
searchSourceBuilder.aggregation(termsAggregationBuilder);
//2.0封装查询结果
Map resultMap = new HashMap();
SearchResponse searchResponse = null;
try {
searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();//总记录数
SearchHit[] searchHits = hits.getHits();
List<Map<String,Object>> resultList=new ArrayList<Map<String, Object>>();
for (SearchHit searchHit : searchHits) {
Map<String, Object> skuMap = searchHit.getSourceAsMap();
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields(); //获取高亮结果集
HighlightField name = highlightFields.get("name"); //获取高亮字段
if (name!=null){
Text[] fragments = name.fragments(); //获取高亮结果
skuMap.put("name",fragments[0].toString());
}
resultList.add(skuMap);
}
resultMap.put("rows",resultList);
//商品分类
Aggregations aggregations = searchResponse.getAggregations();
Map<String, Aggregation> aggregationMap = aggregations.getAsMap();
Terms terms =(Terms) aggregationMap.get("sku_category");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
List<String> categoryList = new ArrayList<>();
for (Terms.Bucket bucket : buckets) {
categoryList.add(bucket.getKeyAsString());
}
resultMap.put("categoryList",categoryList);
String categoryName =""; //商品分类名称
if (searchMap.get("category")==null){
if (categoryList.size()>0){
categoryName=categoryList.get(0);
}
} else {
categoryName=searchMap.get("category");
}
//2.3品牌列表封装
if (searchMap.get("brand")==null){
List<Map> brandList= brandMapper.findListByCategoryName(categoryName);
resultMap.put("brandList",brandList);
}
//2.4 规格数据
List<Map> specList = specMapper.findListByCategoryName(categoryName);
for (Map spec:specList){
String[] options =( (String)spec.get("options")).split(",");
spec.put("options",options);
}
resultMap.put("specList",specList);
long totalCount =hits.getTotalHits(); //总记录数
long pageCount = (totalCount%pageSize==0 )?totalCount/pageSize:(totalCount/pageSize+1);//总页数
resultMap.put("totalPages",pageCount);
} catch (IOException e) {
}
return resultMap ;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2