|
|
|
@ -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) {
|
|
|
|
|