|
|
package com.yoho.search.service.scene;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
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 com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.cache.SearchCacheFactory;
|
|
|
import com.yoho.search.common.cache.model.SearchCache;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.base.ProductListSortService;
|
|
|
import com.yoho.search.service.base.SearchCacheService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.SearchDynamicConfigService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
|
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
|
|
import com.yoho.search.service.helper.SearchKeyWordHelper;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.helper.SearchSortHelper;
|
|
|
import com.yoho.search.service.service.ISearchRecommendService;
|
|
|
import com.yoho.search.service.service.impl.ProductListServiceImpl;
|
|
|
|
|
|
@Service
|
|
|
public class WebProductListService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductListServiceImpl.class);
|
|
|
|
|
|
// 当少于20个商品时 返回智能搜索词提示
|
|
|
private static final int SMART_SUGGESTION_PRODUCT_LIMIT = 20;
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchSortHelper searchSortHelper;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchKeyWordHelper searchKeyWordService;
|
|
|
@Autowired
|
|
|
private ISearchRecommendService searchRecommendService;
|
|
|
@Autowired
|
|
|
private SearchDynamicConfigService searchDynamicConfigService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private SearchCacheService searchCacheService;
|
|
|
@Autowired
|
|
|
private SearchCacheFactory searchCacheFactory;
|
|
|
@Autowired
|
|
|
private ProductListSortService productListSortService;
|
|
|
|
|
|
private SearchCache productListSearchCache;
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
productListSearchCache = searchCacheFactory.getWebProductListSearchCache();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* pc的商品列表页
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public SearchApiResult webProductList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
SearchApiResult searchResult = innerProductList(paramMap);
|
|
|
if (needTermSuggestion(searchResult, paramMap)) {
|
|
|
// 当商品数量太少或无结果时 支持智能推荐Term搜索
|
|
|
JSONObject dataMap = ((JSONObject) searchResult.getData());
|
|
|
dataMap.put("suggestion", searchRecommendService.recommend(searchResult, paramMap));
|
|
|
}
|
|
|
return searchResult;
|
|
|
} catch (Exception e) {
|
|
|
logger.error("[func=productList][params=" + paramMap + "]", e);
|
|
|
return SearchApiResultUtils.errorSearchApiResult("productList", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchApiResult innerProductList(Map<String, String> paramMap) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[func=productList][param={}][begin={}]", paramMap, begin);
|
|
|
|
|
|
// 1)构造搜索参数
|
|
|
SearchParam searchParam = buildProductListSearchParam(paramMap);
|
|
|
|
|
|
// 2)从缓存中获取数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
return new SearchApiResult().setData(cacheObject);
|
|
|
}
|
|
|
|
|
|
// 3)查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null) {
|
|
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
|
|
}
|
|
|
|
|
|
// 4)记录关键字对应的查询结果
|
|
|
String queryWord = paramMap.get("query");
|
|
|
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
|
|
|
searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());
|
|
|
}
|
|
|
|
|
|
// 5)构造返回结果
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
dataMap.put("total", searchResult.getTotal());
|
|
|
dataMap.put("page", searchResult.getPage());
|
|
|
dataMap.put("page_size", searchParam.getSize());
|
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
|
|
|
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
|
|
|
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序
|
|
|
|
|
|
// 6)将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
|
|
|
logger.info("[func=productList][cost={}]", System.currentTimeMillis() - begin);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
}
|
|
|
|
|
|
private boolean needTermSuggestion(SearchApiResult searchResult, Map<String, String> paramMap) {
|
|
|
if (searchResult == null || searchResult.getCode() != 200 || searchResult.getData() == null) {
|
|
|
return false;
|
|
|
}
|
|
|
// 1. 判断是否需要进行term推荐
|
|
|
// 1.1 query不为空
|
|
|
String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY);
|
|
|
if (StringUtils.isEmpty(queryWord) || (queryWord.length() > 30 && !searchCommonHelper.isQuerySknOrSku(queryWord))) {
|
|
|
return false;
|
|
|
}
|
|
|
// 1.2 请求制定需要返回term推荐
|
|
|
if (!"Y".equalsIgnoreCase(paramMap.get(SearchRequestParams.PARAM_SEARCH_NEED_SUGGESTION))) {
|
|
|
return false;
|
|
|
}
|
|
|
// 1.3 搜索的数量小于20条
|
|
|
JSONObject dataMap = ((JSONObject) searchResult.getData());
|
|
|
if (dataMap.getIntValue("total") >= SMART_SUGGESTION_PRODUCT_LIMIT) {
|
|
|
return false;
|
|
|
}
|
|
|
// 1.4 打开智能推荐全局开关
|
|
|
return searchDynamicConfigService.isSearchSuggestionTipsOpen();
|
|
|
}
|
|
|
|
|
|
private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
|
|
|
// 1)验证查询条数
|
|
|
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
if (page < 1 || pageSize < 0) {
|
|
|
throw new IllegalArgumentException("分页参数不合法");
|
|
|
}
|
|
|
if (pageSize > 500) {
|
|
|
pageSize = 500;
|
|
|
}
|
|
|
// 2)构建基本查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true);
|
|
|
searchParam.setAggregationBuilders(null);
|
|
|
searchParam.setSize(pageSize);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
// 3)设置排序字段
|
|
|
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
|
|
|
// 4)设置返回的结果
|
|
|
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
searchParam.setIncludeFields(includeFields);
|
|
|
return searchParam;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|