Authored by henry

修改聚合的数量

... ... @@ -2,6 +2,7 @@ package com.yoho.search.aggregations.impls;
import java.util.Map;
import com.yoho.search.common.AggInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -14,6 +15,52 @@ public class AggregationFactoryService {
@Autowired
private SearchCommonService searchCommonService;
public IAggregation getSubAggregationByType(int type,Map<String, String> paramMap){
IAggregation iAggregation = null;
switch (type){
case AggInterface.AGE_LEVEL:
iAggregation= new AgeLevelAggregation();
break;
case AggInterface.DISCOUNT:
iAggregation= new DiscountAggregation();
break;
case AggInterface.GENDER:
iAggregation= new GenderAggregation(paramMap);
break;
case AggInterface.PRICE:
iAggregation= new PriceAggregation();
break;
case AggInterface.COLOR:
iAggregation= new ColorAggregation(searchCommonService, paramMap);
break;
case AggInterface.STYLE:
iAggregation= new StyleAggregation(searchCommonService, paramMap);
break;
case AggInterface.BRAND:
iAggregation= new BrandAggregation(searchCommonService, paramMap);
break;
case AggInterface.SIZE:
iAggregation= new SizeAggregation(searchCommonService);
break;
case AggInterface.STANDARD:
iAggregation= new StandardAggregation(searchCommonService, paramMap);
break;
case AggInterface.RECENT_SHELVE_DAY:
iAggregation= new RecentShelveDayAggregation();
break;
case AggInterface.IS_NEW:
iAggregation= new IsNewAggregation();
break;
case AggInterface.IS_LIMITED:
iAggregation= new IsLimitedAggregation();
break;
case AggInterface.IS_SPECIAL:
iAggregation= new IsSecialofferAggregation();
break;
}
return iAggregation;
}
public IAggregation getAgeLevelAggregation() {
return new AgeLevelAggregation();
}
... ...
... ... @@ -33,7 +33,7 @@ public class BrandAggregation extends AbstractAggregation{
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("brandId").size(1000);
return AggregationBuilders.terms(aggName()).field("brandId").size(500);
}
@Override
... ...
package com.yoho.search.aggregations.impls;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import com.yoho.search.aggregations.AbstractAggregation;
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 com.yoho.search.aggregations.AbstractAggregation;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 折扣聚合对象
*
* @author hugufei
*
* @author hugufei
*/
public class DiscountAggregation extends AbstractAggregation {
DiscountAggregation(){
super();
}
@Override
public String aggName() {
return "discountAgg";
}
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.range(aggName()).field("promotionDiscount").addRange(0.0, 0.399).addRange(0.4, 0.699).addRange(0.7, 0.999);
}
@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();
if (lt.getDocCount() > 0) {
String key = lt.getKey();
if ("0.0-0.399".equals(key)) {
map.put("0.01,0.399", makeValueMap("1~3", lt.getDocCount()));
} else if ("0.4-0.699".equals(key)) {
map.put("0.4,0.699", makeValueMap("4~6", lt.getDocCount()));
} else {
map.put("0.7,0.999", makeValueMap("7~9", lt.getDocCount()));
}
}
}
return map;
}
DiscountAggregation() {
super();
}
@Override
public String aggName() {
return "discountAgg";
}
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.range(aggName()).field("promotionDiscount").addRange(0.0, 0.399).addRange(0.4, 0.699).addRange(0.7, 0.999);
}
@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();
if (lt.getDocCount() > 0) {
String key = lt.getKey();
if ("0.0-0.399".equals(key)) {
map.put("0.01,0.399", makeValueMap("1~3", lt.getDocCount()));
} else if ("0.4-0.699".equals(key)) {
map.put("0.4,0.699", makeValueMap("4~6", lt.getDocCount()));
} else {
map.put("0.7,0.999", makeValueMap("7~9", lt.getDocCount()));
}
}
}
return map;
}
private 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;
}
private 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;
}
}
... ...
... ... @@ -28,7 +28,7 @@ public class GenderAggregation extends AbstractAggregation {
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("gender").size(10);
return AggregationBuilders.terms(aggName()).field("gender").size(5);
}
@Override
... ...
... ... @@ -38,7 +38,7 @@ public class StandardAggregation extends AbstractAggregation {
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("standardNames").size(10000);
return AggregationBuilders.terms(aggName()).field("standardNames").size(5000);
}
@Override
... ...
package com.yoho.search.common;
/**
* Created by xy on 2016/7/1.
*/
public class AggInterface {
public static final int AGE_LEVEL =1;
public static final int DISCOUNT =2;
public static final int GENDER = 3;
public static final int PRICE= 4;
public static final int COLOR= 5;
public static final int STYLE=6;
public static final int BRAND=7;
public static final int SIZE=8;
public static final int STANDARD=9;
public static final int RECENT_SHELVE_DAY =10;
public static final int IS_NEW = 11;
public static final int IS_LIMITED =12;
public static final int IS_SPECIAL =13;
}
... ...
... ... @@ -146,14 +146,14 @@ public class SearchProductsServiceNew {
// 5)风格
Object styleAgg = aggregationService.getStyleAggregationResult(searchParam, paramMap, true);
if (colorAgg != null) {
if (styleAgg != null) {
aggregationResult.put("styleAgg", styleAgg);
}
// 6)品牌
Object brandAgg = aggregationService.getBrandAggregationResult(searchParam, paramMap, true);
if (colorAgg != null) {
//aggregationResult.put("brandAgg", brandAgg);
if (brandAgg != null) {
aggregationResult.put("brandAgg", brandAgg);
}
// 7)标准
... ...
... ... @@ -13,6 +13,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.yoho.search.common.AggInterface;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolFilterBuilder;
... ... @@ -78,7 +79,7 @@ public class SearchService {
searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, null));
// 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
IAggregation discountAggregation = aggregationFactoryService.getDiscountAggregation();
IAggregation discountAggregation = aggregationFactoryService.getSubAggregationByType(AggInterface.DISCOUNT,null);
searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));
searchParam.setSearchType(SearchType.COUNT);
... ...