Authored by 胡古飞

二手-瑕疵筛选项

... ... @@ -22,6 +22,7 @@ public class UfoSearchRequestParams {
public static final String UFO_PARAM_COLOR = "color";
public static final String UFO_PARAM_POOL = "pool";
public static final String UFO_PARAM_SHOW_CHANNEL = "showChannel";
public static final String UFO_PARAM_PRICE= "price";
// ufo发售日历 商品类型
public static final String UFO_PARAM_SEARCH_SALE_CALENDAR_PRODUCT_TYPE = "product_type";
... ...
... ... @@ -29,4 +29,12 @@ public class UfoSecondhandController {
return ufoSecondhandSkupService.secondHandSkupList(paramMap);
}
@RequestMapping(method = RequestMethod.GET, value = "/ufo/secondHand/aggregation")
public SearchApiResult secondHandAggregation(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
int maxStoragePriceId = ufoMaxStoragePriceIdService.queryUfoMaxStoragePriceId();
paramMap.put(UfoSearchRequestParams.UFO_PARAM_MAXSTORAGEPRICE_ID, maxStoragePriceId + "");
return ufoSecondhandSkupService.secondHandAggregation(paramMap);
}
}
... ...
... ... @@ -170,12 +170,20 @@ public class AggregationFactory {
return new UfoSortAggregation(ufoProductSortIndexBaseService, paramMap);
}
public IAggregation getUfoSizeAggregation() {
return new UfoSizeAggregation(ufoSizeIndexBaseService);
public IAggregation getUfoSizeAggregation(String sizeFieldName) {
return new UfoSizeAggregation(ufoSizeIndexBaseService,sizeFieldName);
}
public IAggregation getUfoGenderAggregation() {
return new UfoGenderAggregation();
}
public IAggregation getUfoPreSaleFlagAggregation() {
return new UfoPreSaleFlagAggregation();
}
public IAggregation getUfoPriceAggregation() {
return new UfoPriceAggregation();
}
}
... ...
... ... @@ -45,7 +45,7 @@ public class UfoBrandAggregation extends AbstractAggregation {
@Override
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(UfoProductIndexEsField.brandId).size(brandCount);
return AggregationBuilders.terms(aggName()).field("brandId").size(brandCount);
}
@Override
... ...
... ... @@ -30,7 +30,7 @@ public class UfoGenderAggregation extends AbstractAggregation {
@Override
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(UfoProductIndexEsField.gender).size(5);
return AggregationBuilders.terms(aggName()).field("gender").size(5);
}
@Override
... ...
package com.yoho.search.service.aggregations.impls;
import com.yoho.search.core.es.agg.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 java.util.*;
/**
* @author wangnan
* @version 2018/9/19
*/
public class UfoPreSaleFlagAggregation extends AbstractAggregation {
@Override
public String aggName() {
return "ufoPreSaleFlagAgg";
}
@Override
public String filterName() {
return "preSaleFlag";
}
@Override
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field("preSaleFlag").size(5);
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
List<Map<String, Object>> resultList = new ArrayList<>();
Iterator<? extends Bucket> it = aggregation.getBuckets().iterator();
while (it.hasNext()) {
Bucket ltgender = it.next();
String id = ltgender.getKeyAsString();
Map<String, Object> result = new HashMap<>();
result.put("value", id);
if ("5".equals(id)) {
result.put("key", "全新瑕疵");
} else if ("6".equals(id)) {
result.put("key", "二手");
} else {
result.put("key", "其他");
}
resultList.add(result);
}
return resultList;
}
}
... ...
package com.yoho.search.service.aggregations.impls;
import com.yoho.search.core.es.agg.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.range.RangeAggregationBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UfoPriceAggregation extends AbstractAggregation {
public UfoPriceAggregation() {
super();
}
@Override
public String aggName() {
return "priceAgg";
}
@Override
public String filterName() {
return "priceRange";
}
@Override
public AbstractAggregationBuilder<?> getBuilder() {
RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range(aggName()).field("price");
rangeAggregationBuilder.addRange("0-500",0,500);
rangeAggregationBuilder.addRange("500-2000",500,2000);
rangeAggregationBuilder.addRange("2000-4000",2000,4000);
rangeAggregationBuilder.addRange("4000-6000",4000,6000);
rangeAggregationBuilder.addUnboundedFrom("6000以上",6000);
return rangeAggregationBuilder;
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
List<? extends MultiBucketsAggregation.Bucket> buckets = aggregation.getBuckets();
List<Map<String,Object>> priceList = new ArrayList<>();
for(MultiBucketsAggregation.Bucket bucket:buckets){
if(bucket.getDocCount()<=0){
continue;
}
Map<String,Object> result = new HashMap<>();
result.put("key",bucket.getKeyAsString());
result.put("value",bucket.getKeyAsString().replace("-",","));
result.put("count",bucket.getDocCount());
priceList.add(result);
}
return priceList;
}
}
... ...
package com.yoho.search.service.aggregations.impls;
import com.yoho.search.base.constants.UfoProductIndexEsField;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.index.ufo.UfoSizeIndexBaseService;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
... ... @@ -13,16 +12,14 @@ import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* @author wangnan
* @version 2018/9/17
*/
public class UfoSizeAggregation extends AbstractAggregation {
private String sizeFieldName;
private UfoSizeIndexBaseService sizeIndexBaseService;
UfoSizeAggregation(UfoSizeIndexBaseService sizeIndexBaseService) {
UfoSizeAggregation(UfoSizeIndexBaseService sizeIndexBaseService,String sizeFieldName) {
this.sizeIndexBaseService = sizeIndexBaseService;
this.sizeFieldName = sizeFieldName;
}
@Override
... ... @@ -37,7 +34,7 @@ public class UfoSizeAggregation extends AbstractAggregation {
@Override
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(UfoProductIndexEsField.sizeIds).size(300);
return AggregationBuilders.terms(aggName()).field(sizeFieldName).size(300);
}
@Override
... ...
... ... @@ -123,6 +123,14 @@ public class UfoSearchQueryHelper extends BaseService {
boolFilter.must(QueryBuilders.termsQuery(esField, values));
}
public void addMustDoubleRangeQuery(BoolQueryBuilder boolFilter, Map<String, String> paramMap, String paramName, String esField) {
List<Double> values = SearchConvertUtils.stringToDoubleList(paramMap.get(paramName), ",");
if (values == null || values.isEmpty() || values.size() != 2) {
return;
}
boolFilter.must(QueryBuilders.rangeQuery(esField).gte(values.get(0)).lte(values.get(1)));
}
public void addMustNotIntTermsQuery(BoolQueryBuilder boolFilter, Map<String, String> paramMap, String paramName, String esField) {
List<Integer> values = SearchConvertUtils.stringToIntList(paramMap.get(paramName), ",");
if (values == null || values.isEmpty()) {
... ... @@ -196,7 +204,7 @@ public class UfoSearchQueryHelper extends BaseService {
}
// 3、处理价格排序
if (order.equalsIgnoreCase("price:asc") || order.equalsIgnoreCase("price:desc")|| order.equalsIgnoreCase("availableNowPrice:desc")|| order.equalsIgnoreCase("availableNowPrice:asc")) {
if (order.equalsIgnoreCase("price:asc") || order.equalsIgnoreCase("price:desc") || order.equalsIgnoreCase("availableNowPrice:desc") || order.equalsIgnoreCase("availableNowPrice:asc")) {
sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.storage).order(SortOrder.DESC));
}
... ...
package com.yoho.search.service.index.ufo;
import com.yoho.search.base.constants.UfoStoragePriceIndexEsField;
import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
... ... @@ -12,8 +13,6 @@ import java.util.Map;
@Component
public class UfoStoragePriceIndexBaseService {
private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknIndexBaseService.class);
/**
* 构造返回结果
*/
... ... @@ -21,27 +20,31 @@ public class UfoStoragePriceIndexBaseService {
Map<String, Object> skupResult = new HashMap<>();
//基本信息
skupResult.put("skup", MapUtils.getIntValue(map, "skup", 0));
skupResult.put("product_id", MapUtils.getIntValue(map, "productId", 0));
skupResult.put("pre_sale_flag", MapUtils.getIntValue(map, "preSaleFlag", 0));
skupResult.put("storage_id", MapUtils.getIntValue(map, "storageId", 0));
//skupResult.put("goods_id", MapUtils.getIntValue(map, "goodsId", 0));
//skupResult.put("seller_uid", MapUtils.getIntValue(map, "sellerUid", 0));
//skupResult.put("status", MapUtils.getIntValue(map, "status", 0));
//skupResult.put("is_hide", MapUtils.getIntValue(map, "isHide", 0));
//skupResult.put("region", MapUtils.getIntValue(map, "region", 0));
skupResult.put("skup", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.skup, 0));
skupResult.put("product_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.productId, 0));
skupResult.put("pre_sale_flag", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.preSaleFlag, 0));
skupResult.put("storage_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.storageId, 0));
//价格
skupResult.put("skup_price", MapUtils.getDouble(map, "price", 0d));
skupResult.put("skup_price", MapUtils.getDouble(map, UfoStoragePriceIndexEsField.price, 0d));
//图片
String secondhand_image = MapUtils.getString(map, "secondhandImage", "");
String secondhand_image = MapUtils.getString(map, UfoStoragePriceIndexEsField.secondhandImage, "");
skupResult.put("secondhand_image", secondhand_image == null ? "" : secondhand_image);
//尺码
skupResult.put("size_id", MapUtils.getIntValue(map, "sizeId", 0));
skupResult.put("size_name", MapUtils.getString(map, "sizeName", ""));
skupResult.put("size_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.sizeId, 0));
skupResult.put("size_name", MapUtils.getString(map, UfoStoragePriceIndexEsField.sizeName, ""));
//品牌
skupResult.put("brand_id", MapUtils.getString(map, UfoStoragePriceIndexEsField.brandId, ""));
//其他
// skupResult.put("status", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.status, 0));
// skupResult.put("is_hide", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.isHide, 0));
// skupResult.put("goods_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.goodsId, 0));
// skupResult.put("seller_uid", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.sellerUid, 0));
// skupResult.put("region", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.region, 0));
return skupResult;
}
}
... ...
package com.yoho.search.service.scene.ufo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.base.constants.ISearchConstants;
import com.yoho.search.base.constants.UfoProductIndexEsField;
import com.yoho.search.base.constants.UfoStoragePriceIndexEsField;
import com.yoho.search.base.utils.SearchConvertUtils;
import com.yoho.search.cache.CacheInMinute;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.UfoSearchRequestParams;
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;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.aggregations.impls.AggregationFactory;
import com.yoho.search.service.helper.UfoSearchQueryHelper;
import com.yoho.search.service.index.ufo.UfoProductIndexBaseService;
import com.yoho.search.service.index.ufo.UfoStoragePriceIndexBaseService;
... ... @@ -20,6 +24,9 @@ import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
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.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
... ... @@ -27,6 +34,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
/**
... ... @@ -45,6 +53,76 @@ public class UfoSecondhandSkupService {
private UfoProductIndexBaseService ufoProductIndexBaseService;
@Autowired
private UfoSearchQueryHelper ufoSearchQueryHelper;
@Autowired
private AggregationFactory aggregationFactory;
private Set<String> orderValues = new HashSet<>();
@PostConstruct
private void init() {
//默认
orderValues.add("id:desc");
//价格
orderValues.add("price:asc");
orderValues.add("price:desc");
}
// 构造排序参数
private List<SortBuilder<?>> buildSortBuilders(Map<String, String> paramMap) {
String order = paramMap.get("order");
if (StringUtils.isBlank(order) || !orderValues.contains(order)) {
return Arrays.asList(SortBuilders.fieldSort("id").order(SortOrder.DESC));
}
String[] sortParts = order.split(ISearchConstants.SPLIT_CHAR_COLON);
String fieldName = sortParts[0];
SortOrder sortOrder = SortOrder.ASC.toString().equals(sortParts[1]) ? SortOrder.ASC : SortOrder.DESC;
return Arrays.asList(SortBuilders.fieldSort(fieldName).order(sortOrder), SortBuilders.fieldSort("id").order(SortOrder.DESC));
}
// 构造过滤参数
private BoolQueryBuilder builderFilter(Map<String, String> paramMap) {
BoolQueryBuilder filter = QueryBuilders.boolQuery();
// 硬过滤
filter.must(QueryBuilders.termQuery(UfoStoragePriceIndexEsField.isHide, 0));
filter.must(QueryBuilders.termQuery(UfoStoragePriceIndexEsField.status, 1));
// 防止翻页重复,过滤最大id
int maxStoragePriceId = MapUtils.getIntValue(paramMap, UfoSearchRequestParams.UFO_PARAM_MAXSTORAGEPRICE_ID, Integer.MAX_VALUE);
filter.must(QueryBuilders.rangeQuery(UfoStoragePriceIndexEsField.id).lte(maxStoragePriceId));
// 支持过滤类型,默认5,6
List<Integer> preSaleFlags = SearchConvertUtils.stringToIntList(MapUtils.getString(paramMap, UfoSearchRequestParams.UFO_PARAM_PRE_SALE_FLAG), ",");
if (CollectionUtils.isNotEmpty(preSaleFlags)) {
filter.must(QueryBuilders.termsQuery(UfoStoragePriceIndexEsField.preSaleFlag, preSaleFlags));
} else {
filter.must(QueryBuilders.termsQuery(UfoStoragePriceIndexEsField.preSaleFlag, Arrays.asList(5, 6)));
}
// 支持过滤showChannel
ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_SHOW_CHANNEL, UfoStoragePriceIndexEsField.showChannel);
// 支持过滤性别
ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_GENDER, UfoStoragePriceIndexEsField.gender);
// 支持过滤尺码
ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_SIZE, UfoStoragePriceIndexEsField.sizeId);
// 支持过滤品牌
ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_BRAND, UfoStoragePriceIndexEsField.brandId);
// 支持商品池参数
ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_POOL, UfoStoragePriceIndexEsField.poolIds);
// 支持过滤价格
ufoSearchQueryHelper.addMustDoubleRangeQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_PRICE, UfoStoragePriceIndexEsField.price);
return filter;
}
private SearchParam buildSearchParam(Map<String, String> paramMap, int page, int pageSize) {
SearchParam searchParam = new SearchParam();
// 1、构建Filter
searchParam.setFiter(this.builderFilter(paramMap));
// 2、设置排序字段
searchParam.setSortBuilders(this.buildSortBuilders(paramMap));
// 3、构建分页参数
searchParam.setSize(pageSize);
searchParam.setOffset((page - 1) * pageSize);
return searchParam;
}
@SearchCacheAble(cacheName = "SECONDEHAND_SKUP_LIST", cacheInMinute = CacheInMinute.Minute_UFO)
public SearchApiResult secondHandSkupList(Map<String, String> paramMap) {
... ... @@ -57,37 +135,32 @@ public class UfoSecondhandSkupService {
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
}
// 2、构造SearchParam
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = this.builderFilter(paramMap);
searchParam.setFiter(boolFilter);
searchParam.setQuery(QueryBuilders.matchAllQuery());
// 3、构建分页参数
searchParam.setSize(pageSize);
searchParam.setOffset((page - 1) * pageSize);
// 4、设置排序字段
searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort("id").order(SortOrder.DESC)));
// 5、执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_STORAGE_PRICE, searchParam);
// 6、构造搜索结果
SearchParam searchParams = this.buildSearchParam(paramMap, page, pageSize);
// 3、执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_STORAGE_PRICE, searchParams);
// 4、构造搜索结果
List<Map<String, Object>> skupResultList = new ArrayList<>();
List<Integer> ufoProductIds = new ArrayList<>();
List<Integer> ufoStorageIds = new ArrayList<>();
for (Map<String, Object> skupEsSource : searchResult.getResultList()) {
skupResultList.add(ufoStoragePriceIndexBaseService.buildStoragePriceMap(skupEsSource));
ufoProductIds.add(MapUtils.getIntValue(skupEsSource, "productId", 0));
ufoStorageIds.add(MapUtils.getIntValue(skupEsSource, "storageId", 0));
ufoProductIds.add(MapUtils.getIntValue(skupEsSource, UfoStoragePriceIndexEsField.productId, 0));
ufoStorageIds.add(MapUtils.getIntValue(skupEsSource, UfoStoragePriceIndexEsField.storageId, 0));
}
// 7、根据ufoStorageIds查询最低价信息
// 5、根据ufoStorageIds查询最低价信息
Map<Integer, Double> storageAvailableNowPriceMap = this.queryAvailableNowPriceByStorageIds(ufoStorageIds);
// 8、根据productIds查询商品信息
// 6、根据productIds查询商品信息
Map<Integer, Map<String, Object>> ufoProductMap = ufoProductIndexBaseService.queryUfoProductMapByProductIds(ufoProductIds);
// 9、处理结果
for(Map<String, Object> skupResult:skupResultList){
for (Map<String, Object> skupResult : skupResultList) {
//1) 价格处理[现货最低价-skupPrice]
double skupPrice = MapUtils.getDoubleValue(skupResult, "skup_price", 0);
int storageId = MapUtils.getIntValue(skupResult,"storage_id",0);
int storageId = MapUtils.getIntValue(skupResult, "storage_id", 0);
double available_now_price = MapUtils.getDoubleValue(storageAvailableNowPriceMap, storageId, 0);
skupResult.put("available_now_price", available_now_price);
if (skupPrice > 0 && available_now_price > 0 && skupPrice < available_now_price) {
... ... @@ -123,24 +196,6 @@ public class UfoSecondhandSkupService {
}
}
private BoolQueryBuilder builderFilter(Map<String, String> paramMap) {
BoolQueryBuilder filter = QueryBuilders.boolQuery();
// 硬过滤
int maxStoragePriceId = MapUtils.getIntValue(paramMap, UfoSearchRequestParams.UFO_PARAM_MAXSTORAGEPRICE_ID, Integer.MAX_VALUE);
filter.must(QueryBuilders.rangeQuery("id").lte(maxStoragePriceId));
filter.must(QueryBuilders.termQuery("isHide", 0));
filter.must(QueryBuilders.termQuery("status", 1));
// 过滤preSaleFlag,默认5,6
List<Integer> preSaleFlags = SearchConvertUtils.stringToIntList(MapUtils.getString(paramMap, UfoSearchRequestParams.UFO_PARAM_PRE_SALE_FLAG), ",");
if (CollectionUtils.isNotEmpty(preSaleFlags)) {
filter.must(QueryBuilders.termsQuery("preSaleFlag", preSaleFlags));
} else {
filter.must(QueryBuilders.termsQuery("preSaleFlag", Arrays.asList(5, 6)));
}
// 支持过滤showChannel
ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_SHOW_CHANNEL, UfoProductIndexEsField.showChannel);
return filter;
}
//查询每个storage对应的现货最低价
private Map<Integer, Double> queryAvailableNowPriceByStorageIds(final Collection<?> ufoStorageIds) {
... ... @@ -162,4 +217,91 @@ public class UfoSecondhandSkupService {
}
}
//二手列表筛选项
@SearchCacheAble(cacheName = "SECONDEHAND_AGGREGATION", cacheInMinute = CacheInMinute.Minute_UFO)
public SearchApiResult secondHandAggregation(Map<String, String> paramMap) {
SearchParam searchParam = new SearchParam();
// 1、构建Filter
searchParam.setFiter(this.builderFilter(paramMap));
searchParam.setSize(0);
//2、构建聚合参数
searchParam.setAggregationBuilders(this.buildAggs(paramMap));
// 3、查询
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_STORAGE_PRICE, searchParam);
// 4、构造结果
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
if (aggMaps == null) {
return null;
}
// 构造返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("filter", this.getAggResultMap(paramMap, aggMaps));
return new SearchApiResult().setData(dataMap);
}
private List<AbstractAggregationBuilder<?>> buildAggs(Map<String, String> paramMap) {
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
//品牌
list.add(aggregationFactory.getUfoBrandAggregation(paramMap).getBuilder());
//性别
list.add(aggregationFactory.getUfoGenderAggregation().getBuilder());
//尺码
list.add(aggregationFactory.getUfoSizeAggregation(UfoStoragePriceIndexEsField.sizeId).getBuilder());
//二手类型
list.add(aggregationFactory.getUfoPreSaleFlagAggregation().getBuilder());
//价格
list.add(aggregationFactory.getUfoPriceAggregation().getBuilder());
return list;
}
private Map<String, Object> getAggResultMap(Map<String, String> paramMap, Map<String, Aggregation> aggMaps) {
Map<String, Object> filter = new HashMap<String, Object>();
// 品牌层面的聚合结果
IAggregation brandAggregation = aggregationFactory.getUfoBrandAggregation(paramMap);
Object brandResponse = brandAggregation.getAggregationResponseMap(aggMaps);
if (brandResponse != null) {
filter.put("brand", brandResponse);
} else {
filter.put("brand", new JSONArray());
}
// 性别的聚合结果
IAggregation genderAggregation = aggregationFactory.getUfoGenderAggregation();
Object genderResponse = genderAggregation.getAggregationResponseMap(aggMaps);
if (genderResponse != null) {
filter.put("gender", genderResponse);
} else {
filter.put("gender", new JSONArray());
}
// 尺码
IAggregation sizeAggregation = aggregationFactory.getUfoSizeAggregation(UfoProductIndexEsField.sizeIds);
Object sizeResponse = sizeAggregation.getAggregationResponseMap(aggMaps);
if (sizeResponse != null) {
filter.put("size", sizeResponse);
} else {
filter.put("size", new JSONArray());
}
//二手类型
IAggregation preSaleFlagAggregation = aggregationFactory.getUfoPreSaleFlagAggregation();
Object preSaleFlagResponse = preSaleFlagAggregation.getAggregationResponseMap(aggMaps);
if (preSaleFlagResponse != null) {
filter.put("preSaleFlag", preSaleFlagResponse);
} else {
filter.put("preSaleFlag", new JSONArray());
}
//价格
IAggregation priceAggregation = aggregationFactory.getUfoPriceAggregation();
Object priceResponse = priceAggregation.getAggregationResponseMap(aggMaps);
if (priceResponse != null) {
filter.put("price", priceResponse);
} else {
filter.put("price", new JSONArray());
}
return filter;
}
}
... ...
... ... @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.base.constants.ISearchConstants;
import com.yoho.search.base.constants.UfoProductIndexEsField;
import com.yoho.search.cache.CacheInMinute;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.UfoSearchRequestParams;
... ... @@ -106,7 +107,7 @@ public class UfoSelectionListService {
//性别
list.add(aggregationFactory.getUfoGenderAggregation().getBuilder());
//尺码
list.add(aggregationFactory.getUfoSizeAggregation().getBuilder());
list.add(aggregationFactory.getUfoSizeAggregation(UfoProductIndexEsField.sizeIds).getBuilder());
return list;
}
... ... @@ -141,14 +142,13 @@ public class UfoSelectionListService {
}
// 尺码
IAggregation sizeAggregation = aggregationFactory.getUfoSizeAggregation();
IAggregation sizeAggregation = aggregationFactory.getUfoSizeAggregation(UfoProductIndexEsField.sizeIds);
Object sizeResponse = sizeAggregation.getAggregationResponseMap(aggMaps);
if (sizeResponse != null) {
filter.put("size", sizeResponse);
} else {
filter.put("size", new JSONArray());
}
return filter;
}
... ...