Authored by wangnan9279

ufo 销售日历 聚合日期数量接口

... ... @@ -29,4 +29,12 @@ public class UfoSaleCalendarListController {
return ufoSaleCalendarService.SaleCalendarProductList(paramMap);
}
@RequestMapping(method = RequestMethod.GET, value = "/ufo/SaleDateCountList")
@ResponseBody
public SearchApiResult SaleDateCountList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return ufoSaleCalendarService.SaleDateCountList(paramMap);
}
}
... ...
... ... @@ -7,6 +7,7 @@ import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.UfoProductIndexEsField;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.common.utils.UfoImageUrlAssist;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
... ... @@ -14,6 +15,11 @@ import com.yoho.search.service.index.UfoProductIndexBaseService;
import org.apache.commons.collections.MapUtils;
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.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
... ... @@ -42,6 +48,7 @@ public class UfoSaleCalendarService {
private UfoProductIndexBaseService ufoProductIndexBaseService;
private static final String RETURN_LIST_NAME = "product_list";
private static final String SALE_TIME_AGG_NAME = "sale_time_agg";
@SearchCacheAble(cacheName = "UFO_SALE_CALENDAR_PRODUCT_LIST", cacheInMinute = 3)
public SearchApiResult SaleCalendarProductList(Map<String, String> paramMap) {
... ... @@ -99,7 +106,7 @@ public class UfoSaleCalendarService {
Map<String, Object> productMap = new HashMap<String, Object>();
productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0));
productMap.put("product_name", MapUtils.getString(map, UfoProductIndexEsField.productName, ""));
productMap.put("default_images", MapUtils.getString(map, UfoProductIndexEsField.defaultImages, ""));
productMap.put("default_images", UfoImageUrlAssist.getAllProductPicUrl(MapUtils.getString(map, UfoProductIndexEsField.defaultImages, ""), "goodsimg", "center", "d2hpdGU="));
Double price = MapUtils.getDouble(map, UfoProductIndexEsField.price);
productMap.put("price", price == null || price == -1d ? null : price);
Integer saleTime = MapUtils.getInteger(map, UfoProductIndexEsField.saleTime, 0);
... ... @@ -108,4 +115,56 @@ public class UfoSaleCalendarService {
productMap.put("sale_time", month + "." + day);
return productMap;
}
@SearchCacheAble(cacheName = "UFO_SALE_DATE_COUNT_LIST", cacheInMinute = 3)
public SearchApiResult SaleDateCountList(Map<String, String> paramMap) {
try {
SearchParam searchParam = new SearchParam();
searchParam.setSize(0);
//1.构造过滤
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
searchParam.setFiter(boolFilter);
//2.构造聚合
List<AbstractAggregationBuilder<?>> list = new ArrayList<>();
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms(SALE_TIME_AGG_NAME).field(UfoProductIndexEsField.saleDate).size(200000);
list.add(firstAggregationBuilder);
searchParam.setAggregationBuilders(list);
//3.执行搜索
final String indexName = ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX;
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null || searchResult.getAggMaps() == null) {
return new SearchApiResult().setData(null).setCode(500);
}
Map<String, Aggregation> aggregationResult = searchResult.getAggMaps();
if (!aggregationResult.containsKey(SALE_TIME_AGG_NAME)) {
return new SearchApiResult().setData(null).setCode(500);
}
//4.构造返回数据结构
List<Map<String, Object>> saleDateCountList = this.getSeriesDateCountList(aggregationResult);
JSONObject jsonObject = new JSONObject();
jsonObject.put("sale_date_count_list", saleDateCountList);
jsonObject.put("total", saleDateCountList.size());
return new SearchApiResult().setData(jsonObject);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setCode(500);
}
}
private List<Map<String, Object>> getSeriesDateCountList(Map<String, Aggregation> aggregationResult) {
List<Map<String, Object>> returnList = new ArrayList<>();
MultiBucketsAggregation saleDateCountAgg = ((MultiBucketsAggregation) aggregationResult.get(SALE_TIME_AGG_NAME));
for (MultiBucketsAggregation.Bucket brandIdBucket : saleDateCountAgg.getBuckets()) {
Map<String, Object> saleDate = new HashMap<>();
saleDate.put("sale_date", brandIdBucket.getKeyAsString());
saleDate.put("count", brandIdBucket.getDocCount());
returnList.add(saleDate);
}
return returnList;
}
}
... ...