Authored by hugufei

增加最近7天上架商品数的聚合对象

... ... @@ -12,7 +12,7 @@ public abstract class AbstractAggregation implements IAggregation{
protected static final Logger logger = LoggerFactory.getLogger(AbstractAggregation.class);
protected MultiBucketsAggregation getAggregation(Map<String, Aggregation> aggMaps){
if(!aggMaps.containsKey(aggName())){
if(aggMaps==null || !aggMaps.containsKey(aggName())){
return null;
}
return (MultiBucketsAggregation)aggMaps.get(aggName());
... ...
... ... @@ -50,4 +50,8 @@ public class AggregationFactoryService {
return new StandardAggregation(searchCommonService,paramMap);
}
public IAggregation getRecentShelveDayAggregation(){
return new RecentShelveDayAggregation();
}
}
... ...
package com.yoho.search.aggregations.impls;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
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.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import com.yoho.search.aggregations.AbstractAggregation;
/**
* 最近7天每天上架商品数量的聚合对象
*
* @author hugufei
*
*/
public class RecentShelveDayAggregation extends AbstractAggregation {
RecentShelveDayAggregation(){
super();
}
@Override
public String aggName() {
return "recentShelveDayAgg";
}
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("shelveDay").size(7).order(Terms.Order.term(false));
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
Map<String, Object> map = new LinkedHashMap<String, Object>();
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
while (itAgg.hasNext()) {
Bucket lt = itAgg.next();
map.put(lt.getKey(), lt.getDocCount());
}
return map;
}
}
... ...
... ... @@ -96,13 +96,11 @@ public class SearchService {
jsonMap.put("data", "");
return jsonMap;
}
if (searchResult.getAggMaps() != null) {
//获取聚合结果
Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
if (discountAggResult != null) {
Map<String, Object> filter = new HashMap<String, Object>();
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
Object discountAggResult = discountAggregation.getAggregationResponseMap(aggMaps);
if (discountAggResult != null) {
filter.put("discount", discountAggResult);
}
filter.put("discount", discountAggResult);
jsonMap.put("data", filter);
} else {
jsonMap.put("data", "");
... ... @@ -111,13 +109,6 @@ public class SearchService {
return jsonMap;
}
public Map<String, Object> makeValueMap(String name, long count) {
Map<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put("name", name);
valueMap.put("count", count);
return valueMap;
}
/**
* 制作过滤条件报文
* */
... ... @@ -133,37 +124,6 @@ public class SearchService {
return searchCommonService.doMultiGet(indexName, set, null);
}
public List<Map<String, Object>> getFilterResponseMapForBrand(MultiBucketsAggregation aggregation, String indexName, String[] ids) throws Exception {
Set<String> set = new LinkedHashSet<String>();
Iterator<? extends Bucket> itSizeAgg = aggregation.getBuckets().iterator();
while (itSizeAgg.hasNext()) {
Bucket ltSize = itSizeAgg.next();
for (String sizeId : ltSize.getKey().split(",")) {
set.add(sizeId);
}
}
for (String id : ids) {
set.add(id);
}
return searchCommonService.doMultiGet(indexName, set, null);
}
/**
* 获取按最近上架时间聚合的结果
*
* @param aggregation
* @return
*/
public Map<String, Object> getRecentShelveDayResponseMap(MultiBucketsAggregation aggregation) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
while (itAgg.hasNext()) {
Bucket lt = itAgg.next();
map.put(lt.getKey(), lt.getDocCount());
}
return map;
}
/**
* 获取按上架日期聚合的结果[日期列表]
*
... ... @@ -181,9 +141,8 @@ public class SearchService {
searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, null));
// 构造聚合参数
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
list.add(AggregationBuilders.terms("recentShelveDay").field("shelveDay").size(7).order(Terms.Order.term(false)));
searchParam.setAggregationBuilders(list);
IAggregation recentShelveDayAggregation = aggregationFactoryService.getRecentShelveDayAggregation();
searchParam.setAggregationBuilders(Arrays.asList(recentShelveDayAggregation.getBuilder()));
searchParam.setSearchType(SearchType.COUNT);
// 进行检索
... ... @@ -199,12 +158,12 @@ public class SearchService {
jsonMap.put("data", "");
return jsonMap;
}
if (searchResult.getAggMaps() != null) {
// 获取聚合结果
Object recentShelveDayResponse = recentShelveDayAggregation.getAggregationResponseMap(searchResult.getAggMaps());
if (recentShelveDayResponse != null) {
Map<String, Object> filter = new HashMap<String, Object>();
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
if (aggMaps.containsKey("recentShelveDay")) {
filter.put("recent", this.getRecentShelveDayResponseMap((MultiBucketsAggregation) aggMaps.get("recentShelveDay")));
}
filter.put("recent", recentShelveDayResponse);
jsonMap.put("data", filter);
} else {
jsonMap.put("data", "");
... ... @@ -648,7 +607,7 @@ public class SearchService {
}
return brandAlifMap;
}
/**
* 根据query关键字获取商品总数
*
... ...