Authored by hugufei

DiscountService使用AbstractPageComponent重构

... ... @@ -35,6 +35,8 @@ public class CacheTimeConstants {
public static final int CACHE_60_MINUTE = 60;
public static final int CACHE_30_MINUTE = 30;
public static final int CACHE_10_MINUTE = 10;
}
... ...
... ... @@ -2,7 +2,9 @@ package com.yoho.search.service.helper;
import java.util.Map;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -35,22 +37,46 @@ public class SearchParamHelper {
}
public SearchParam buildSearchParam(Map<String, String> paramMap, boolean needPersional, BoolQueryBuilder mustFilter, String filterParamName) throws Exception {
ParamQueryFilter paramQueryFilter = buildParamQueryFilter(paramMap,needPersional,mustFilter,filterParamName);
SearchParam searchParam = this.buildSearchParam(paramQueryFilter);
return searchParam;
}
public SearchParam buildSearchParam(ParamQueryFilter paramQueryFilter){
SearchParam searchParam = new SearchParam();
searchParam.setFiter(paramQueryFilter.getParamFilter());
searchParam.setQuery(paramQueryFilter.getParamQuery());
searchParam.setSize(0);
return searchParam;
}
public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap) throws Exception {
return buildParamQueryFilter(paramMap,false,null,null);
}
public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap,String filterParamName) throws Exception {
return buildParamQueryFilter(paramMap,false,null,filterParamName);
}
public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap,BoolQueryBuilder mustFilter) throws Exception {
return buildParamQueryFilter(paramMap,false,mustFilter,null);
}
public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap, boolean needPersional, BoolQueryBuilder mustFilter,String filterParamName) throws Exception {
// 1、构造filter
BoolQueryBuilder filter = searchQueryHelper.constructFilterBuilder(paramMap, filterParamName);
if (mustFilter != null) {
filter.must(mustFilter);
}
searchParam.setFiter(filter);
// 2、构造query
QueryBuilder queryBuilder;
if (needPersional) {
searchParam.setQuery(searchQueryHelper.constructQueryBuilderPersional(paramMap, filter));
queryBuilder = searchQueryHelper.constructQueryBuilderPersional(paramMap, filter);
} else {
searchParam.setQuery(searchQueryHelper.constructQueryBuilder(paramMap));
queryBuilder = searchQueryHelper.constructQueryBuilder(paramMap);
}
// 3、默认设置size为0
searchParam.setSize(0);
return searchParam;
return new ParamQueryFilter(queryBuilder,filter);
}
}
... ...
package com.yoho.search.service.scene.general;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.beans.AbstractPageComponent;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.core.es.agg.IAggregation;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
... ... @@ -14,6 +15,7 @@ import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.aggregations.impls.AggregationFactory;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -26,88 +28,74 @@ import java.util.Arrays;
import java.util.Map;
@Service
public class DiscountService implements ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(DiscountService.class);
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private SearchCommonService searchCommonService;
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
@SearchCacheAble(cacheInMinute = 30, cacheName = "DISCOUNT", excludeParams = {"client_type", "yh_channel", "order", "uid", "udid"})
public SearchApiResult discount(Map<String, String> paramMap) {
try {
// 构造查询参数
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
// 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();
searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));
searchParam.setSize(0);
// 构造返回结果
SearchApiResult searchApiResult = new SearchApiResult().setMessage("discount List.");
// es检索
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null || searchResult.getAggMaps() == null) {
return searchApiResult.setData("");
}
Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
if (discountAggResult == null) {
return searchApiResult.setData("");
}
JSONObject discountObject = new JSONObject();
discountObject.put("discount", discountAggResult);
return searchApiResult.setData(discountObject);
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult("discount", paramMap, e);
}
}
public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
try {
logger.info("[func=discount][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 构造查询参数
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilter);
// 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();
searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));
searchParam.setSize(0);
// es检索
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null || searchResult.getAggMaps() == null) {
return null;
}
Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
if (discountAggResult == null) {
return null;
}
JSONObject discountObject = new JSONObject();
discountObject.put("discount", discountAggResult);
return discountObject.getJSONObject("discount");
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return null;
}
}
public class DiscountService extends AbstractPageComponent<JSONObject> implements ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(DiscountService.class);
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private SearchCommonService searchCommonService;
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public SearchApiResult discount(Map<String, String> paramMap) {
JSONObject discountObject = discount(paramMap, null);
return new SearchApiResult().setMessage("discount List.").setData(discountObject);
}
public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
try {
ParamQueryFilter paramQueryFilter = searchParamHelper.buildParamQueryFilter(paramMap, mustFilter);
Object value = super.queryWithCache(paramQueryFilter, JSONObject.class);
return value == null ? null : (JSONObject) value;
} catch (Exception e) {
logger.error(e.getMessage(), e);
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return null;
}
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("DISCOUNT:").appendVar(paramQueryFilter.getParamMd5Key());
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_30_MINUTE;
}
@Override
protected JSONObject doRealQuery(ParamQueryFilter paramQueryFilter) {
// 构造查询参数
SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
// 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();
searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));
searchParam.setSize(0);
// es检索
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null || searchResult.getAggMaps() == null) {
return null;
}
Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
if (discountAggResult == null) {
return null;
}
JSONObject discountObject = new JSONObject();
discountObject.put("discount", discountAggResult);
return discountObject.getJSONObject("discount");
}
}
... ...