Authored by unknown

优化场景拆分的接口

package com.yoho.search.service.common;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
/**
* Created by ginozhang on 2017/3/30.
*/
... ...
... ... @@ -18,7 +18,7 @@ import com.yoho.search.service.downgrade.DownGradeService;
import com.yoho.search.service.monitor.PerformanceMonitor;
import com.yoho.search.service.utils.HttpServletRequestUtils;
public class ControllerCostInterceptor implements HandlerInterceptor{
public class ControllerCostInterceptor implements HandlerInterceptor {
private static final Logger CONTROLLER_COST = LoggerFactory.getLogger("CONTROLLER_COST");
... ... @@ -56,23 +56,15 @@ public class ControllerCostInterceptor implements HandlerInterceptor{
// 3、增加相应耗时
monitor.addCost(cost);
// // 4、上报耗时
// this.publisher
// .publishEvent(new
// ServiceAccessEvent(HttpRequestUtils.getGatewayServiceName(request),
// cost, response.getStatus(),
// this.getRequestUrl(request).toString()));
// 5、如果响应时间小于100ms,则直接返回
// 4、如果响应时间小于100ms,则直接返回
if (cost <= 500) {
return;
}
// 6、耗时超过1000ms,则试图去降级
// 5、耗时超过1000ms,则试图去降级
if (cost >= 500) {
downGradeService.tryDowngrade();
}
// 7、打印响应超过500ms的请求与参数
// 6、打印响应超过500ms的请求与参数
CONTROLLER_COST.info("run more than 500ms ,cost [{}] ms, RequestURL: {}", cost, getRequestUrl(request));
} catch (Exception e) {
... ... @@ -98,8 +90,4 @@ public class ControllerCostInterceptor implements HandlerInterceptor{
return sb;
}
Map<String, Object> getLogReqParams(final HttpServletRequest request, final HttpServletResponse response) {
return HttpRequestUtils.getRequestParams(request);
}
}
... ...
package com.yoho.search.service.servicenew.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.core.es.agg.IAggregation;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.service.service.helper.SearchParamHelper;
import com.yoho.search.service.service.helper.SearchSortHelper;
... ... @@ -102,33 +98,6 @@ public class BaseService {
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
return page;
}
protected 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);
setHighlight(paramMap, searchParam);
searchParam.setAggregationBuilders(null);
searchParam.setPage(page);
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
// 3)设置排序字段
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
// 4)设置查询结果返回字段
if (StringUtils.isNotBlank(paramMap.get("resultFields"))) {
String resultFields = paramMap.get("resultFields");
searchParam.setResultFields(Arrays.asList(resultFields.split(",")));
}
return searchParam;
}
protected JSONObject getFilterResults(List<IAggregation> aggregations, Map<String, Aggregation> aggMaps) {
JSONObject filter = new JSONObject();
... ... @@ -142,12 +111,4 @@ public class BaseService {
return filter;
}
private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) {
if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) {
searchParam.setHighlight(true);
List<String> highlightFields = new ArrayList<String>();
highlightFields.add("productName.productName_ansj");
searchParam.setHighlightFields(highlightFields);
}
}
}
... ...
... ... @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.SearchPageIdDefine;
import com.yoho.search.service.service.SearchDynamicConfigService;
import com.yoho.search.service.service.SearchKeyWordService;
import com.yoho.search.service.service.helper.SearchCommonHelper;
import com.yoho.search.service.servicenew.ISearchRecommendService;
import com.yoho.search.service.servicenew.scene.common.AbstractSceneService;
... ... @@ -21,7 +22,7 @@ import com.yoho.search.service.utils.SearchRequestParams;
import com.yoho.search.service.vo.SearchApiResult;
@Service
public class FuzzySceneService extends AbstractSceneService{
public class FuzzySceneService extends AbstractSceneService {
private static final Logger logger = LoggerFactory.getLogger(FuzzySceneService.class);
... ... @@ -35,16 +36,17 @@ public class FuzzySceneService extends AbstractSceneService{
private ISearchRecommendService searchRecommendService;
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
@Autowired
private SearchKeyWordService searchKeyWordService;
// 当少于20个商品时 返回智能搜索词提示
private static final int SMART_SUGGESTION_PRODUCT_LIMIT = 20;
@Override
public String pageId() {
return SearchPageIdDefine.PAGE_ID_SEARCH;
}
/**
* @1、返回商品列表
* @2、数量太多则返回建议词
... ... @@ -54,8 +56,14 @@ public class FuzzySceneService extends AbstractSceneService{
try {
// 1、获取商品列表
SearchApiResult searchApiResult = splitProductListService.productList(paramMap);
// 2、加入建议词
// 2)、加入建议词
this.addSuggestion(searchApiResult, paramMap);
// 3)模糊搜索页记录关键字对应的查询结果
String queryWord = paramMap.get("query");
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySkn(queryWord)) {
long total = ((JSONObject) searchApiResult.getData()).getLongValue("total");
searchKeyWordService.recordKeyWordByResultCount(queryWord, total);
}
return searchApiResult;
} catch (Exception e) {
logger.error("[func=fuzzyProductList][params=" + paramMap + "]", e);
... ...
... ... @@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.SearchPageIdDefine;
import com.yoho.search.service.service.helper.SearchCommonHelper;
import com.yoho.search.service.servicenew.scene.common.AbstractSceneService;
import com.yoho.search.service.servicenew.scene.common.SceneProductListService;
import com.yoho.search.service.servicenew.scene.common.SceneSelectionsService;
... ... @@ -24,6 +25,8 @@ public class SortSceneService extends AbstractSceneService {
private SceneProductListService splitProductListService;
@Autowired
private SceneSelectionsService splitSelectionsService;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Override
public String pageId() {
... ... @@ -33,13 +36,18 @@ public class SortSceneService extends AbstractSceneService {
@Override
public SearchApiResult productList(Map<String, String> paramMap) {
try {
return splitProductListService.productList(paramMap);
// 1、获取商品列表
SearchApiResult productList = splitProductListService.productList(paramMap);
// 2、返回规则聚合结果
// TODO
// 3、返回最终结果
return productList;
} catch (Exception e) {
logger.error("[func=sortProductList][params=" + paramMap + "]", e);
return SearchApiResultUtils.errorSearchApiResult("sortProductList", paramMap, e);
}
}
@Override
public SearchApiResult aggregations(Map<String, String> paramMap) {
// 1、获取通用筛选项
... ...
package com.yoho.search.service.servicenew.scene.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
... ... @@ -21,9 +22,10 @@ import com.yoho.search.service.cache.SearchCacheMatchLogger;
import com.yoho.search.service.cache.model.SearchCache;
import com.yoho.search.service.service.SearchCacheService;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.SearchKeyWordService;
import com.yoho.search.service.service.base.ProductIndexBaseService;
import com.yoho.search.service.service.helper.SearchCommonHelper;
import com.yoho.search.service.service.helper.SearchParamHelper;
import com.yoho.search.service.service.helper.SearchSortHelper;
import com.yoho.search.service.servicenew.impl.BaseService;
import com.yoho.search.service.vo.SearchApiResult;
... ... @@ -35,19 +37,21 @@ public class SceneProductListService extends BaseService {
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchKeyWordService searchKeyWordService;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private SearchSortHelper searchSortHelper;
private SearchCache productListSearchCache;
private SearchCache searchCache;
@PostConstruct
void init() {
productListSearchCache = searchCacheFactory.getProductListSearchCache();
searchCache = searchCacheFactory.getProductListSearchCache();
}
/**
... ... @@ -63,7 +67,7 @@ public class SceneProductListService extends BaseService {
// 2)从缓存中获取数据
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchCache, indexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/split/productList.json", paramMap);
return new SearchApiResult().setData(cacheObject);
... ... @@ -75,26 +79,27 @@ public class SceneProductListService extends BaseService {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 4)记录关键字对应的查询结果
String queryWord = paramMap.get("query");
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySkn(queryWord)) {
searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());
}
// 5)构造返回结果
// 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());
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null);
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));// 处理一下商品的顺序
// 6)将结果存进缓存
searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));
// 5)将结果存进缓存
searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap);
return new SearchApiResult().setData(dataMap);
}
/**
* 品类列表页针对第一页的商品,将价格高的往前面平移8个
*
* @param paramMap
* @param product_list
* @return
*/
private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
// 判断页面合法性
if (!searchCommonHelper.isSortPageDefault(paramMap)) {
... ... @@ -133,4 +138,40 @@ public class SceneProductListService extends BaseService {
}
return results;
}
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);
setHighlight(paramMap, searchParam);
searchParam.setAggregationBuilders(null);
searchParam.setPage(page);
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
// 3)设置排序字段
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
// 4)设置查询结果返回字段
if (StringUtils.isNotBlank(paramMap.get("resultFields"))) {
String resultFields = paramMap.get("resultFields");
searchParam.setResultFields(Arrays.asList(resultFields.split(",")));
}
return searchParam;
}
private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) {
if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) {
searchParam.setHighlight(true);
List<String> highlightFields = new ArrayList<String>();
highlightFields.add("productName.productName_ansj");
searchParam.setHighlightFields(highlightFields);
}
}
}
... ...