|
|
package com.yoho.search.service.scene.web;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
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.cache.model.SearchCache;
|
|
|
import com.yoho.search.cache.CacheTimeConstants;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheComponent;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchDynamicConfigService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
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.helper.ProductListSortHelper;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchDynamicConfigService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.service.helper.*;
|
|
|
import com.yoho.search.service.index.WebProductIndexBaseService;
|
|
|
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.cache.beans.AbstractCacheAbleService;
|
|
|
import com.yoho.search.service.scene.suggest.RecommendWordsService;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
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.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class WebProductListService extends AbstractCacheAbleService {
|
|
|
public class WebProductListService extends AbstractCacheComponent<JSONObject> {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(WebProductListService.class);
|
|
|
|
...
|
...
|
@@ -54,11 +51,6 @@ public class WebProductListService extends AbstractCacheAbleService { |
|
|
@Autowired
|
|
|
private ProductListSortHelper productListSortHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getWebProductListSearchCache();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* pc的商品列表页
|
|
|
*
|
...
|
...
|
@@ -67,9 +59,28 @@ public class WebProductListService extends AbstractCacheAbleService { |
|
|
*/
|
|
|
public SearchApiResult webProductList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
SearchApiResult searchResult = innerProductList(paramMap);
|
|
|
//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 || page * pageSize > 1000000) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
|
|
|
}
|
|
|
if (pageSize > 100) {
|
|
|
pageSize = 100;
|
|
|
}
|
|
|
|
|
|
//2、查询列表结果
|
|
|
SearchApiResult searchResult = innerProductList(paramMap,page,pageSize);
|
|
|
long tatal = MapUtils.getIntValue((JSONObject)searchResult.getData(),"total",0);
|
|
|
|
|
|
//3、记录关键字对应的查询结果
|
|
|
String queryWord = paramMap.get("query");
|
|
|
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
|
|
|
searchKeyWordService.recordKeyWordByResultCount(queryWord,tatal);
|
|
|
}
|
|
|
|
|
|
//4、当商品数量太少或无结果时 支持智能推荐Term搜索
|
|
|
if (needTermSuggestion(searchResult, paramMap)) {
|
|
|
// 当商品数量太少或无结果时 支持智能推荐Term搜索
|
|
|
JSONObject dataMap = ((JSONObject) searchResult.getData());
|
|
|
dataMap.put("suggestion", recommendWordsService.recommendWords(searchResult, paramMap));
|
|
|
}
|
...
|
...
|
@@ -79,90 +90,80 @@ public class WebProductListService extends AbstractCacheAbleService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchApiResult innerProductList(Map<String, String> paramMap) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[func=productList][param={}][begin={}]", paramMap, begin);
|
|
|
|
|
|
// 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 || page * pageSize > 1000000) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
|
|
|
}
|
|
|
if (pageSize > 100) {
|
|
|
pageSize = 100;
|
|
|
}
|
|
|
// 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 = webProductIndexBaseService.getWebProductIndexIncludeFields();
|
|
|
searchParam.setIncludeFields(includeFields);
|
|
|
|
|
|
// 5)从缓存中获取数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
return new SearchApiResult().setData(cacheObject);
|
|
|
}
|
|
|
|
|
|
// 6)查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null) {
|
|
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
|
|
}
|
|
|
|
|
|
// 7)记录关键字对应的查询结果
|
|
|
String queryWord = paramMap.get("query");
|
|
|
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
|
|
|
searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());
|
|
|
}
|
|
|
|
|
|
// 8)构造返回结果
|
|
|
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 = webProductIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
|
|
|
|
|
|
if(searchCommonHelper.isOrderEmpty(paramMap)){
|
|
|
product_list = productListSortHelper.sortProductList(product_list);// 处理一下商品的顺序;
|
|
|
}
|
|
|
dataMap.put("product_list",product_list);
|
|
|
|
|
|
// 9)将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
|
|
|
logger.info("[func=productList][cost={}]", System.currentTimeMillis() - begin);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
private SearchApiResult innerProductList(Map<String, String> paramMap,int page,int pageSize) throws Exception {
|
|
|
// 1)构建基本查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true);
|
|
|
searchParam.setAggregationBuilders(null);
|
|
|
// 2)设置分页参数
|
|
|
searchParam.setSize(pageSize);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
// 3/**/)设置排序字段
|
|
|
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
|
|
|
// 4)设置返回的结果
|
|
|
List<String> includeFields = webProductIndexBaseService.getWebProductIndexIncludeFields();
|
|
|
searchParam.setIncludeFields(includeFields);
|
|
|
// 5)从缓存中获取数据
|
|
|
JSONObject webProductListResult = super.queryWithCache(searchParam, paramMap);
|
|
|
return new SearchApiResult().setData(webProductListResult);
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
}
|
|
|
@Override
|
|
|
protected JSONObject doRealQuery(SearchParam searchParam, Map<String, String> paramMap) throws Exception {
|
|
|
//1、执行查询
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (searchResult == null) {
|
|
|
return new JSONObject();
|
|
|
}
|
|
|
|
|
|
//2、填充变价计划
|
|
|
List<Map<String, Object>> product_list = webProductIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
|
|
|
|
|
|
//3、按需排序
|
|
|
if (searchCommonHelper.isOrderEmpty(paramMap)) {
|
|
|
product_list = productListSortHelper.sortProductList(product_list);// 处理一下商品的顺序;
|
|
|
}
|
|
|
|
|
|
//4、构造返回结果
|
|
|
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());
|
|
|
dataMap.put("product_list", product_list);
|
|
|
return dataMap;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.CACHE_15_MINUTE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String cacheSceneKey() {
|
|
|
return "WEB_PRODUCT_LIST";
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|