Authored by unknown

Merge branch 'master' into new_list

... ... @@ -12,11 +12,15 @@ public class SearchCacheMatchLogger {
private static final Logger CACHE_MATCH_REQUEST = LoggerFactory.getLogger("CACHE_MATCH_REQUEST");
public static void doSearchCacheMatchLog(String url, Map<String, String> paramMap) {
CACHE_MATCH_REQUEST.info("match cache , url is :{}?{}", url, HttpServletRequestUtils.genParamString(paramMap));
if(CACHE_MATCH_REQUEST.isInfoEnabled()){
CACHE_MATCH_REQUEST.info("match cache , url is :{}?{}", url, HttpServletRequestUtils.genParamString(paramMap));
}
}
public static void doSearchCacheMatchLog(String method, String key) {
CACHE_MATCH_REQUEST.info("match cache , method is :{}, key is {}", method, key);
if(CACHE_MATCH_REQUEST.isInfoEnabled()){
CACHE_MATCH_REQUEST.info("match cache , method is :{}, key is {}", method, key);
}
}
}
... ...
package com.yoho.search.common.interceptor;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
... ... @@ -12,6 +13,7 @@ import org.springframework.web.servlet.ModelAndView;
import com.yoho.core.common.monitor.ThreadProfile;
import com.yoho.core.common.utils.HttpRequestUtils;
import com.yoho.search.base.monitor.PerformanceMonitor;
import com.yoho.search.common.downgrade.DownGradeService;
import com.yoho.search.common.utils.HttpServletRequestUtils;
... ... @@ -23,14 +25,21 @@ import com.yoho.search.common.utils.HttpServletRequestUtils;
public class ControllerCostInterceptor implements HandlerInterceptor {
private static final Logger CONTROLLER_COST = LoggerFactory.getLogger("CONTROLLER_COST");
private static final Logger CONTROLLER_PERFORMANCE = LoggerFactory.getLogger("CONTROLLER_PERFORMANCE");
private static final ThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("ThreadLocal StartTime");
@Autowired
private PerformanceMonitor monitor;
@Autowired
private DownGradeService downGradeService;
private PerformanceMonitor monitor;
@PostConstruct
void init(){
monitor = new PerformanceMonitor("CONTROLLER_PERFORMANCE",CONTROLLER_PERFORMANCE, 10);
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
long beginTime = System.currentTimeMillis();// 1、开始时间
... ...
package com.yoho.search.common.interceptor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class PerformanceMonitor {
private static final Logger logger = LoggerFactory.getLogger(PerformanceMonitor.class);
private static final Logger CONTROLLER_PERFORMANCE = LoggerFactory.getLogger("CONTROLLER_PERFORMANCE");
private AtomicLong totalVisit = new AtomicLong(0);
private AtomicLong totalCost = new AtomicLong(0);
private AtomicLong over500msCount = new AtomicLong(0);
private AtomicLong over1000msCount = new AtomicLong(0);
private final int MonitorPeriodInSecond = 10;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
@PostConstruct
void init() {
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
logAndClear();
}
}, 0, MonitorPeriodInSecond, TimeUnit.SECONDS);
}
private void logAndClear() {
try {
long visitCount = totalVisit.longValue();
long vostMs = totalCost.longValue();
long average = visitCount == 0 ? 0 : (vostMs / visitCount);
long over500msCnt = over500msCount.longValue();
double over500msCntPercent = visitCount == 0 ? 0 : (over500msCnt * 100L / visitCount);
long over1000msCnt = over1000msCount.longValue();
double over1000msCntPercent = visitCount == 0 ? 0 : (over1000msCnt * 100L / visitCount);
CONTROLLER_PERFORMANCE.info("Performance.Monitoring,Peroid [{}]s,totalVisit[{}],totalCost[{}] ms,AverageCost[{}]ms,over500msCount[{}][{}%],over1000msCnt[{}][{}%]", MonitorPeriodInSecond, visitCount,
vostMs, average,over500msCnt,over500msCntPercent,over1000msCnt,over1000msCntPercent);
totalVisit.set(0);
totalCost.set(0);
over500msCount.set(0);
over1000msCount.set(0);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public void addVisitCount() {
totalVisit.incrementAndGet();
}
public void addCost(long cost) {
totalCost.addAndGet(cost);
if(cost>=1000){
over1000msCount.incrementAndGet();
}
if(cost>=500){
over500msCount.incrementAndGet();
}
}
}
... ... @@ -8,17 +8,22 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.monitor.PerformanceMonitor;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.es.impl.YohoIndexHelper;
... ... @@ -28,13 +33,21 @@ import com.yoho.search.core.es.model.SearchResult;
@Service
public class SearchCommonService implements ApplicationEventPublisherAware {
private static final Logger ES_PERFORMANCE = LoggerFactory.getLogger("ES_PERFORMANCE");
@Autowired
private ESClientMgr esClientMgr;
@Autowired
private YohoIndexHelper yohoIndexHelper;
private ApplicationEventPublisher publisher;
private PerformanceMonitor performanceMonitor;
@PostConstruct
void init(){
performanceMonitor = new PerformanceMonitor("ES_PERFORMANCE",ES_PERFORMANCE, 10);
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
... ... @@ -65,12 +78,20 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
IElasticsearchClient client = esClientMgr.getClient(indexName);
SearchResult searchResult = client.search(indexName, indexName, searchParam);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
return searchResult;
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount();
try {
IElasticsearchClient client = esClientMgr.getClient(indexName);
SearchResult searchResult = client.search(indexName, indexName, searchParam);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
return searchResult;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
* 根据多个索引查询
*
... ... @@ -79,10 +100,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public SearchResult doSearch(final List<String> indexNames, final SearchParam searchParam) {
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
return searchResult;
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount();
try {
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
return searchResult;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
... ... @@ -93,17 +122,25 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
if (searchParams == null || searchParams.isEmpty()) {
return new ArrayList<SearchResult>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
for (int i = 0; i < searchParams.size(); i++) {
SearchResult searchResult = results.get(i);
SearchParam searchParam = searchParams.get(i);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(searchParams==null?0:searchParams.size());
try {
if (searchParams == null || searchParams.isEmpty()) {
return new ArrayList<SearchResult>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
for (int i = 0; i < searchParams.size(); i++) {
SearchResult searchResult = results.get(i);
SearchParam searchParam = searchParams.get(i);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
return results;
}
/**
... ... @@ -114,20 +151,28 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public Map<String, Object> doGetCommon(final String indexName, final String id) {
if (StringUtils.isBlank(id)) {
return null;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return null;
}
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
return null;
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
try {
if (StringUtils.isBlank(id)) {
return null;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return null;
}
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
return null;
}
return response.getSourceAsMap();
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
return response.getSourceAsMap();
}
/**
... ... @@ -137,26 +182,34 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
Set<String> idSet = new HashSet<String>();
for (Object id : idList) {
idSet.add(id.toString());
}
MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, null);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
results.add(item.getResponse().getSource());
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(idList==null||idList.isEmpty()?0:1);
try {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
Set<String> idSet = new HashSet<String>();
for (Object id : idList) {
idSet.add(id.toString());
}
MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, null);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
results.add(item.getResponse().getSource());
}
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
return results;
}
}
... ...
package com.yoho.search.service.scene;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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.SearchPageIdDefine;
import com.yoho.search.common.utils.SearchApiResultUtils;
... ... @@ -14,18 +26,6 @@ import com.yoho.search.service.scene.common.SceneSelectionsService;
import com.yoho.search.service.service.IAggRecommendService;
import com.yoho.search.service.service.IProductIndexService;
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.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Service
public class FuzzySceneService extends AbstractSceneService {
... ... @@ -44,12 +44,12 @@ public class FuzzySceneService extends AbstractSceneService {
private SearchDynamicConfigService searchDynamicConfigService;
@Autowired
private SearchKeyWordHelper searchKeyWordService;
@Autowired
private IProductIndexService productIndexService;
@Autowired
private IAggRecommendService aggRecommendService;
private IProductIndexService productIndexService;
@Autowired
private IAggRecommendService recommendService;
private ExecutorService executorService = Executors.newFixedThreadPool(100);
private ExecutorService executor = Executors.newFixedThreadPool(100);
// 当少于20个商品时 返回智能搜索词提示
private static final int SMART_SUGGESTION_PRODUCT_LIMIT = 20;
... ... @@ -81,29 +81,30 @@ public class FuzzySceneService extends AbstractSceneService {
// 2、添加默认参数
this.addParamsToParamMap(paramMap);
// 3、获取商品列表
SearchApiResult searchApiResult = sceneProductListService.productList(paramMap);
// 4、加入建议词
this.addSuggestion(searchApiResult, paramMap);
// 5、模糊搜索页记录关键字对应的查询结果
CompletableFuture<SearchApiResult> productListuture = CompletableFuture.supplyAsync(() -> sceneProductListService.productList(this.newParamMap(paramMap)), executor);
// 4、获取自定义标签聚合结果
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> productIndexService.aggCustomizeTag(this.newParamMap(paramMap)), executor);
// 5、获取促销专题
CompletableFuture<List<Object>> recommendPromotionFuture = CompletableFuture.supplyAsync(() -> recommendService.recommendPromotion(this.newParamMap(paramMap)),
executor);
// 6、加入建议词
SearchApiResult productListResult = productListuture.get();
this.addSuggestion(productListResult, paramMap);
// 7、模糊搜索页记录关键字对应的查询结果
String queryWord = paramMap.get("query");
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
long total = ((JSONObject) searchApiResult.getData()).getLongValue("total");
long total = ((JSONObject) productListResult.getData()).getLongValue("total");
searchKeyWordService.recordKeyWordByResultCount(queryWord, total);
}
ConcurrentHashMap<String, String> syncParamMap = new ConcurrentHashMap<>(paramMap);
// 6、获取自定义标签聚合结果
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> productIndexService.aggCustomizeTag(syncParamMap), executorService);
CompletableFuture<List<Object>> recommendProductFuture = CompletableFuture.supplyAsync(() -> aggRecommendService.recommendPromotion(syncParamMap), executorService);
SearchApiResult customizeTags = customizeTagFuture.get();
List<Object> recommendProducts = recommendProductFuture.get();
JSONObject dataMap = (JSONObject)searchApiResult.getData();
dataMap.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
// 8、组合结果
SearchApiResult customizeTags = customizeTagFuture.get();
List<Object> recommendProducts = recommendPromotionFuture.get();
JSONObject dataMap = (JSONObject) productListResult.getData();
dataMap.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
dataMap.put(RECOMMEND_PROMOTION_LIST, recommendProducts);
return searchApiResult;
return productListResult;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return SearchApiResultUtils.errorSearchApiResult("fuzzyProductList", paramMap, e);
... ...
... ... @@ -41,7 +41,7 @@ public class ReducePriceSceneService extends AbstractSceneService {
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISDISCOUNT, "Y");
// 默认一周降价
long end = DateUtil.getLastTimeSecond(new Date());
long begin = DateUtil.getFirstTimeSecond(DateUtil.addDay(new Date(), -365));
long begin = DateUtil.getFirstTimeSecond(DateUtil.addDay(new Date(), -7));
paramMap.put(SearchRequestParams.PARAM_SEARCH_PRICE_UPDATE_TIME, begin + "," + end);
}
... ...
package com.yoho.search.service.scene;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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.SearchPageIdDefine;
import com.yoho.search.models.SearchApiResult;
... ... @@ -10,21 +22,6 @@ import com.yoho.search.service.scene.common.SceneProductListService;
import com.yoho.search.service.scene.common.SceneSelectionsService;
import com.yoho.search.service.service.IAggRecommendService;
import com.yoho.search.service.service.IProductIndexService;
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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Service
public class SortSceneService extends AbstractSceneService {
... ... @@ -85,31 +82,25 @@ public class SortSceneService extends AbstractSceneService {
}
// 1、添加默认参数
this.addParamsToParamMap(paramMap);
ConcurrentHashMap<String, String> syncParamMap = new ConcurrentHashMap<>(paramMap);
CompletableFuture<SearchApiResult> productListFuture = null;
// 2、获取商品列表
CompletableFuture<SearchApiResult> productListFuture = CompletableFuture.supplyAsync(() -> {
try {
return sceneProductListService.productList(syncParamMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}, executorService);
// 3、获取规则和自定义标签的聚合结果
CompletableFuture<SearchApiResult> standardsFuture = CompletableFuture.supplyAsync(() -> productIndexService.aggStandard(syncParamMap), executorService);
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> productIndexService.aggCustomizeTag(syncParamMap), executorService);
CompletableFuture<List<Object>> recommendProductFuture = CompletableFuture.supplyAsync(() -> aggRecommendService.recommendPromotion(syncParamMap), executorService);
if(!"13420925".equals(this.getuid(paramMap))){
productListFuture = CompletableFuture.supplyAsync(() -> sceneProductListService.productList(this.newParamMap(paramMap)),executorService);
}else{
productListFuture = CompletableFuture.supplyAsync(() -> sceneProductListService.productList(this.newParamMap(paramMap)),executorService);
}
// 3、获取聚合结果
CompletableFuture<SearchApiResult> standardsFuture = CompletableFuture.supplyAsync(() -> productIndexService.aggStandard(this.newParamMap(paramMap)),executorService);
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> productIndexService.aggCustomizeTag(this.newParamMap(paramMap)),executorService);
CompletableFuture<List<Object>> recommendProductFuture = CompletableFuture.supplyAsync(() -> aggRecommendService.recommendPromotion(this.newParamMap(paramMap)), executorService);
// 4、组合结果
SearchApiResult productList = productListFuture.get();
SearchApiResult standards = standardsFuture.get();
SearchApiResult customizeTags = customizeTagFuture.get();
SearchApiResult customizeTags = customizeTagFuture.get();
List<Object> recommendProducts = recommendProductFuture.get();
JSONObject jsonObject = (JSONObject) productList.getData();
jsonObject.put("standard", standards.getData());
jsonObject.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
jsonObject.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
jsonObject.put(RECOMMEND_PROMOTION_LIST, recommendProducts);
return productList;
} catch (Exception e) {
... ... @@ -135,51 +126,4 @@ public class SortSceneService extends AbstractSceneService {
}
}
/**
* 品类列表页针对第一页的商品,将价格高的往前面平移8个
*
* @param paramMap
* @param product_list
* @return
*/
@SuppressWarnings("unused")
private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
// 判断页面合法性
if (!searchCommonHelper.isSortPageDefault(paramMap)) {
return product_list;
}
String page = paramMap.get("page");
if (StringUtils.isNotBlank(page) && !page.equals("1")) {
return product_list;
}
// 判断总数
int total = product_list.size();
if (total <= 10) {
return product_list;
}
// 计算均价
double totalPrice = 0;
for (Map<String, Object> product : product_list) {
totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);
}
double averagePrice = totalPrice / total;
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
results.add(product_list.get(0));
product_list.remove(0);
Iterator<Map<String, Object>> iterator = product_list.iterator();
int moveCount = 0;
while (iterator.hasNext() && moveCount < 8) {
Map<String, Object> product = iterator.next();
if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {
results.add(product);
moveCount++;
iterator.remove();
}
}
if (!product_list.isEmpty()) {
results.addAll(product_list);
}
return results;
}
}
... ...
... ... @@ -2,15 +2,29 @@ package com.yoho.search.service.scene.common;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
public abstract class AbstractSceneService {
protected static final String RECOMMEND_PROMOTION_LIST = "recommend_promotion_list";
protected static final String CUSTOMIZE_TAG_LIST = "customize_tag";
protected Map<String, String> newParamMap(Map<String, String> paramMap) {
return new HashMap<String, String>(paramMap);
}
protected CompletableFuture<?> getFuture(Supplier<?> supplier,Executor executor){
return CompletableFuture.supplyAsync(() -> supplier,executor);
}
/**
* 场景化的默认参数
*
... ... @@ -33,6 +47,10 @@ public abstract class AbstractSceneService {
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
return page;
}
protected String getuid(Map<String, String> paramMap) {
return MapUtils.getString(paramMap, "uid", "0");
}
public abstract void addParamsToParamMap(Map<String, String> paramMap);
... ...
... ... @@ -5,9 +5,12 @@ import java.util.List;
import java.util.Map;
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.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.cache.SearchCacheMatchLogger;
... ... @@ -24,6 +27,8 @@ import com.yoho.search.service.helper.SearchSortHelper;
@Service
public class SceneProductListService extends AbstractCacheAbleService {
private static final Logger logger = LoggerFactory.getLogger(SceneProductListService.class);
@Autowired
private SearchCommonService searchCommonService;
@Autowired
... ... @@ -47,37 +52,41 @@ public class SceneProductListService extends AbstractCacheAbleService {
* @return
* @throws Exception
*/
public SearchApiResult productList(Map<String, String> paramMap) throws Exception {
// 1)构造搜索参数
SearchParam searchParam = this.buildProductListSearchParam(paramMap);
public SearchApiResult productList(Map<String, String> paramMap) {
try {
// 1)构造搜索参数
SearchParam searchParam = this.buildProductListSearchParam(paramMap);
// 2)从缓存中获取数据
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, indexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/scene/productList.json", paramMap);
return new SearchApiResult().setData(cacheObject);
}
// 2)从缓存中获取数据
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, indexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/scene/productList.json", paramMap);
return new SearchApiResult().setData(cacheObject);
}
// 3)查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 3)查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 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());
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序;
// 5)将结果存进缓存
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
return new SearchApiResult().setData(dataMap);
// 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());
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序;
// 5)将结果存进缓存
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setCode(500).setMessage("scene productList exception").setData(new JSONArray());
}
}
private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
... ...
... ... @@ -49,19 +49,25 @@
</mvc:interceptors>
<!--字符串转换器-->
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- json转换器 application/json -->
<bean id="jsonConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="features">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<value>DisableCircularReferenceDetect</value>
... ...
... ... @@ -148,7 +148,7 @@
</encoder>
</appender>
<!-- controller-cost appender -->
<!-- controller-performance appender -->
<appender name="CONTROLLER_PERFORMANCE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/%d{yyyy-MM-dd}/controller-performance.log</fileNamePattern>
... ... @@ -163,6 +163,21 @@
</encoder>
</appender>
<!-- es-performance appender -->
<appender name="ES_PERFORMANCE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/%d{yyyy-MM-dd}/es-performance.log</fileNamePattern>
<!-- 日志最大的保存天数 -->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>${maxFileSize}</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-1relative - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} -- %msg%n</pattern>
</encoder>
</appender>
<!-- downgrade appender -->
<appender name="DOWNGRADE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
... ... @@ -268,6 +283,12 @@
<level value="INFO"/>
<appender-ref ref="CONTROLLER_PERFORMANCE_APPENDER"/>
</logger>
<!-- ES耗时统计 -->
<logger name="ES_PERFORMANCE" additivity="false">
<level value="INFO"/>
<appender-ref ref="ES_PERFORMANCE_APPENDER"/>
</logger>
<!-- 降级日志 -->
<logger name="DOWNGRADE" additivity="false">
... ...
... ... @@ -214,7 +214,7 @@
</encoder>
</appender>
<!-- 请求超时 appender -->
<!-- controller-performance appender -->
<appender name="CONTROLLER_PERFORMANCE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${yoho.logs.basedir}/${yoho.search.service.env.namespace}/controller-performance.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
... ... @@ -232,6 +232,24 @@
</encoder>
</appender>
<!-- es-performance appender -->
<appender name="ES_PERFORMANCE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${yoho.logs.basedir}/${yoho.search.service.env.namespace}/es-performance.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${yoho.logs.basedir}/${yoho.search.service.env.namespace}/archived/es-performance.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>${yoho.logs.maxFileSize}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>${yoho.logs.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger:%line - %msg%n</pattern>
</encoder>
</appender>
<!-- 降级 appender -->
<appender name="DOWNGRADE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${yoho.logs.basedir}/${yoho.search.service.env.namespace}/down-grade.log</file>
... ... @@ -342,10 +360,16 @@
<!-- 性能监控 -->
<logger name="CONTROLLER_PERFORMANCE" additivity="false">
<level value="INFO"/>
<level value="${yoho.logs.level}"/>
<appender-ref ref="CONTROLLER_PERFORMANCE_APPENDER"/>
</logger>
<!-- ES耗时统计 -->
<logger name="ES_PERFORMANCE" additivity="false">
<level value="INFO"/>
<appender-ref ref="ES_PERFORMANCE_APPENDER"/>
</logger>
<!-- 降级日志 -->
<logger name="DOWNGRADE" additivity="false">
<level value="INFO"/>
... ... @@ -353,8 +377,8 @@
</logger>
<!-- 个性化搜索日志 -->
<logger name="PERSONALIZED" additivity="${yoho.logs.level}">
<level value="INFO"/>
<logger name="PERSONALIZED" additivity="false">
<level value="${yoho.logs.level}"/>
<appender-ref ref="PERSONALIZED_APPENDER"/>
</logger>
... ...