diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java index a1070ba..adb162d 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java @@ -12,6 +12,7 @@ import com.wayn.common.core.domain.vo.GoodsSaveRelatedVO; import com.wayn.common.core.service.shop.IGoodsService; import com.wayn.common.util.R; import com.wayn.common.util.file.FileUtils; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -31,6 +32,7 @@ import java.util.concurrent.TimeUnit; * @author wayn * @since 2020-07-06 */ +@Slf4j @RestController @RequestMapping("/shop/goods") public class GoodsController extends BaseController { @@ -75,28 +77,34 @@ public class GoodsController extends BaseController { } boolean flag = false; redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 3, TimeUnit.MINUTES); - baseElasticService.deleteIndex(SysConstants.ES_GOODS_INDEX); - InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME); - if (baseElasticService.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) { - List list = iGoodsService.list(); - List entities = new ArrayList<>(); - for (Goods goods : list) { - ElasticEntity elasticEntity = new ElasticEntity(); - Map map = new HashMap<>(); - elasticEntity.setId(goods.getId().toString()); - map.put("id", goods.getId()); - map.put("name", goods.getName()); - map.put("sales", goods.getActualSales() + goods.getVirtualSales()); - map.put("isHot", goods.getIsHot()); - map.put("isNew", goods.getIsNew()); - map.put("countPrice", goods.getCounterPrice()); - map.put("retailPrice", goods.getRetailPrice()); - map.put("keyword", goods.getKeywords().split(",")); - map.put("isOnSale", goods.getIsOnSale()); - elasticEntity.setData(map); - entities.add(elasticEntity); + try { + baseElasticService.deleteIndex(SysConstants.ES_GOODS_INDEX); + InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME); + if (baseElasticService.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) { + List list = iGoodsService.list(); + List entities = new ArrayList<>(); + for (Goods goods : list) { + ElasticEntity elasticEntity = new ElasticEntity(); + Map map = new HashMap<>(); + elasticEntity.setId(goods.getId().toString()); + map.put("id", goods.getId()); + map.put("name", goods.getName()); + map.put("sales", goods.getActualSales() + goods.getVirtualSales()); + map.put("isHot", goods.getIsHot()); + map.put("isNew", goods.getIsNew()); + map.put("countPrice", goods.getCounterPrice()); + map.put("retailPrice", goods.getRetailPrice()); + map.put("keyword", goods.getKeywords().split(",")); + map.put("isOnSale", goods.getIsOnSale()); + map.put("createTime", goods.getCreateTime()); + elasticEntity.setData(map); + entities.add(elasticEntity); + } + flag = baseElasticService.insertBatch("goods", entities); } - flag = baseElasticService.insertBatch("goods", entities); + } catch (Exception e) { + log.error(e.getMessage(), e); + } finally { redisCache.deleteObject(SysConstants.REDIS_ES_GOODS_INDEX); } return R.result(flag); diff --git a/waynboot-admin-api/src/main/resources/es/index/goods.json b/waynboot-admin-api/src/main/resources/es/index/goods.json index 080d141..1570630 100644 --- a/waynboot-admin-api/src/main/resources/es/index/goods.json +++ b/waynboot-admin-api/src/main/resources/es/index/goods.json @@ -27,6 +27,9 @@ }, "isOnSale": { "type": "boolean" + }, + "createTime": { + "type": "date" } } } diff --git a/waynboot-common/src/main/java/com/wayn/common/core/service/shop/impl/GoodsServiceImpl.java b/waynboot-common/src/main/java/com/wayn/common/core/service/shop/impl/GoodsServiceImpl.java index 2c0b041..548aa7a 100644 --- a/waynboot-common/src/main/java/com/wayn/common/core/service/shop/impl/GoodsServiceImpl.java +++ b/waynboot-common/src/main/java/com/wayn/common/core/service/shop/impl/GoodsServiceImpl.java @@ -141,6 +141,7 @@ public class GoodsServiceImpl extends ServiceImpl implements map.put("retailPrice", goods.getRetailPrice()); map.put("keyword", goods.getKeywords().split(",")); map.put("isOnSale", goods.getIsOnSale()); + map.put("createTime", goods.getCreateTime()); elasticEntity.setData(map); boolean one = baseElasticService.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity); if (!one) { @@ -237,6 +238,7 @@ public class GoodsServiceImpl extends ServiceImpl implements map.put("retailPrice", goods.getRetailPrice()); map.put("keyword", Objects.isNull(goods.getKeywords()) ? Collections.emptyList() : goods.getKeywords().split(",")); map.put("isOnSale", goods.getIsOnSale()); + map.put("createTime", goods.getCreateTime()); elasticEntity.setData(map); boolean one = baseElasticService.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity); if (!one) { diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java index a7a11bb..7b55a09 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java @@ -59,8 +59,10 @@ public class SearchController extends BaseController { Long memberId = SecurityUtils.getUserId(); String keyword = searchVO.getKeyword(); // Integer categoryId = searchVO.getCategoryId(); - Boolean isHot = searchVO.getIsHot(); + Boolean filterNew = searchVO.getFilterNew(); + Boolean filterHot = searchVO.getFilterHot(); Boolean isNew = searchVO.getIsNew(); + Boolean isHot = searchVO.getIsHot(); Boolean isPrice = searchVO.getIsPrice(); Boolean isSales = searchVO.getIsSales(); String orderBy = searchVO.getOrderBy(); @@ -74,15 +76,17 @@ public class SearchController extends BaseController { // 查询包含关键字、已上架商品 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); - MatchQueryBuilder matchQuery1 = QueryBuilders.matchQuery("isOnSale", true); - MatchQueryBuilder matchQuery2 = QueryBuilders.matchQuery("name", keyword); + MatchQueryBuilder matchFiler = QueryBuilders.matchQuery("isOnSale", true); + MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", keyword); MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("keyword", keyword); - boolQueryBuilder.must(matchQuery1).must(matchQuery2).should(matchPhraseQueryBuilder); - // boolQueryBuilder.should(matchQuery1).should(matchPhraseQueryBuilder); - searchSourceBuilder.query(boolQueryBuilder); - searchSourceBuilder.from((int) (page.getCurrent() - 1) * (int) page.getSize()); - searchSourceBuilder.size((int) page.getSize()); + boolQueryBuilder.filter(matchFiler).should(matchQuery).should(matchPhraseQueryBuilder).minimumShouldMatch(1); searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS)); + if (isNew) { + searchSourceBuilder.sort(new FieldSortBuilder("isNew").order(SortOrder.DESC)); + } + if (isHot) { + searchSourceBuilder.sort(new FieldSortBuilder("isHot").order(SortOrder.DESC)); + } // 按价格高低排序 if (isPrice) { searchSourceBuilder.sort(new FieldSortBuilder("retailPrice").order("asc".equals(orderBy) ? SortOrder.ASC : SortOrder.DESC)); @@ -91,16 +95,20 @@ public class SearchController extends BaseController { if (isSales) { searchSourceBuilder.sort(new FieldSortBuilder("sales").order(SortOrder.DESC)); } - // 按热门商品 - if (isHot) { - // searchSourceBuilder.sort(new FieldSortBuilder("isHot").order(SortOrder.DESC)); - MatchQueryBuilder filterQuery1 = QueryBuilders.matchQuery("isHot", true); - searchSourceBuilder.postFilter(filterQuery1); + // 筛选新品 + if (filterNew) { + MatchQueryBuilder filterQuery = QueryBuilders.matchQuery("isNew", true); + boolQueryBuilder.filter(filterQuery); } - // 按新品 - if (isNew) { - searchSourceBuilder.sort(new FieldSortBuilder("isNew").order(SortOrder.DESC)); + // 筛选热品 + if (filterHot) { + MatchQueryBuilder filterQuery = QueryBuilders.matchQuery("isHot", true); + boolQueryBuilder.filter(filterQuery); } + + searchSourceBuilder.query(boolQueryBuilder); + searchSourceBuilder.from((int) (page.getCurrent() - 1) * (int) page.getSize()); + searchSourceBuilder.size((int) page.getSize()); List list = baseElasticService.search("goods", searchSourceBuilder, JSONObject.class); List goodsIdList = list.stream().map(jsonObject -> (Integer) jsonObject.get("id")).collect(Collectors.toList()); if (goodsIdList.size() == 0) {