feat(商城): 搜索模块

完善搜索操作
master
wayn 4 years ago
parent 670e213e64
commit e4ebcb3eb0

@ -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,6 +77,7 @@ public class GoodsController extends BaseController {
}
boolean flag = false;
redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 3, TimeUnit.MINUTES);
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))) {
@ -93,10 +96,15 @@ public class GoodsController extends BaseController {
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);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
redisCache.deleteObject(SysConstants.REDIS_ES_GOODS_INDEX);
}
return R.result(flag);

@ -27,6 +27,9 @@
},
"isOnSale": {
"type": "boolean"
},
"createTime": {
"type": "date"
}
}
}

@ -141,6 +141,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> 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<GoodsMapper, Goods> 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) {

@ -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<JSONObject> list = baseElasticService.search("goods", searchSourceBuilder, JSONObject.class);
List<Integer> goodsIdList = list.stream().map(jsonObject -> (Integer) jsonObject.get("id")).collect(Collectors.toList());
if (goodsIdList.size() == 0) {

Loading…
Cancel
Save