搜索分页
分页分析
页面需要实现分页搜索,所以我们后台每次查询的时候,需要实现分页。用户页面每次会传入当前页和每页查询多少条数据,当然如果不传入每页显示多少条数据,默认查询30条即可。
分页实现
分页使用PageRequest.of( pageNo- 1, pageSize);实现,第1个参数表示第N页,从0开始,第2个参数表示每页显示多少条,实现代码如下:
代码如下:
@Override public Map search(Map<String, String> searchMap) throws Exception { Map<String, Object> resultMap = new HashMap<>(); //有条件才查询Es if (null != searchMap) { //组合条件对象 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //0:关键词 if (!StringUtils.isEmpty(searchMap.get("keywords"))) { boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND)); } //1:条件 品牌 if (!StringUtils.isEmpty(searchMap.get("brand"))) { boolQuery.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand"))); } //2:条件 规格 for (String key : searchMap.keySet()) { if (key.startsWith("spec_")) { String value = searchMap.get(key).replace("%2B", "+"); boolQuery.filter(QueryBuilders.termQuery("specMap." + key.substring(5) + ".keyword",value)); } } //3:条件 价格 if (!StringUtils.isEmpty(searchMap.get("price"))) { String[] p = searchMap.get("price").split("-"); boolQuery.filter(QueryBuilders.rangeQuery("price").gte(p[0])); if (p.length == 2) { boolQuery.filter(QueryBuilders.rangeQuery("price").lte(p[1])); } } //4. 原生搜索实现类 NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilder.withQuery(boolQuery); //6. 品牌聚合(分组)查询 String skuBrand = "skuBrand"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuBrand).field("brandName")); //7. 规格聚合(分组)查询 String skuSpec = "skuSpec"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuSpec).field("spec.keyword")); String pageNum = searchMap.get("pageNum"); if (null == pageNum) { pageNum = "1"; } //9: 分页 nativeSearchQueryBuilder.withPageable(PageRequest.of(Integer.parseInt(pageNum) - 1, Page.pageSize)); //10: 执行查询, 返回结果对象 AggregatedPage<SkuInfo> aggregatedPage = esTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) { List<T> list = new ArrayList<>(); SearchHits hits = searchResponse.getHits(); if (null != hits) { for (SearchHit hit : hits) { SkuInfo skuInfo = JSON.parseObject(hit.getSourceAsString(), SkuInfo.class); list.add((T) skuInfo); } } return new AggregatedPageImpl<T>(list, pageable, hits.getTotalHits(), searchResponse.getAggregations()); } }); //11. 总条数 resultMap.put("total", aggregatedPage.getTotalElements()); //12. 总页数 resultMap.put("totalPages", aggregatedPage.getTotalPages()); //13. 查询结果集合 resultMap.put("rows", aggregatedPage.getContent()); //14. 获取品牌聚合结果 StringTerms brandTerms = (StringTerms) aggregatedPage.getAggregation(skuBrand); List<String> brandList = brandTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("brandList", brandList); //15. 获取规格聚合结果 StringTerms specTerms = (StringTerms) aggregatedPage.getAggregation(skuSpec); List<String> specList = specTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("specList", specList(specList)); //16. 返回当前页 resultMap.put("pageNum", pageNum); return resultMap; } return null; }
测试如下:
搜索排序
排序分析
排序这里总共有根据价格排序、根据评价排序、根据新品排序、根据销量排序,排序要想实现非常简单,只需要告知排序的域以及排序方式即可实现。
价格排序:只需要根据价格高低排序即可,降序价格高->低,升序价格低->高
评价排序:评价分为好评、中评、差评,可以在数据库中设计3个列,用来记录好评、中评、差评的量,每次排序的时候,好评的比例来排序,当然还要有条数限制,评价条数需要超过N条。
新品排序:直接根据商品的发布时间或者更新时间排序。
销量排序:销量排序除了销售数量外,还应该要有时间段限制。
排序实现
这里我们不单独针对某个功能实现排序,我们只需要在后台接收2个参数,分别是排序域名字和排序方式,代码如下:
@Override public Map search(Map<String, String> searchMap) throws Exception { Map<String, Object> resultMap = new HashMap<>(); //有条件才查询Es if (null != searchMap) { //组合条件对象 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //0:关键词 if (!StringUtils.isEmpty(searchMap.get("keywords"))) { boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND)); } //1:条件 品牌 if (!StringUtils.isEmpty(searchMap.get("brand"))) { boolQuery.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand"))); } //2:条件 规格 for (String key : searchMap.keySet()) { if (key.startsWith("spec_")) { String value = searchMap.get(key).replace("%2B", "+"); boolQuery.filter(QueryBuilders.termQuery("specMap." + key.substring(5) + ".keyword",value)); } } //3:条件 价格 if (!StringUtils.isEmpty(searchMap.get("price"))) { String[] p = searchMap.get("price").split("-"); boolQuery.filter(QueryBuilders.rangeQuery("price").gte(p[0])); if (p.length == 2) { boolQuery.filter(QueryBuilders.rangeQuery("price").lte(p[1])); } } //4. 原生搜索实现类 NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilder.withQuery(boolQuery); //6. 品牌聚合(分组)查询 String skuBrand = "skuBrand"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuBrand).field("brandName")); //7. 规格聚合(分组)查询 String skuSpec = "skuSpec"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuSpec).field("spec.keyword")); //8: 排序 if (!StringUtils.isEmpty(searchMap.get("sortField"))) { if ("ASC".equals(searchMap.get("sortRule"))) { nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(searchMap.get("sortField")).order(SortOrder.ASC)); } else { nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(searchMap.get("sortField")).order(SortOrder.DESC)); } } String pageNum = searchMap.get("pageNum"); if (null == pageNum) { pageNum = "1"; } //9: 分页 nativeSearchQueryBuilder.withPageable(PageRequest.of(Integer.parseInt(pageNum) - 1, Page.pageSize)); //10: 执行查询, 返回结果对象 AggregatedPage<SkuInfo> aggregatedPage = esTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) { List<T> list = new ArrayList<>(); SearchHits hits = searchResponse.getHits(); if (null != hits) { for (SearchHit hit : hits) { SkuInfo skuInfo = JSON.parseObject(hit.getSourceAsString(), SkuInfo.class); list.add((T) skuInfo); } } return new AggregatedPageImpl<T>(list, pageable, hits.getTotalHits(), searchResponse.getAggregations()); } }); //11. 总条数 resultMap.put("total", aggregatedPage.getTotalElements()); //12. 总页数 resultMap.put("totalPages", aggregatedPage.getTotalPages()); //13. 查询结果集合 resultMap.put("rows", aggregatedPage.getContent()); //14. 获取品牌聚合结果 StringTerms brandTerms = (StringTerms) aggregatedPage.getAggregation(skuBrand); List<String> brandList = brandTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("brandList", brandList); //15. 获取规格聚合结果 StringTerms specTerms = (StringTerms) aggregatedPage.getAggregation(skuSpec); List<String> specList = specTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("specList", specList(specList)); //16. 返回当前页 resultMap.put("pageNum", pageNum); return resultMap; } return null; }
测试
根据价格降序:
{"keywords":"手机","pageNum":"1","sortRule":"DESC","sortField":"price"}
根据价格升序:
{"keywords":"手机","pageNum":"1","sortRule":"ASC","sortField":"price"}
高亮显示
高亮分析
高亮显示是指根据商品关键字搜索商品的时候,显示的页面对关键字给定了特殊样式,让它显示更加突出,如上图商品搜索中,关键字编程了红色,其实就是给定了红色样式。
高亮搜索实现步骤解析
将之前的搜索换掉,换成高亮搜索,我们需要做3个步骤:
1.指定高亮域,也就是设置哪个域需要高亮显示 设置高亮域的时候,需要指定前缀和后缀,也就是关键词用什么html标签包裹,再给该标签样式 2.高亮搜索实现 3.将非高亮数据替换成高亮数据
第1点,例如在百度中搜索数据的时候,会有2个地方高亮显示,分别是标题和描述,商城搜索的时候,只是商品名称高亮显示了。而高亮显示其实就是添加了样式,例如<span style="color:red;">笔记本</span>
,而其中span开始标签可以称为前缀,span结束标签可以称为后缀。
第2点,高亮搜索使用ElasticsearchTemplate实现。
第3点,高亮搜索后,会搜出非高亮数据和高亮数据,高亮数据会加上第1点中的高亮样式,此时我们需要将非高亮数据换成高亮数据即可。例如非高亮:华为笔记本性能超强悍
高亮数据:华为<span style="color:red;"笔记本</span>性能超强悍
,将非高亮的换成高亮的,到页面就能显示样式了。
高亮代码实现
删掉之前com.changgou.service.impl.SearchServiceImpl的搜索方法搜索代码,用下面高亮搜索代码替换:
代码如下:
@Override public Map search(Map<String, String> searchMap) throws Exception { Map<String, Object> resultMap = new HashMap<>(); //有条件才查询Es if (null != searchMap) { //组合条件对象 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //0:关键词 if (!StringUtils.isEmpty(searchMap.get("keywords"))) { boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND)); } //1:条件 品牌 if (!StringUtils.isEmpty(searchMap.get("brand"))) { boolQuery.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand"))); } //2:条件 规格 for (String key : searchMap.keySet()) { if (key.startsWith("spec_")) { String value = searchMap.get(key).replace("%2B", "+"); boolQuery.filter(QueryBuilders.termQuery("specMap." + key.substring(5) + ".keyword",value)); } } //3:条件 价格 if (!StringUtils.isEmpty(searchMap.get("price"))) { String[] p = searchMap.get("price").split("-"); boolQuery.filter(QueryBuilders.rangeQuery("price").gte(p[0])); if (p.length == 2) { boolQuery.filter(QueryBuilders.rangeQuery("price").lte(p[1])); } } //4. 原生搜索实现类 NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilder.withQuery(boolQuery); //5:高亮 HighlightBuilder.Field field = new HighlightBuilder .Field("name") .preTags("<span style='color:red'>") .postTags("</span>"); nativeSearchQueryBuilder.withHighlightFields(field); //6. 品牌聚合(分组)查询 String skuBrand = "skuBrand"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuBrand).field("brandName")); //7. 规格聚合(分组)查询 String skuSpec = "skuSpec"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuSpec).field("spec.keyword")); //8: 排序 if (!StringUtils.isEmpty(searchMap.get("sortField"))) { if ("ASC".equals(searchMap.get("sortRule"))) { nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(searchMap.get("sortField")).order(SortOrder.ASC)); } else { nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(searchMap.get("sortField")).order(SortOrder.DESC)); } } String pageNum = searchMap.get("pageNum"); if (null == pageNum) { pageNum = "1"; } //9: 分页 nativeSearchQueryBuilder.withPageable(PageRequest.of(Integer.parseInt(pageNum) - 1, Page.pageSize)); //10: 执行查询, 返回结果对象 AggregatedPage<SkuInfo> aggregatedPage = esTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) { List<T> list = new ArrayList<>(); SearchHits hits = searchResponse.getHits(); if (null != hits) { for (SearchHit hit : hits) { SkuInfo skuInfo = JSON.parseObject(hit.getSourceAsString(), SkuInfo.class); Map<String, HighlightField> highlightFields = hit.getHighlightFields(); if (null != highlightFields && highlightFields.size() > 0) { skuInfo.setName(highlightFields.get("name").getFragments()[0].toString()); } list.add((T) skuInfo); } } return new AggregatedPageImpl<T>(list, pageable, hits.getTotalHits(), searchResponse.getAggregations()); } }); //11. 总条数 resultMap.put("total", aggregatedPage.getTotalElements()); //12. 总页数 resultMap.put("totalPages", aggregatedPage.getTotalPages()); //13. 查询结果集合 resultMap.put("rows", aggregatedPage.getContent()); //14. 获取品牌聚合结果 StringTerms brandTerms = (StringTerms) aggregatedPage.getAggregation(skuBrand); List<String> brandList = brandTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("brandList", brandList); //15. 获取规格聚合结果 StringTerms specTerms = (StringTerms) aggregatedPage.getAggregation(skuSpec); List<String> specList = specTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("specList", specList(specList)); //16. 返回当前页 resultMap.put("pageNum", pageNum); return resultMap; } return null; }
测试
效果如下:
"name": "HTC M8Sd (E8) 波尔多红 电信4G<span style=\"color:red\">手机</span> 双卡双待双通",
最终搜索业务代码如下
@Service public class SearchServiceImpl implements SearchService { @Autowired private ElasticsearchTemplate esTemplate; //设置每页查询条数据 public final static Integer PAGE_SIZE = 20; @Override public Map search(Map<String, String> searchMap) throws Exception { Map<String, Object> resultMap = new HashMap<>(); //有条件才查询Es if (null != searchMap) { //组合条件对象 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //0:关键词 if (!StringUtils.isEmpty(searchMap.get("keywords"))) { boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND)); } //1:条件 品牌 if (!StringUtils.isEmpty(searchMap.get("brand"))) { boolQuery.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand"))); } //2:条件 规格 for (String key : searchMap.keySet()) { if (key.startsWith("spec_")) { String value = searchMap.get(key).replace("%2B", "+"); boolQuery.filter(QueryBuilders.termQuery("specMap." + key.substring(5) + ".keyword",value)); } } //3:条件 价格 if (!StringUtils.isEmpty(searchMap.get("price"))) { String[] p = searchMap.get("price").split("-"); boolQuery.filter(QueryBuilders.rangeQuery("price").gte(p[0])); if (p.length == 2) { boolQuery.filter(QueryBuilders.rangeQuery("price").lte(p[1])); } } //4. 原生搜索实现类 NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilder.withQuery(boolQuery); //5:高亮 HighlightBuilder.Field field = new HighlightBuilder .Field("name") .preTags("<span style='color:red'>") .postTags("</span>"); nativeSearchQueryBuilder.withHighlightFields(field); //6. 品牌聚合(分组)查询 String skuBrand = "skuBrand"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuBrand).field("brandName")); //7. 规格聚合(分组)查询 String skuSpec = "skuSpec"; nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuSpec).field("spec.keyword")); //8: 排序 if (!StringUtils.isEmpty(searchMap.get("sortField"))) { if ("ASC".equals(searchMap.get("sortRule"))) { nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(searchMap.get("sortField")).order(SortOrder.ASC)); } else { nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(searchMap.get("sortField")).order(SortOrder.DESC)); } } String pageNum = searchMap.get("pageNum"); if (null == pageNum) { pageNum = "1"; } //9: 分页 nativeSearchQueryBuilder.withPageable(PageRequest.of(Integer.parseInt(pageNum) - 1, Page.pageSize)); //10: 执行查询, 返回结果对象 AggregatedPage<SkuInfo> aggregatedPage = esTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) { List<T> list = new ArrayList<>(); SearchHits hits = searchResponse.getHits(); if (null != hits) { for (SearchHit hit : hits) { SkuInfo skuInfo = JSON.parseObject(hit.getSourceAsString(), SkuInfo.class); Map<String, HighlightField> highlightFields = hit.getHighlightFields(); if (null != highlightFields && highlightFields.size() > 0) { skuInfo.setName(highlightFields.get("name").getFragments()[0].toString()); } list.add((T) skuInfo); } } return new AggregatedPageImpl<T>(list, pageable, hits.getTotalHits(), searchResponse.getAggregations()); } }); //11. 总条数 resultMap.put("total", aggregatedPage.getTotalElements()); //12. 总页数 resultMap.put("totalPages", aggregatedPage.getTotalPages()); //13. 查询结果集合 resultMap.put("rows", aggregatedPage.getContent()); //14. 获取品牌聚合结果 StringTerms brandTerms = (StringTerms) aggregatedPage.getAggregation(skuBrand); List<String> brandList = brandTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("brandList", brandList); //15. 获取规格聚合结果 StringTerms specTerms = (StringTerms) aggregatedPage.getAggregation(skuSpec); List<String> specList = specTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList()); resultMap.put("specList", specList(specList)); //16. 返回当前页 resultMap.put("pageNum", pageNum); return resultMap; } return null; } //处理规格集合 public Map<String, Set<String>> specList(List<String> specList) { Map<String, Set<String>> specMap = new HashMap<>(); if (null != specList && specList.size() > 0) { for (String spec : specList) { Map<String, String> map = JSON.parseObject(spec, Map.class); Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { String key = entry.getKey(); String value = entry.getValue(); Set<String> specValues = specMap.get(key); if (null == specValues) { specValues = new HashSet<>(); } specValues.add(value); specMap.put(key, specValues); } } } return specMap; } }