Authored by wangnan

promotion聚合的各种地方改成zqname聚合

... ... @@ -126,8 +126,8 @@ public class AggregationFactoryService {
return new IsGlobalAggregation();
}
public IAggregation getPromotionAggregation(int aggCount) {
return new PromotionAggregation(zqNameIndexBaseService, aggCount);
public IAggregation getZqNameAggregation(int aggCount) {
return new ZqNameAggregation(zqNameIndexBaseService, aggCount);
}
public IAggregation getKeywordAggregation(Map<String, String> paramMap, int aggCount) {
... ...
... ... @@ -14,11 +14,11 @@ import java.util.stream.Collectors;
/**
* Created by wangnan on 2017/5/11.
*/
public class PromotionAggregation extends AbstractSingleFieldAggregation {
public class ZqNameAggregation extends AbstractSingleFieldAggregation {
private ZqNameIndexBaseService zqNameIndexBaseService;
public PromotionAggregation(ZqNameIndexBaseService zqNameIndexBaseService, int count) {
public ZqNameAggregation(ZqNameIndexBaseService zqNameIndexBaseService, int count) {
super(count);
this.zqNameIndexBaseService = zqNameIndexBaseService;
}
... ... @@ -30,36 +30,31 @@ public class PromotionAggregation extends AbstractSingleFieldAggregation {
@Override
public String aggName() {
return "promotionAgg";
return "zqNameAgg";
}
@Override
public String filterName() {
return "promotion";
return "zqName";
}
@SuppressWarnings("unchecked")
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
List<String> promotionIdList = (List<String>) super.getAggregationResponseMap(aggMaps);
if (CollectionUtils.isNotEmpty(promotionIdList)) {
promotionIdList = promotionIdList.stream().collect(Collectors.toList());
}
if (CollectionUtils.isEmpty(promotionIdList)) {
List<String> zqIdList = (List<String>) super.getAggregationResponseMap(aggMaps);
if (CollectionUtils.isEmpty(zqIdList)) {
return new ArrayList<>();
}
// 调用zqname索引查询title
Map<String, String> promotionParam = new HashMap<>();
promotionParam.put("ids", promotionIdList.stream().collect(Collectors.joining(",")));
List<Map<String, Object>> promotionList = zqNameIndexBaseService.queryZQNameListByParam(promotionParam);
// 调用zqname索引查询zqtitle
String ids = zqIdList.stream().collect(Collectors.joining(","));
List<Map<String, Object>> zqNameList = zqNameIndexBaseService.queryZQNameListByIds(ids);
// promotion_title去重,is_promotion用逗号连接
Map<String, Map<String, Object>> titleMap = new HashMap<>();
for (Map<String, Object> map : promotionList) {
for (Map<String, Object> map : zqNameList) {
String title = map.get("promotion_title").toString();
if (titleMap.containsKey(title)) {
String newKey = map.get("is_promotion").toString();
String oldKey = titleMap.get(title).get("is_promotion").toString();
Map<String, Object> newMap = new HashMap<String, Object>();
Map<String, Object> newMap = new HashMap<>();
newMap.put("promotion_title", title);
newMap.put("is_promotion", newKey + "," + oldKey);
titleMap.put(title, newMap);
... ... @@ -67,11 +62,11 @@ public class PromotionAggregation extends AbstractSingleFieldAggregation {
titleMap.put(title, map);
}
}
promotionList = new ArrayList<>();
zqNameList = new ArrayList<>();
for (Map.Entry<String, Map<String, Object>> entry : titleMap.entrySet()) {
promotionList.add(entry.getValue());
zqNameList.add(entry.getValue());
}
return promotionList == null || promotionList.isEmpty() ? new ArrayList<>() : promotionList;
return zqNameList == null || zqNameList.isEmpty() ? new ArrayList<>() : zqNameList;
}
}
... ...
... ... @@ -28,22 +28,25 @@ import java.util.Map;
@Service
public class ZqNameIndexBaseService {
@Autowired
private SearchCommonService searchCommonService;
private static final Logger logger = LoggerFactory.getLogger(PromotionIndexBaseService.class);
private static final String INDEX_NAME = ISearchConstants.INDEX_NAME_ZQ_NAME;
public List<Map<String, Object>> queryZQNameListByParam(Map<String, String> paramMap) {
@Autowired
private SearchCommonService searchCommonService;
public List<Map<String, Object>> queryZQNameListByIds(String ids) {
try {
// 1、构建SearchParam
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilterForShops = this.constructFilterBuilder(paramMap);
searchParam.setFiter(boolFilterForShops);
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
if (StringUtils.isNotBlank(ids)) {
int[] idArray = ConvertUtils.stringToIntArray(ids, ",");
boolFilter.must(QueryBuilders.termsQuery("id", idArray));
}
searchParam.setFiter(boolFilter);
searchParam.setSize(10000);
searchParam.setQuery(QueryBuilders.matchAllQuery());
// 2、调搜索,并将结果加入缓存
List<Map<String, Object>> promotionsList = this.queryZQNameListByParam(searchParam);
return promotionsList;
List<Map<String, Object>> ZqNameList = this.queryZQNameListByParam(searchParam);
return ZqNameList;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<>();
... ... @@ -62,15 +65,6 @@ public class ZqNameIndexBaseService {
return results;
}
private BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
if (paramMap.containsKey("ids") && StringUtils.isNotBlank(paramMap.get("ids"))) {
int[] ids = ConvertUtils.stringToIntArray(paramMap.get("ids"), ",");
boolFilter.must(QueryBuilders.termsQuery("id", ids));
}
return boolFilter;
}
private Map<String, Object> getMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<>();
int id = MapUtils.getIntValue(esMap, "id", 0);
... ...
package com.yoho.search.service.scene.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
... ... @@ -29,6 +13,21 @@ import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.service.IAggRecommendService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Service
public class SceneSelectionsService extends AbstractCacheAbleService {
... ... @@ -68,8 +67,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService {
public List<IAggregation> getCommonAggregations(Map<String, String> paramMap) {
List<IAggregation> aggregations = new ArrayList<>();
// 1)促销
aggregations.add(aggregationFactoryService.getPromotionAggregation(1000));
// 1)专区名称
aggregations.add(aggregationFactoryService.getZqNameAggregation(1000));
// 2)全球购
aggregations.add(aggregationFactoryService.getIsGlobalAggregation());
// 3)品牌
... ... @@ -99,8 +98,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService {
*/
public List<IAggregation> getBreakSizeAggregations(Map<String, String> paramMap) {
List<IAggregation> aggregations = new ArrayList<>();
// 1)促销
aggregations.add(aggregationFactoryService.getPromotionAggregation(1000));
// 1)专区名称
aggregations.add(aggregationFactoryService.getZqNameAggregation(1000));
// 2)全球购
aggregations.add(aggregationFactoryService.getIsGlobalAggregation());
// 3)品牌
... ...
package com.yoho.search.service.service;
import java.util.Arrays;
import java.util.Map;
import org.elasticsearch.search.aggregations.Aggregation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
... ... @@ -25,6 +14,16 @@ import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
import org.elasticsearch.search.aggregations.Aggregation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Map;
@Service
public class AggregationService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
... ... @@ -232,14 +231,14 @@ public class AggregationService extends AbstractCacheAbleService implements Appl
}
/**
* 获取促销层面的聚合结果
* 获取专区名称面的聚合结果
*/
public JSONObject getPromotionAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
IAggregation promotionAggregation = aggregationFactoryService.getPromotionAggregation(aggCount);
IAggregation zqNameAggregation = aggregationFactoryService.getZqNameAggregation(aggCount);
String filterParamName = null;
Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5"));
SearchParam searchParam = this.genSearchParamForAgg(promotionAggregation, paramMap, filterParamName, paramNum);
return this.getAggNameAndResponseWithCache(promotionAggregation, searchParam);
SearchParam searchParam = this.genSearchParamForAgg(zqNameAggregation, paramMap, filterParamName, paramNum);
return this.getAggNameAndResponseWithCache(zqNameAggregation, searchParam);
}
/**
... ...
package com.yoho.search.service.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
... ... @@ -34,6 +19,15 @@ import com.yoho.search.service.helper.SearchAfterCacheHelper;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
import com.yoho.search.service.service.ISelectionsForApp;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.Map.Entry;
@Service
public class SelectionForAppImpl extends AbstractCacheAbleService implements ISelectionsForApp {
... ... @@ -137,8 +131,8 @@ public class SelectionForAppImpl extends AbstractCacheAbleService implements ISe
// 12)全球购
list.add(aggregationFactoryService.getIsGlobalAggregation().getBuilder());
// 13)促销
list.add(aggregationFactoryService.getPromotionAggregation(1000).getBuilder());
// 13)专区名称
list.add(aggregationFactoryService.getZqNameAggregation(1000).getBuilder());
return list;
}
... ... @@ -205,9 +199,9 @@ public class SelectionForAppImpl extends AbstractCacheAbleService implements ISe
filter.put("size", new JSONArray());
}
// 8)获取促销聚合结果[促销不会提前聚合]
IAggregation promotionAggregation = aggregationFactoryService.getPromotionAggregation(1000);
Object promotionResponse = promotionAggregation.getAggregationResponseMap(aggMaps);
// 8)获取专区名称聚合结果[促销不会提前聚合]
IAggregation zqNameAggregation = aggregationFactoryService.getZqNameAggregation(1000);
Object promotionResponse = zqNameAggregation.getAggregationResponseMap(aggMaps);
if (promotionResponse != null) {
filter.put("promotion", promotionResponse);
} else {
... ...