Authored by hugufei

为小程序提供模糊搜索接口

package com.yoho.search.restapi.sapp;
import com.yoho.search.common.downgrade.persional.PersionalRateLimit;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.FuzzySceneService;
import com.yoho.search.service.scene.sapp.SappFuzzySceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Controller
public class SappProductListController {
@Autowired
private SappFuzzySceneService sappFuzzySceneService;
/**
* 小程序模糊搜索列表
*
* @return
*/
@PersionalRateLimit(isOrderUseable = true)
@RequestMapping(method = RequestMethod.GET, value = "/sapp/fuzzy/productList")
@ResponseBody
public SearchApiResult sappFuzzyProductList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
SearchApiResult searchApiResult = sappFuzzySceneService.productList(paramMap);
return searchApiResult;
}
/**
* 小程序模糊搜索筛选项
*
* @return
*/
@PersionalRateLimit(isOrderUseable = false)
@RequestMapping(method = RequestMethod.GET, value = "/sapp/fuzzy/aggregations")
@ResponseBody
public SearchApiResult sappFuzzyAggregations(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return sappFuzzySceneService.aggregations(paramMap);
}
}
... ...
... ... @@ -35,13 +35,7 @@ public class CouponSceneService extends AbstractSceneService {
@Override
public void addParamsToParamMap(Map<String, String> paramMap) {
super.addDefaultParamsToParamMap(paramMap);
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITED, "N");// 不支持限定
paramMap.put(SearchRequestParams.PARAM_SEARCH_CONTAIN_SECKILL, "N");// 不支持秒杀
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISADVANCE, "N");// 不支持预售
paramMap.put(SearchRequestParams.PARAM_SEARCH_BUNDLETYPE, "0");// 不支持套餐
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY, "N");// 不支持限购
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE, "N");// 不支持定金预售
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITTIMEADVANCE, "N");// 不支持定金促销
super.filterNotNormalToMap(paramMap);
}
@Override
... ...
... ... @@ -135,7 +135,7 @@ public class FuzzySceneService extends AbstractSceneService {
* @param paramMap
* @return
*/
private void addSuggestion(SearchApiResult searchResult, Map<String, String> paramMap) {
public void addSuggestion(SearchApiResult searchResult, Map<String, String> paramMap) {
if (searchResult == null || searchResult.getCode() != 200 || searchResult.getData() == null) {
return;
}
... ...
... ... @@ -17,7 +17,21 @@ public abstract class AbstractSceneService {
protected static final String STANDARD = "standard";
protected Map<String, String> newParamMap(Map<String, String> paramMap) {
return new HashMap<String, String>(paramMap);
return new HashMap<>(paramMap);
}
/**
* 去除非正常商品的显示
* @param paramMap
*/
protected void filterNotNormalToMap(Map<String, String> paramMap) {
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITED, "N");// 不支持限定
paramMap.put(SearchRequestParams.PARAM_SEARCH_CONTAIN_SECKILL, "N");// 不支持秒杀
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISADVANCE, "N");// 不支持预售
paramMap.put(SearchRequestParams.PARAM_SEARCH_BUNDLETYPE, "0");// 不支持套餐
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY, "N");// 不支持限购
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE, "N");// 不支持定金预售
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITTIMEADVANCE, "N");// 不支持定金促销
}
/**
... ... @@ -48,10 +62,6 @@ public abstract class AbstractSceneService {
return page;
}
protected String getuid(Map<String, String> paramMap) {
return MapUtils.getString(paramMap, "uid", "0");
}
public abstract void addParamsToParamMap(Map<String, String> paramMap);
public abstract String pageId();
... ...
package com.yoho.search.service.scene.sapp;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.SearchPageIdDefine;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchDynamicConfigService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.helper.SearchCommonHelper;
import com.yoho.search.service.helper.SearchKeyWordHelper;
import com.yoho.search.service.list.FuzzySceneProductListService;
import com.yoho.search.service.scene.FuzzySceneService;
import com.yoho.search.service.scene.aggregations.SceneAggregationsHelper;
import com.yoho.search.service.scene.common.AbstractSceneService;
import com.yoho.search.service.scene.common.SceneRecommendBrandsService;
import com.yoho.search.service.scene.common.SceneSelectionsService;
import com.yoho.search.service.service.ISearchRecommendService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Service
public class SappFuzzySceneService extends AbstractSceneService {
private static final Logger logger = LoggerFactory.getLogger(SappFuzzySceneService.class);
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private SearchKeyWordHelper searchKeyWordService;
@Autowired
private FuzzySceneProductListService fuzzySceneProductListService;
@Autowired
private FuzzySceneService fuzzySceneService;
@Autowired
private SceneSelectionsService sceneSelectionsService;
@Autowired
private SceneRecommendBrandsService sceneRecommendBrandsService;
@Override
public String pageId() {
return SearchPageIdDefine.PAGE_ID_SEARCH;
}
@Override
public void addParamsToParamMap(Map<String, String> paramMap) {
super.addDefaultParamsToParamMap(paramMap);
super.filterNotNormalToMap(paramMap);
paramMap.put(SearchRequestParams.PARAM_SEARCH_NEED_SUGGESTION, "Y");// 返回建议词
}
/**
* @1、返回商品列表
* @2、数量太多则返回建议词
*/
@Override
public SearchApiResult productList(Map<String, String> paramMap) {
try {
// 1、参数校验
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY))) {
return new SearchApiResult().setCode(400).setMessage("请传query参数");
}
// 2、添加默认参数
this.addParamsToParamMap(paramMap);
// 3、获取列表
SearchApiResult productListResult = fuzzySceneProductListService.productList(paramMap);
// 4、加入建议词
fuzzySceneService.addSuggestion(productListResult, paramMap);
// 5、模糊搜索页记录关键字对应的查询结果
String queryWord = paramMap.get("query");
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
long total = ((JSONObject) productListResult.getData()).getLongValue("total");
searchKeyWordService.recordKeyWordByResultCount(queryWord, total);
}
//6、返回结果
return productListResult;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return SearchApiResultUtils.errorSearchApiResult("SappFuzzySceneService productList Exception", paramMap, e);
}
}
@Override
public SearchApiResult aggregations(Map<String, String> paramMap) {
try {
// 1、参数校验
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY))) {
return new SearchApiResult().setCode(400).setMessage("请传query参数");
}
// 1、添加默认参数
this.addParamsToParamMap(paramMap);
// 2、返回聚合结果
SearchApiResult result = sceneSelectionsService.aggregations(paramMap);
if(result!=null){
sceneRecommendBrandsService.getRecommendBrands(paramMap, result);
}
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("SappFuzzySceneService Aggregations Exception").setCode(500);
}
}
}
... ...