Authored by 胡古飞

二手-瑕疵筛选项

@@ -22,6 +22,7 @@ public class UfoSearchRequestParams { @@ -22,6 +22,7 @@ public class UfoSearchRequestParams {
22 public static final String UFO_PARAM_COLOR = "color"; 22 public static final String UFO_PARAM_COLOR = "color";
23 public static final String UFO_PARAM_POOL = "pool"; 23 public static final String UFO_PARAM_POOL = "pool";
24 public static final String UFO_PARAM_SHOW_CHANNEL = "showChannel"; 24 public static final String UFO_PARAM_SHOW_CHANNEL = "showChannel";
  25 + public static final String UFO_PARAM_PRICE= "price";
25 26
26 // ufo发售日历 商品类型 27 // ufo发售日历 商品类型
27 public static final String UFO_PARAM_SEARCH_SALE_CALENDAR_PRODUCT_TYPE = "product_type"; 28 public static final String UFO_PARAM_SEARCH_SALE_CALENDAR_PRODUCT_TYPE = "product_type";
@@ -29,4 +29,12 @@ public class UfoSecondhandController { @@ -29,4 +29,12 @@ public class UfoSecondhandController {
29 return ufoSecondhandSkupService.secondHandSkupList(paramMap); 29 return ufoSecondhandSkupService.secondHandSkupList(paramMap);
30 } 30 }
31 31
  32 + @RequestMapping(method = RequestMethod.GET, value = "/ufo/secondHand/aggregation")
  33 + public SearchApiResult secondHandAggregation(HttpServletRequest request) {
  34 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  35 + int maxStoragePriceId = ufoMaxStoragePriceIdService.queryUfoMaxStoragePriceId();
  36 + paramMap.put(UfoSearchRequestParams.UFO_PARAM_MAXSTORAGEPRICE_ID, maxStoragePriceId + "");
  37 + return ufoSecondhandSkupService.secondHandAggregation(paramMap);
  38 + }
  39 +
32 } 40 }
@@ -170,12 +170,20 @@ public class AggregationFactory { @@ -170,12 +170,20 @@ public class AggregationFactory {
170 return new UfoSortAggregation(ufoProductSortIndexBaseService, paramMap); 170 return new UfoSortAggregation(ufoProductSortIndexBaseService, paramMap);
171 } 171 }
172 172
173 - public IAggregation getUfoSizeAggregation() {  
174 - return new UfoSizeAggregation(ufoSizeIndexBaseService); 173 + public IAggregation getUfoSizeAggregation(String sizeFieldName) {
  174 + return new UfoSizeAggregation(ufoSizeIndexBaseService,sizeFieldName);
175 } 175 }
176 176
177 public IAggregation getUfoGenderAggregation() { 177 public IAggregation getUfoGenderAggregation() {
178 return new UfoGenderAggregation(); 178 return new UfoGenderAggregation();
179 } 179 }
180 180
  181 + public IAggregation getUfoPreSaleFlagAggregation() {
  182 + return new UfoPreSaleFlagAggregation();
  183 + }
  184 +
  185 + public IAggregation getUfoPriceAggregation() {
  186 + return new UfoPriceAggregation();
  187 + }
  188 +
181 } 189 }
@@ -45,7 +45,7 @@ public class UfoBrandAggregation extends AbstractAggregation { @@ -45,7 +45,7 @@ public class UfoBrandAggregation extends AbstractAggregation {
45 45
46 @Override 46 @Override
47 public AbstractAggregationBuilder<?> getBuilder() { 47 public AbstractAggregationBuilder<?> getBuilder() {
48 - return AggregationBuilders.terms(aggName()).field(UfoProductIndexEsField.brandId).size(brandCount); 48 + return AggregationBuilders.terms(aggName()).field("brandId").size(brandCount);
49 } 49 }
50 50
51 @Override 51 @Override
@@ -30,7 +30,7 @@ public class UfoGenderAggregation extends AbstractAggregation { @@ -30,7 +30,7 @@ public class UfoGenderAggregation extends AbstractAggregation {
30 30
31 @Override 31 @Override
32 public AbstractAggregationBuilder<?> getBuilder() { 32 public AbstractAggregationBuilder<?> getBuilder() {
33 - return AggregationBuilders.terms(aggName()).field(UfoProductIndexEsField.gender).size(5); 33 + return AggregationBuilders.terms(aggName()).field("gender").size(5);
34 } 34 }
35 35
36 @Override 36 @Override
  1 +package com.yoho.search.service.aggregations.impls;
  2 +
  3 +import com.yoho.search.core.es.agg.AbstractAggregation;
  4 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  5 +import org.elasticsearch.search.aggregations.Aggregation;
  6 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  7 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  8 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
  9 +
  10 +import java.util.*;
  11 +
  12 +/**
  13 + * @author wangnan
  14 + * @version 2018/9/19
  15 + */
  16 +public class UfoPreSaleFlagAggregation extends AbstractAggregation {
  17 +
  18 + @Override
  19 + public String aggName() {
  20 + return "ufoPreSaleFlagAgg";
  21 + }
  22 +
  23 + @Override
  24 + public String filterName() {
  25 + return "preSaleFlag";
  26 + }
  27 +
  28 + @Override
  29 + public AbstractAggregationBuilder<?> getBuilder() {
  30 + return AggregationBuilders.terms(aggName()).field("preSaleFlag").size(5);
  31 + }
  32 +
  33 + @Override
  34 + public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
  35 + MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
  36 + if (aggregation == null) {
  37 + return null;
  38 + }
  39 + List<Map<String, Object>> resultList = new ArrayList<>();
  40 + Iterator<? extends Bucket> it = aggregation.getBuckets().iterator();
  41 + while (it.hasNext()) {
  42 + Bucket ltgender = it.next();
  43 + String id = ltgender.getKeyAsString();
  44 + Map<String, Object> result = new HashMap<>();
  45 + result.put("value", id);
  46 + if ("5".equals(id)) {
  47 + result.put("key", "全新瑕疵");
  48 + } else if ("6".equals(id)) {
  49 + result.put("key", "二手");
  50 + } else {
  51 + result.put("key", "其他");
  52 + }
  53 + resultList.add(result);
  54 + }
  55 + return resultList;
  56 + }
  57 +}
  1 +package com.yoho.search.service.aggregations.impls;
  2 +
  3 +import com.yoho.search.core.es.agg.AbstractAggregation;
  4 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  5 +import org.elasticsearch.search.aggregations.Aggregation;
  6 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  7 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  8 +import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
  9 +
  10 +import java.util.ArrayList;
  11 +import java.util.HashMap;
  12 +import java.util.List;
  13 +import java.util.Map;
  14 +
  15 +public class UfoPriceAggregation extends AbstractAggregation {
  16 +
  17 + public UfoPriceAggregation() {
  18 + super();
  19 + }
  20 +
  21 + @Override
  22 + public String aggName() {
  23 + return "priceAgg";
  24 + }
  25 +
  26 + @Override
  27 + public String filterName() {
  28 + return "priceRange";
  29 + }
  30 +
  31 + @Override
  32 + public AbstractAggregationBuilder<?> getBuilder() {
  33 + RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range(aggName()).field("price");
  34 + rangeAggregationBuilder.addRange("0-500",0,500);
  35 + rangeAggregationBuilder.addRange("500-2000",500,2000);
  36 + rangeAggregationBuilder.addRange("2000-4000",2000,4000);
  37 + rangeAggregationBuilder.addRange("4000-6000",4000,6000);
  38 + rangeAggregationBuilder.addUnboundedFrom("6000以上",6000);
  39 + return rangeAggregationBuilder;
  40 + }
  41 +
  42 + @Override
  43 + public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
  44 + MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
  45 + if (aggregation == null) {
  46 + return null;
  47 + }
  48 + List<? extends MultiBucketsAggregation.Bucket> buckets = aggregation.getBuckets();
  49 + List<Map<String,Object>> priceList = new ArrayList<>();
  50 + for(MultiBucketsAggregation.Bucket bucket:buckets){
  51 + if(bucket.getDocCount()<=0){
  52 + continue;
  53 + }
  54 + Map<String,Object> result = new HashMap<>();
  55 + result.put("key",bucket.getKeyAsString());
  56 + result.put("value",bucket.getKeyAsString().replace("-",","));
  57 + result.put("count",bucket.getDocCount());
  58 + priceList.add(result);
  59 + }
  60 + return priceList;
  61 + }
  62 +
  63 +
  64 +}
1 package com.yoho.search.service.aggregations.impls; 1 package com.yoho.search.service.aggregations.impls;
2 2
3 -import com.yoho.search.base.constants.UfoProductIndexEsField;  
4 import com.yoho.search.core.es.agg.AbstractAggregation; 3 import com.yoho.search.core.es.agg.AbstractAggregation;
5 import com.yoho.search.service.index.ufo.UfoSizeIndexBaseService; 4 import com.yoho.search.service.index.ufo.UfoSizeIndexBaseService;
6 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; 5 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
@@ -13,16 +12,14 @@ import java.util.LinkedHashSet; @@ -13,16 +12,14 @@ import java.util.LinkedHashSet;
13 import java.util.Map; 12 import java.util.Map;
14 import java.util.Set; 13 import java.util.Set;
15 14
16 -/**  
17 - * @author wangnan  
18 - * @version 2018/9/17  
19 - */  
20 public class UfoSizeAggregation extends AbstractAggregation { 15 public class UfoSizeAggregation extends AbstractAggregation {
21 16
  17 + private String sizeFieldName;
22 private UfoSizeIndexBaseService sizeIndexBaseService; 18 private UfoSizeIndexBaseService sizeIndexBaseService;
23 19
24 - UfoSizeAggregation(UfoSizeIndexBaseService sizeIndexBaseService) { 20 + UfoSizeAggregation(UfoSizeIndexBaseService sizeIndexBaseService,String sizeFieldName) {
25 this.sizeIndexBaseService = sizeIndexBaseService; 21 this.sizeIndexBaseService = sizeIndexBaseService;
  22 + this.sizeFieldName = sizeFieldName;
26 } 23 }
27 24
28 @Override 25 @Override
@@ -37,7 +34,7 @@ public class UfoSizeAggregation extends AbstractAggregation { @@ -37,7 +34,7 @@ public class UfoSizeAggregation extends AbstractAggregation {
37 34
38 @Override 35 @Override
39 public AbstractAggregationBuilder<?> getBuilder() { 36 public AbstractAggregationBuilder<?> getBuilder() {
40 - return AggregationBuilders.terms(aggName()).field(UfoProductIndexEsField.sizeIds).size(300); 37 + return AggregationBuilders.terms(aggName()).field(sizeFieldName).size(300);
41 } 38 }
42 39
43 @Override 40 @Override
@@ -123,6 +123,14 @@ public class UfoSearchQueryHelper extends BaseService { @@ -123,6 +123,14 @@ public class UfoSearchQueryHelper extends BaseService {
123 boolFilter.must(QueryBuilders.termsQuery(esField, values)); 123 boolFilter.must(QueryBuilders.termsQuery(esField, values));
124 } 124 }
125 125
  126 + public void addMustDoubleRangeQuery(BoolQueryBuilder boolFilter, Map<String, String> paramMap, String paramName, String esField) {
  127 + List<Double> values = SearchConvertUtils.stringToDoubleList(paramMap.get(paramName), ",");
  128 + if (values == null || values.isEmpty() || values.size() != 2) {
  129 + return;
  130 + }
  131 + boolFilter.must(QueryBuilders.rangeQuery(esField).gte(values.get(0)).lte(values.get(1)));
  132 + }
  133 +
126 public void addMustNotIntTermsQuery(BoolQueryBuilder boolFilter, Map<String, String> paramMap, String paramName, String esField) { 134 public void addMustNotIntTermsQuery(BoolQueryBuilder boolFilter, Map<String, String> paramMap, String paramName, String esField) {
127 List<Integer> values = SearchConvertUtils.stringToIntList(paramMap.get(paramName), ","); 135 List<Integer> values = SearchConvertUtils.stringToIntList(paramMap.get(paramName), ",");
128 if (values == null || values.isEmpty()) { 136 if (values == null || values.isEmpty()) {
@@ -196,7 +204,7 @@ public class UfoSearchQueryHelper extends BaseService { @@ -196,7 +204,7 @@ public class UfoSearchQueryHelper extends BaseService {
196 } 204 }
197 205
198 // 3、处理价格排序 206 // 3、处理价格排序
199 - if (order.equalsIgnoreCase("price:asc") || order.equalsIgnoreCase("price:desc")|| order.equalsIgnoreCase("availableNowPrice:desc")|| order.equalsIgnoreCase("availableNowPrice:asc")) { 207 + if (order.equalsIgnoreCase("price:asc") || order.equalsIgnoreCase("price:desc") || order.equalsIgnoreCase("availableNowPrice:desc") || order.equalsIgnoreCase("availableNowPrice:asc")) {
200 sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.storage).order(SortOrder.DESC)); 208 sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.storage).order(SortOrder.DESC));
201 } 209 }
202 210
1 package com.yoho.search.service.index.ufo; 1 package com.yoho.search.service.index.ufo;
2 2
  3 +import com.yoho.search.base.constants.UfoStoragePriceIndexEsField;
3 import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService; 4 import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
4 import org.apache.commons.collections.MapUtils; 5 import org.apache.commons.collections.MapUtils;
5 import org.slf4j.Logger; 6 import org.slf4j.Logger;
@@ -12,8 +13,6 @@ import java.util.Map; @@ -12,8 +13,6 @@ import java.util.Map;
12 @Component 13 @Component
13 public class UfoStoragePriceIndexBaseService { 14 public class UfoStoragePriceIndexBaseService {
14 15
15 - private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknIndexBaseService.class);  
16 -  
17 /** 16 /**
18 * 构造返回结果 17 * 构造返回结果
19 */ 18 */
@@ -21,27 +20,31 @@ public class UfoStoragePriceIndexBaseService { @@ -21,27 +20,31 @@ public class UfoStoragePriceIndexBaseService {
21 Map<String, Object> skupResult = new HashMap<>(); 20 Map<String, Object> skupResult = new HashMap<>();
22 21
23 //基本信息 22 //基本信息
24 - skupResult.put("skup", MapUtils.getIntValue(map, "skup", 0));  
25 - skupResult.put("product_id", MapUtils.getIntValue(map, "productId", 0));  
26 - skupResult.put("pre_sale_flag", MapUtils.getIntValue(map, "preSaleFlag", 0));  
27 -  
28 - skupResult.put("storage_id", MapUtils.getIntValue(map, "storageId", 0));  
29 - //skupResult.put("goods_id", MapUtils.getIntValue(map, "goodsId", 0));  
30 - //skupResult.put("seller_uid", MapUtils.getIntValue(map, "sellerUid", 0));  
31 - //skupResult.put("status", MapUtils.getIntValue(map, "status", 0));  
32 - //skupResult.put("is_hide", MapUtils.getIntValue(map, "isHide", 0));  
33 - //skupResult.put("region", MapUtils.getIntValue(map, "region", 0)); 23 + skupResult.put("skup", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.skup, 0));
  24 + skupResult.put("product_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.productId, 0));
  25 + skupResult.put("pre_sale_flag", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.preSaleFlag, 0));
34 26
  27 + skupResult.put("storage_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.storageId, 0));
35 //价格 28 //价格
36 - skupResult.put("skup_price", MapUtils.getDouble(map, "price", 0d)); 29 + skupResult.put("skup_price", MapUtils.getDouble(map, UfoStoragePriceIndexEsField.price, 0d));
37 30
38 //图片 31 //图片
39 - String secondhand_image = MapUtils.getString(map, "secondhandImage", ""); 32 + String secondhand_image = MapUtils.getString(map, UfoStoragePriceIndexEsField.secondhandImage, "");
40 skupResult.put("secondhand_image", secondhand_image == null ? "" : secondhand_image); 33 skupResult.put("secondhand_image", secondhand_image == null ? "" : secondhand_image);
41 34
42 //尺码 35 //尺码
43 - skupResult.put("size_id", MapUtils.getIntValue(map, "sizeId", 0));  
44 - skupResult.put("size_name", MapUtils.getString(map, "sizeName", "")); 36 + skupResult.put("size_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.sizeId, 0));
  37 + skupResult.put("size_name", MapUtils.getString(map, UfoStoragePriceIndexEsField.sizeName, ""));
  38 +
  39 + //品牌
  40 + skupResult.put("brand_id", MapUtils.getString(map, UfoStoragePriceIndexEsField.brandId, ""));
  41 +
  42 + //其他
  43 +// skupResult.put("status", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.status, 0));
  44 +// skupResult.put("is_hide", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.isHide, 0));
  45 +// skupResult.put("goods_id", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.goodsId, 0));
  46 +// skupResult.put("seller_uid", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.sellerUid, 0));
  47 +// skupResult.put("region", MapUtils.getIntValue(map, UfoStoragePriceIndexEsField.region, 0));
45 return skupResult; 48 return skupResult;
46 } 49 }
47 } 50 }
1 package com.yoho.search.service.scene.ufo; 1 package com.yoho.search.service.scene.ufo;
2 2
  3 +import com.alibaba.fastjson.JSONArray;
3 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
4 import com.yoho.search.aop.cache.SearchCacheAble; 5 import com.yoho.search.aop.cache.SearchCacheAble;
5 import com.yoho.search.base.constants.ISearchConstants; 6 import com.yoho.search.base.constants.ISearchConstants;
6 import com.yoho.search.base.constants.UfoProductIndexEsField; 7 import com.yoho.search.base.constants.UfoProductIndexEsField;
  8 +import com.yoho.search.base.constants.UfoStoragePriceIndexEsField;
7 import com.yoho.search.base.utils.SearchConvertUtils; 9 import com.yoho.search.base.utils.SearchConvertUtils;
8 import com.yoho.search.cache.CacheInMinute; 10 import com.yoho.search.cache.CacheInMinute;
9 import com.yoho.search.common.SearchCommonService; 11 import com.yoho.search.common.SearchCommonService;
10 import com.yoho.search.common.UfoSearchRequestParams; 12 import com.yoho.search.common.UfoSearchRequestParams;
11 import com.yoho.search.common.utils.SearchApiResultUtils; 13 import com.yoho.search.common.utils.SearchApiResultUtils;
  14 +import com.yoho.search.core.es.agg.IAggregation;
12 import com.yoho.search.core.es.model.SearchParam; 15 import com.yoho.search.core.es.model.SearchParam;
13 import com.yoho.search.core.es.model.SearchResult; 16 import com.yoho.search.core.es.model.SearchResult;
14 import com.yoho.search.models.SearchApiResult; 17 import com.yoho.search.models.SearchApiResult;
  18 +import com.yoho.search.service.aggregations.impls.AggregationFactory;
15 import com.yoho.search.service.helper.UfoSearchQueryHelper; 19 import com.yoho.search.service.helper.UfoSearchQueryHelper;
16 import com.yoho.search.service.index.ufo.UfoProductIndexBaseService; 20 import com.yoho.search.service.index.ufo.UfoProductIndexBaseService;
17 import com.yoho.search.service.index.ufo.UfoStoragePriceIndexBaseService; 21 import com.yoho.search.service.index.ufo.UfoStoragePriceIndexBaseService;
@@ -20,6 +24,9 @@ import org.apache.commons.collections.MapUtils; @@ -20,6 +24,9 @@ import org.apache.commons.collections.MapUtils;
20 import org.apache.commons.lang3.StringUtils; 24 import org.apache.commons.lang3.StringUtils;
21 import org.elasticsearch.index.query.BoolQueryBuilder; 25 import org.elasticsearch.index.query.BoolQueryBuilder;
22 import org.elasticsearch.index.query.QueryBuilders; 26 import org.elasticsearch.index.query.QueryBuilders;
  27 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  28 +import org.elasticsearch.search.aggregations.Aggregation;
  29 +import org.elasticsearch.search.sort.SortBuilder;
23 import org.elasticsearch.search.sort.SortBuilders; 30 import org.elasticsearch.search.sort.SortBuilders;
24 import org.elasticsearch.search.sort.SortOrder; 31 import org.elasticsearch.search.sort.SortOrder;
25 import org.slf4j.Logger; 32 import org.slf4j.Logger;
@@ -27,6 +34,7 @@ import org.slf4j.LoggerFactory; @@ -27,6 +34,7 @@ import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired; 34 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component; 35 import org.springframework.stereotype.Component;
29 36
  37 +import javax.annotation.PostConstruct;
30 import java.util.*; 38 import java.util.*;
31 39
32 /** 40 /**
@@ -45,6 +53,76 @@ public class UfoSecondhandSkupService { @@ -45,6 +53,76 @@ public class UfoSecondhandSkupService {
45 private UfoProductIndexBaseService ufoProductIndexBaseService; 53 private UfoProductIndexBaseService ufoProductIndexBaseService;
46 @Autowired 54 @Autowired
47 private UfoSearchQueryHelper ufoSearchQueryHelper; 55 private UfoSearchQueryHelper ufoSearchQueryHelper;
  56 + @Autowired
  57 + private AggregationFactory aggregationFactory;
  58 +
  59 + private Set<String> orderValues = new HashSet<>();
  60 +
  61 + @PostConstruct
  62 + private void init() {
  63 + //默认
  64 + orderValues.add("id:desc");
  65 + //价格
  66 + orderValues.add("price:asc");
  67 + orderValues.add("price:desc");
  68 + }
  69 +
  70 + // 构造排序参数
  71 + private List<SortBuilder<?>> buildSortBuilders(Map<String, String> paramMap) {
  72 + String order = paramMap.get("order");
  73 + if (StringUtils.isBlank(order) || !orderValues.contains(order)) {
  74 + return Arrays.asList(SortBuilders.fieldSort("id").order(SortOrder.DESC));
  75 + }
  76 + String[] sortParts = order.split(ISearchConstants.SPLIT_CHAR_COLON);
  77 + String fieldName = sortParts[0];
  78 + SortOrder sortOrder = SortOrder.ASC.toString().equals(sortParts[1]) ? SortOrder.ASC : SortOrder.DESC;
  79 + return Arrays.asList(SortBuilders.fieldSort(fieldName).order(sortOrder), SortBuilders.fieldSort("id").order(SortOrder.DESC));
  80 + }
  81 +
  82 + // 构造过滤参数
  83 + private BoolQueryBuilder builderFilter(Map<String, String> paramMap) {
  84 + BoolQueryBuilder filter = QueryBuilders.boolQuery();
  85 + // 硬过滤
  86 + filter.must(QueryBuilders.termQuery(UfoStoragePriceIndexEsField.isHide, 0));
  87 + filter.must(QueryBuilders.termQuery(UfoStoragePriceIndexEsField.status, 1));
  88 +
  89 + // 防止翻页重复,过滤最大id
  90 + int maxStoragePriceId = MapUtils.getIntValue(paramMap, UfoSearchRequestParams.UFO_PARAM_MAXSTORAGEPRICE_ID, Integer.MAX_VALUE);
  91 + filter.must(QueryBuilders.rangeQuery(UfoStoragePriceIndexEsField.id).lte(maxStoragePriceId));
  92 +
  93 + // 支持过滤类型,默认5,6
  94 + List<Integer> preSaleFlags = SearchConvertUtils.stringToIntList(MapUtils.getString(paramMap, UfoSearchRequestParams.UFO_PARAM_PRE_SALE_FLAG), ",");
  95 + if (CollectionUtils.isNotEmpty(preSaleFlags)) {
  96 + filter.must(QueryBuilders.termsQuery(UfoStoragePriceIndexEsField.preSaleFlag, preSaleFlags));
  97 + } else {
  98 + filter.must(QueryBuilders.termsQuery(UfoStoragePriceIndexEsField.preSaleFlag, Arrays.asList(5, 6)));
  99 + }
  100 + // 支持过滤showChannel
  101 + ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_SHOW_CHANNEL, UfoStoragePriceIndexEsField.showChannel);
  102 + // 支持过滤性别
  103 + ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_GENDER, UfoStoragePriceIndexEsField.gender);
  104 + // 支持过滤尺码
  105 + ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_SIZE, UfoStoragePriceIndexEsField.sizeId);
  106 + // 支持过滤品牌
  107 + ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_BRAND, UfoStoragePriceIndexEsField.brandId);
  108 + // 支持商品池参数
  109 + ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_POOL, UfoStoragePriceIndexEsField.poolIds);
  110 + // 支持过滤价格
  111 + ufoSearchQueryHelper.addMustDoubleRangeQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_PRICE, UfoStoragePriceIndexEsField.price);
  112 + return filter;
  113 + }
  114 +
  115 + private SearchParam buildSearchParam(Map<String, String> paramMap, int page, int pageSize) {
  116 + SearchParam searchParam = new SearchParam();
  117 + // 1、构建Filter
  118 + searchParam.setFiter(this.builderFilter(paramMap));
  119 + // 2、设置排序字段
  120 + searchParam.setSortBuilders(this.buildSortBuilders(paramMap));
  121 + // 3、构建分页参数
  122 + searchParam.setSize(pageSize);
  123 + searchParam.setOffset((page - 1) * pageSize);
  124 + return searchParam;
  125 + }
48 126
49 @SearchCacheAble(cacheName = "SECONDEHAND_SKUP_LIST", cacheInMinute = CacheInMinute.Minute_UFO) 127 @SearchCacheAble(cacheName = "SECONDEHAND_SKUP_LIST", cacheInMinute = CacheInMinute.Minute_UFO)
50 public SearchApiResult secondHandSkupList(Map<String, String> paramMap) { 128 public SearchApiResult secondHandSkupList(Map<String, String> paramMap) {
@@ -57,37 +135,32 @@ public class UfoSecondhandSkupService { @@ -57,37 +135,32 @@ public class UfoSecondhandSkupService {
57 return new SearchApiResult().setCode(400).setMessage("分页参数不合法"); 135 return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
58 } 136 }
59 // 2、构造SearchParam 137 // 2、构造SearchParam
60 - SearchParam searchParam = new SearchParam();  
61 - BoolQueryBuilder boolFilter = this.builderFilter(paramMap);  
62 - searchParam.setFiter(boolFilter);  
63 - searchParam.setQuery(QueryBuilders.matchAllQuery());  
64 - // 3、构建分页参数  
65 - searchParam.setSize(pageSize);  
66 - searchParam.setOffset((page - 1) * pageSize);  
67 - // 4、设置排序字段  
68 - searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort("id").order(SortOrder.DESC)));  
69 - // 5、执行搜索  
70 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_STORAGE_PRICE, searchParam);  
71 - // 6、构造搜索结果 138 + SearchParam searchParams = this.buildSearchParam(paramMap, page, pageSize);
  139 +
  140 + // 3、执行搜索
  141 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_STORAGE_PRICE, searchParams);
  142 +
  143 + // 4、构造搜索结果
72 List<Map<String, Object>> skupResultList = new ArrayList<>(); 144 List<Map<String, Object>> skupResultList = new ArrayList<>();
73 List<Integer> ufoProductIds = new ArrayList<>(); 145 List<Integer> ufoProductIds = new ArrayList<>();
74 List<Integer> ufoStorageIds = new ArrayList<>(); 146 List<Integer> ufoStorageIds = new ArrayList<>();
75 for (Map<String, Object> skupEsSource : searchResult.getResultList()) { 147 for (Map<String, Object> skupEsSource : searchResult.getResultList()) {
76 skupResultList.add(ufoStoragePriceIndexBaseService.buildStoragePriceMap(skupEsSource)); 148 skupResultList.add(ufoStoragePriceIndexBaseService.buildStoragePriceMap(skupEsSource));
77 - ufoProductIds.add(MapUtils.getIntValue(skupEsSource, "productId", 0));  
78 - ufoStorageIds.add(MapUtils.getIntValue(skupEsSource, "storageId", 0)); 149 + ufoProductIds.add(MapUtils.getIntValue(skupEsSource, UfoStoragePriceIndexEsField.productId, 0));
  150 + ufoStorageIds.add(MapUtils.getIntValue(skupEsSource, UfoStoragePriceIndexEsField.storageId, 0));
79 } 151 }
80 - // 7、根据ufoStorageIds查询最低价信息 152 +
  153 + // 5、根据ufoStorageIds查询最低价信息
81 Map<Integer, Double> storageAvailableNowPriceMap = this.queryAvailableNowPriceByStorageIds(ufoStorageIds); 154 Map<Integer, Double> storageAvailableNowPriceMap = this.queryAvailableNowPriceByStorageIds(ufoStorageIds);
82 155
83 - // 8、根据productIds查询商品信息 156 + // 6、根据productIds查询商品信息
84 Map<Integer, Map<String, Object>> ufoProductMap = ufoProductIndexBaseService.queryUfoProductMapByProductIds(ufoProductIds); 157 Map<Integer, Map<String, Object>> ufoProductMap = ufoProductIndexBaseService.queryUfoProductMapByProductIds(ufoProductIds);
85 158
86 // 9、处理结果 159 // 9、处理结果
87 - for(Map<String, Object> skupResult:skupResultList){ 160 + for (Map<String, Object> skupResult : skupResultList) {
88 //1) 价格处理[现货最低价-skupPrice] 161 //1) 价格处理[现货最低价-skupPrice]
89 double skupPrice = MapUtils.getDoubleValue(skupResult, "skup_price", 0); 162 double skupPrice = MapUtils.getDoubleValue(skupResult, "skup_price", 0);
90 - int storageId = MapUtils.getIntValue(skupResult,"storage_id",0); 163 + int storageId = MapUtils.getIntValue(skupResult, "storage_id", 0);
91 double available_now_price = MapUtils.getDoubleValue(storageAvailableNowPriceMap, storageId, 0); 164 double available_now_price = MapUtils.getDoubleValue(storageAvailableNowPriceMap, storageId, 0);
92 skupResult.put("available_now_price", available_now_price); 165 skupResult.put("available_now_price", available_now_price);
93 if (skupPrice > 0 && available_now_price > 0 && skupPrice < available_now_price) { 166 if (skupPrice > 0 && available_now_price > 0 && skupPrice < available_now_price) {
@@ -123,24 +196,6 @@ public class UfoSecondhandSkupService { @@ -123,24 +196,6 @@ public class UfoSecondhandSkupService {
123 } 196 }
124 } 197 }
125 198
126 - private BoolQueryBuilder builderFilter(Map<String, String> paramMap) {  
127 - BoolQueryBuilder filter = QueryBuilders.boolQuery();  
128 - // 硬过滤  
129 - int maxStoragePriceId = MapUtils.getIntValue(paramMap, UfoSearchRequestParams.UFO_PARAM_MAXSTORAGEPRICE_ID, Integer.MAX_VALUE);  
130 - filter.must(QueryBuilders.rangeQuery("id").lte(maxStoragePriceId));  
131 - filter.must(QueryBuilders.termQuery("isHide", 0));  
132 - filter.must(QueryBuilders.termQuery("status", 1));  
133 - // 过滤preSaleFlag,默认5,6  
134 - List<Integer> preSaleFlags = SearchConvertUtils.stringToIntList(MapUtils.getString(paramMap, UfoSearchRequestParams.UFO_PARAM_PRE_SALE_FLAG), ",");  
135 - if (CollectionUtils.isNotEmpty(preSaleFlags)) {  
136 - filter.must(QueryBuilders.termsQuery("preSaleFlag", preSaleFlags));  
137 - } else {  
138 - filter.must(QueryBuilders.termsQuery("preSaleFlag", Arrays.asList(5, 6)));  
139 - }  
140 - // 支持过滤showChannel  
141 - ufoSearchQueryHelper.addMustIntTermsQuery(filter, paramMap, UfoSearchRequestParams.UFO_PARAM_SHOW_CHANNEL, UfoProductIndexEsField.showChannel);  
142 - return filter;  
143 - }  
144 199
145 //查询每个storage对应的现货最低价 200 //查询每个storage对应的现货最低价
146 private Map<Integer, Double> queryAvailableNowPriceByStorageIds(final Collection<?> ufoStorageIds) { 201 private Map<Integer, Double> queryAvailableNowPriceByStorageIds(final Collection<?> ufoStorageIds) {
@@ -162,4 +217,91 @@ public class UfoSecondhandSkupService { @@ -162,4 +217,91 @@ public class UfoSecondhandSkupService {
162 } 217 }
163 } 218 }
164 219
  220 + //二手列表筛选项
  221 + @SearchCacheAble(cacheName = "SECONDEHAND_AGGREGATION", cacheInMinute = CacheInMinute.Minute_UFO)
  222 + public SearchApiResult secondHandAggregation(Map<String, String> paramMap) {
  223 + SearchParam searchParam = new SearchParam();
  224 + // 1、构建Filter
  225 + searchParam.setFiter(this.builderFilter(paramMap));
  226 + searchParam.setSize(0);
  227 + //2、构建聚合参数
  228 + searchParam.setAggregationBuilders(this.buildAggs(paramMap));
  229 + // 3、查询
  230 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_STORAGE_PRICE, searchParam);
  231 + // 4、构造结果
  232 + Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
  233 + if (aggMaps == null) {
  234 + return null;
  235 + }
  236 + // 构造返回结果
  237 + JSONObject dataMap = new JSONObject();
  238 + dataMap.put("filter", this.getAggResultMap(paramMap, aggMaps));
  239 + return new SearchApiResult().setData(dataMap);
  240 + }
  241 +
  242 + private List<AbstractAggregationBuilder<?>> buildAggs(Map<String, String> paramMap) {
  243 + List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
  244 + //品牌
  245 + list.add(aggregationFactory.getUfoBrandAggregation(paramMap).getBuilder());
  246 + //性别
  247 + list.add(aggregationFactory.getUfoGenderAggregation().getBuilder());
  248 + //尺码
  249 + list.add(aggregationFactory.getUfoSizeAggregation(UfoStoragePriceIndexEsField.sizeId).getBuilder());
  250 + //二手类型
  251 + list.add(aggregationFactory.getUfoPreSaleFlagAggregation().getBuilder());
  252 + //价格
  253 + list.add(aggregationFactory.getUfoPriceAggregation().getBuilder());
  254 + return list;
  255 + }
  256 +
  257 + private Map<String, Object> getAggResultMap(Map<String, String> paramMap, Map<String, Aggregation> aggMaps) {
  258 + Map<String, Object> filter = new HashMap<String, Object>();
  259 +
  260 + // 品牌层面的聚合结果
  261 + IAggregation brandAggregation = aggregationFactory.getUfoBrandAggregation(paramMap);
  262 + Object brandResponse = brandAggregation.getAggregationResponseMap(aggMaps);
  263 + if (brandResponse != null) {
  264 + filter.put("brand", brandResponse);
  265 + } else {
  266 + filter.put("brand", new JSONArray());
  267 + }
  268 +
  269 + // 性别的聚合结果
  270 + IAggregation genderAggregation = aggregationFactory.getUfoGenderAggregation();
  271 + Object genderResponse = genderAggregation.getAggregationResponseMap(aggMaps);
  272 + if (genderResponse != null) {
  273 + filter.put("gender", genderResponse);
  274 + } else {
  275 + filter.put("gender", new JSONArray());
  276 + }
  277 +
  278 + // 尺码
  279 + IAggregation sizeAggregation = aggregationFactory.getUfoSizeAggregation(UfoProductIndexEsField.sizeIds);
  280 + Object sizeResponse = sizeAggregation.getAggregationResponseMap(aggMaps);
  281 + if (sizeResponse != null) {
  282 + filter.put("size", sizeResponse);
  283 + } else {
  284 + filter.put("size", new JSONArray());
  285 + }
  286 +
  287 + //二手类型
  288 + IAggregation preSaleFlagAggregation = aggregationFactory.getUfoPreSaleFlagAggregation();
  289 + Object preSaleFlagResponse = preSaleFlagAggregation.getAggregationResponseMap(aggMaps);
  290 + if (preSaleFlagResponse != null) {
  291 + filter.put("preSaleFlag", preSaleFlagResponse);
  292 + } else {
  293 + filter.put("preSaleFlag", new JSONArray());
  294 + }
  295 +
  296 + //价格
  297 + IAggregation priceAggregation = aggregationFactory.getUfoPriceAggregation();
  298 + Object priceResponse = priceAggregation.getAggregationResponseMap(aggMaps);
  299 + if (priceResponse != null) {
  300 + filter.put("price", priceResponse);
  301 + } else {
  302 + filter.put("price", new JSONArray());
  303 + }
  304 + return filter;
  305 + }
  306 +
165 } 307 }
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray; @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
4 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
5 import com.yoho.search.aop.cache.SearchCacheAble; 5 import com.yoho.search.aop.cache.SearchCacheAble;
6 import com.yoho.search.base.constants.ISearchConstants; 6 import com.yoho.search.base.constants.ISearchConstants;
  7 +import com.yoho.search.base.constants.UfoProductIndexEsField;
7 import com.yoho.search.cache.CacheInMinute; 8 import com.yoho.search.cache.CacheInMinute;
8 import com.yoho.search.common.SearchCommonService; 9 import com.yoho.search.common.SearchCommonService;
9 import com.yoho.search.common.UfoSearchRequestParams; 10 import com.yoho.search.common.UfoSearchRequestParams;
@@ -106,7 +107,7 @@ public class UfoSelectionListService { @@ -106,7 +107,7 @@ public class UfoSelectionListService {
106 //性别 107 //性别
107 list.add(aggregationFactory.getUfoGenderAggregation().getBuilder()); 108 list.add(aggregationFactory.getUfoGenderAggregation().getBuilder());
108 //尺码 109 //尺码
109 - list.add(aggregationFactory.getUfoSizeAggregation().getBuilder()); 110 + list.add(aggregationFactory.getUfoSizeAggregation(UfoProductIndexEsField.sizeIds).getBuilder());
110 return list; 111 return list;
111 } 112 }
112 113
@@ -141,14 +142,13 @@ public class UfoSelectionListService { @@ -141,14 +142,13 @@ public class UfoSelectionListService {
141 } 142 }
142 143
143 // 尺码 144 // 尺码
144 - IAggregation sizeAggregation = aggregationFactory.getUfoSizeAggregation(); 145 + IAggregation sizeAggregation = aggregationFactory.getUfoSizeAggregation(UfoProductIndexEsField.sizeIds);
145 Object sizeResponse = sizeAggregation.getAggregationResponseMap(aggMaps); 146 Object sizeResponse = sizeAggregation.getAggregationResponseMap(aggMaps);
146 if (sizeResponse != null) { 147 if (sizeResponse != null) {
147 filter.put("size", sizeResponse); 148 filter.put("size", sizeResponse);
148 } else { 149 } else {
149 filter.put("size", new JSONArray()); 150 filter.put("size", new JSONArray());
150 } 151 }
151 -  
152 return filter; 152 return filter;
153 } 153 }
154 154