...
|
...
|
@@ -26,234 +26,247 @@ import com.yoho.search.service.service.IProductIndexService; |
|
|
@Service
|
|
|
public class ProductIndexServiceImpl implements IProductIndexService, ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductIndexServiceImpl.class);
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductIndexServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationService aggregationService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private AggregationService aggregationService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
ApplicationEventPublisher publisher;
|
|
|
ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
private SearchApiResult getSuccessResult(Object resultObject) {
|
|
|
SearchApiResult result = new SearchApiResult();
|
|
|
result.setCode(200);
|
|
|
result.setMessage("success");
|
|
|
result.setData(resultObject);
|
|
|
return result;
|
|
|
}
|
|
|
private SearchApiResult getSuccessResult(Object resultObject) {
|
|
|
SearchApiResult result = new SearchApiResult();
|
|
|
result.setCode(200);
|
|
|
result.setMessage("success");
|
|
|
result.setData(resultObject);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private SearchApiResult getErrorResult(String methodName, Object param, Exception e) {
|
|
|
logger.error("[※查询]失败:[func={}][param={}][message={}]", methodName, JsonUtil.toJson(param), e);
|
|
|
SearchApiResult result = new SearchApiResult();
|
|
|
result.setCode(500);
|
|
|
result.setMessage(e.getMessage());
|
|
|
result.setData(new JSONObject());
|
|
|
return result;
|
|
|
}
|
|
|
private SearchApiResult getErrorResult(String methodName, Object param, Exception e) {
|
|
|
logger.error("[※查询]失败:[func={}][param={}][message={}]", methodName, JsonUtil.toJson(param), e);
|
|
|
SearchApiResult result = new SearchApiResult();
|
|
|
result.setCode(500);
|
|
|
result.setMessage(e.getMessage());
|
|
|
result.setData(new JSONObject());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public interface Searcher {
|
|
|
public Object getResult() throws Exception;
|
|
|
}
|
|
|
public interface Searcher {
|
|
|
public Object getResult() throws Exception;
|
|
|
}
|
|
|
|
|
|
private SearchApiResult getSearchApiResult(String methodName, Map<String, String> paramMap, Searcher searcher) {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[model=ProductIndexService][func={}][param={}][begin={}]", methodName, paramMap.toString(), begin);
|
|
|
try {
|
|
|
Object resultObject = searcher.getResult();
|
|
|
return this.getSuccessResult(resultObject);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.PRODUCTINDEXSERVICEIMPL_GETSEARCHAPIRESULT.getEventName(),
|
|
|
EventReportEnum.PRODUCTINDEXSERVICEIMPL_GETSEARCHAPIRESULT.getFunctionName(), EventReportEnum.PRODUCTINDEXSERVICEIMPL_GETSEARCHAPIRESULT.getMoudleName(),
|
|
|
"exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return this.getErrorResult(methodName, paramMap, e);
|
|
|
} finally {
|
|
|
long end = System.currentTimeMillis();
|
|
|
logger.info("[model=ProductIndexService][func={}][param={}][end={}][cost={}ms]", methodName, paramMap.toString(), end, end - begin);
|
|
|
}
|
|
|
}
|
|
|
private SearchApiResult getSearchApiResult(String methodName, Map<String, String> paramMap, Searcher searcher) {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[model=ProductIndexService][func={}][param={}][begin={}]", methodName, paramMap.toString(), begin);
|
|
|
try {
|
|
|
Object resultObject = searcher.getResult();
|
|
|
return this.getSuccessResult(resultObject);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.PRODUCTINDEXSERVICEIMPL_GETSEARCHAPIRESULT.getEventName(),
|
|
|
EventReportEnum.PRODUCTINDEXSERVICEIMPL_GETSEARCHAPIRESULT.getFunctionName(), EventReportEnum.PRODUCTINDEXSERVICEIMPL_GETSEARCHAPIRESULT.getMoudleName(),
|
|
|
"exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return this.getErrorResult(methodName, paramMap, e);
|
|
|
} finally {
|
|
|
long end = System.currentTimeMillis();
|
|
|
logger.info("[model=ProductIndexService][func={}][param={}][end={}][cost={}ms]", methodName, paramMap.toString(), end, end - begin);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggAgeLevel(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggAgeLevel", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getAgeLevelAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("ageLevelAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggAgeLevel(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggAgeLevel", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getAgeLevelAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("ageLevelAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggGender(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggGender", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getGenderNewAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("genderAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggGender(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggGender", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getGenderNewAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("genderAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggPrice(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggPrice", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getPriceAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("priceAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggPrice(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggPrice", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getPriceAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("priceAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggColor(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggColor", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getColorAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("colorAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggColor(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggColor", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getColorAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("colorAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggStyle(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggStyle", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getStyleAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("styleAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggStyle(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggStyle", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getStyleAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("styleAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggBrand(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggBrand", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getBrandAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("brandAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggBrand(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggBrand", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getBrandAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("brandAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggStandard(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggStandard", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getStandardAggregationResult(paramMap);
|
|
|
return jsonObject.get("standardAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggStandard(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggStandard", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getStandardAggregationResult(paramMap);
|
|
|
return jsonObject.get("standardAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggSize(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggSize", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getSizeAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("sizeAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggSize(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggSize", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getSizeAggregationResult(paramMap, false);
|
|
|
return jsonObject.get("sizeAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggPromotion(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggPromotion", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getPromotionAggregationResult(paramMap, 1000);
|
|
|
return jsonObject.get("promotionAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggPromotion(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggPromotion", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getPromotionAggregationResult(paramMap, 1000);
|
|
|
return jsonObject.get("promotionAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggShops(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggShops", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getShopAggregationResult(paramMap, 100);
|
|
|
return jsonObject.get("shopAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggShops(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggShops", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getShopAggregationResult(paramMap, 100);
|
|
|
return jsonObject.get("shopAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggNew(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggNew", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getIsNewAggregationResult(paramMap, false);
|
|
|
Object isNewResponse = jsonObject.get("isnewAgg");
|
|
|
if (isNewResponse != null && ((Boolean) isNewResponse).booleanValue()) {
|
|
|
return "Y";
|
|
|
} else {
|
|
|
return "N";
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggLimited(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggLimited", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getIsLimitAggregationResult(paramMap, false);
|
|
|
Object islimitedAgg = jsonObject.get("islimitedAgg");
|
|
|
if (islimitedAgg != null && ((Boolean) islimitedAgg).booleanValue()) {
|
|
|
return "Y";
|
|
|
} else {
|
|
|
return "N";
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggNew(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggNew", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getIsNewAggregationResult(paramMap, false);
|
|
|
Object isNewResponse = jsonObject.get("isnewAgg");
|
|
|
if (isNewResponse != null && ((Boolean) isNewResponse).booleanValue()) {
|
|
|
return "Y";
|
|
|
} else {
|
|
|
return "N";
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggSpecialoffer(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggSpecialoffer", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getIsSecialofferAggregationResult(paramMap, false);
|
|
|
Object specialofferAgg = jsonObject.get("specialofferAgg");
|
|
|
if (specialofferAgg != null && ((Boolean) specialofferAgg).booleanValue()) {
|
|
|
return "Y";
|
|
|
} else {
|
|
|
return "N";
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggLimited(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggLimited", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getIsLimitAggregationResult(paramMap, false);
|
|
|
Object islimitedAgg = jsonObject.get("islimitedAgg");
|
|
|
if (islimitedAgg != null && ((Boolean) islimitedAgg).booleanValue()) {
|
|
|
return "Y";
|
|
|
} else {
|
|
|
return "N";
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggKeywords(Map<String, String> paramMap) {
|
|
|
final int randomKeywordCount = StringUtils.isBlank(paramMap.get("keyword_count")) ? 8 : Integer.parseInt(paramMap.get("keyword_count"));
|
|
|
return this.getSearchApiResult("aggKeywords", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getKeywordAggregationResult(paramMap, 50);
|
|
|
@SuppressWarnings("unchecked")
|
|
|
List<String> keywordList = (List<String>) jsonObject.get("keywordAgg");
|
|
|
return getRandomKeywords(keywordList, randomKeywordCount);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggSpecialoffer(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggSpecialoffer", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getIsSecialofferAggregationResult(paramMap, false);
|
|
|
Object specialofferAgg = jsonObject.get("specialofferAgg");
|
|
|
if (specialofferAgg != null && ((Boolean) specialofferAgg).booleanValue()) {
|
|
|
return "Y";
|
|
|
} else {
|
|
|
return "N";
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private List<String> getRandomKeywords(List<String> keywordList, int randomKeywordCount) {
|
|
|
if (keywordList == null || keywordList.size() <= randomKeywordCount) {
|
|
|
return keywordList;
|
|
|
}
|
|
|
return new Random().ints(0, keywordList.size()).distinct().limit(randomKeywordCount).mapToObj(index -> keywordList.get(index)).collect(Collectors.toList());
|
|
|
}
|
|
|
@Override
|
|
|
public SearchApiResult aggKeywords(Map<String, String> paramMap) {
|
|
|
final int randomKeywordCount = StringUtils.isBlank(paramMap.get("keyword_count")) ? 8 : Integer.parseInt(paramMap.get("keyword_count"));
|
|
|
return this.getSearchApiResult("aggKeywords", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getKeywordAggregationResult(paramMap, 50);
|
|
|
@SuppressWarnings("unchecked")
|
|
|
List<String> keywordList = (List<String>) jsonObject.get("keywordAgg");
|
|
|
return getRandomKeywords(keywordList, randomKeywordCount);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private List<String> getRandomKeywords(List<String> keywordList, int randomKeywordCount) {
|
|
|
if (keywordList == null || keywordList.size() <= randomKeywordCount) {
|
|
|
return keywordList;
|
|
|
}
|
|
|
return new Random().ints(0, keywordList.size()).distinct().limit(randomKeywordCount).mapToObj(index -> keywordList.get(index)).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggCustomizeTag(Map<String, String> paramMap) {
|
|
|
return this.getSearchApiResult("aggCustomizeTag", paramMap, new Searcher() {
|
|
|
@Override
|
|
|
public Object getResult() throws Exception {
|
|
|
JSONObject jsonObject = aggregationService.getCustomizeTagResult(paramMap);
|
|
|
return jsonObject.getJSONArray("customizeTagAgg");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
} |
...
|
...
|
|