Authored by zhaojun2

Merge branch 'zj_productanalysis' into 0710

... ... @@ -41,10 +41,4 @@ public class RecommendOnProductAnalysisController {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return analysisService.recommendNewArrivals(paramMap);
}
}
... ...
... ... @@ -11,12 +11,14 @@ import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.aggregations.impls.AggregationFactory;
import com.yoho.search.service.helper.SearchParamHelper;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
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;
... ... @@ -41,7 +43,7 @@ public class AggregationsService extends AbstractCacheComponent<JSONObject>{
return searchCommonService.doSearch(indexName, searchParamClone);
}
private JSONObject getAggNameAndResponse(IAggregation aggregation, SearchParam searchParam, String indexName) throws Exception {
public JSONObject getAggNameAndResponse(IAggregation aggregation, SearchParam searchParam, String indexName) throws Exception {
SearchResult searchResult = this.doSearchForAggregation(searchParam, indexName);
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
JSONObject result = aggregation.getAggNameAndResponse(aggMaps);
... ... @@ -212,6 +214,20 @@ public class AggregationsService extends AbstractCacheComponent<JSONObject>{
}
/**
* 获取店铺聚合结果。
*/
public JSONObject getShopAggregationResultWithOrderByShopCreateTime(Map<String, String> paramMap, int aggCount) throws Exception {
IAggregation shopAggregation = aggregationFactory.getShopAggregation(aggCount);
SearchParam searchParam = searchParamHelper.buildWithFilterParam(paramMap, null);
AbstractAggregationBuilder<?> abstractAggregationBuilder = ((TermsAggregationBuilder)shopAggregation.getBuilder())
.order(Terms.Order.aggregation("maxShopCreateTime", false))
.subAggregation(AggregationBuilders.max("maxShopCreateTime").field("shopCreateTime"));
searchParam.setAggregationBuilders(Arrays.asList(abstractAggregationBuilder));
searchParam.setOffset(0);
return this.getAggNameAndResponseWithCache(shopAggregation, searchParam);
}
/**
* 获取专区名称面的聚合结果
*/
public JSONObject getPromotionAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
... ...
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.terms.Terms;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ProductAnalysisDateAggregation extends AbstractAggregation {
public ProductAnalysisDateAggregation() {
}
@Override
public String aggName() {
return "maxDateAgg";
}
@Override
public String filterName() {
return null;
}
@Override
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field("dayDate").size(2).order(Terms.Order.term(false));
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
List<String> dateList = new ArrayList<>();
Iterator<? extends MultiBucketsAggregation.Bucket> itSizeAgg = aggregation.getBuckets().iterator();
while (itSizeAgg.hasNext()) {
MultiBucketsAggregation.Bucket ltSize = itSizeAgg.next();
String[] dateArray=ltSize.getKeyAsString().split(",");
for (String date : dateArray) {
dateList.add(date);
}
}
return dateList;
}
}
... ...
... ... @@ -3,6 +3,9 @@ package com.yoho.search.service.scene.productanalysis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.yoho.search.base.helper.RnnVectorCalculator;
import com.yoho.search.base.helper.Word2VectorCalculator;
import com.yoho.search.base.utils.*;
... ... @@ -16,6 +19,7 @@ import com.yoho.search.models.SortGroup;
import com.yoho.search.models.aggregations.AggKeyCount;
import com.yoho.search.models.aggregations.AggKeyCountTwoLevel;
import com.yoho.search.service.aggregations.AggregationsService;
import com.yoho.search.service.aggregations.impls.ProductAnalysisDateAggregation;
import com.yoho.search.service.aggregations.impls.ProductAnalysisShopAggregation;
import com.yoho.search.service.aggregations.impls.ProductAnalysisSortAggregation;
import com.yoho.search.service.aggregations.impls.ShopBrandAggregation;
... ... @@ -26,6 +30,7 @@ import com.yoho.search.service.scene.activity.recommendshop.ActivityShopBrand;
import com.yoho.search.service.scene.general.SortGroupService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
... ... @@ -36,6 +41,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service
... ... @@ -52,6 +58,42 @@ public class RecommendOnProductAnalysisService {
private BrandVectorCacheBean brandVectorCacheBean;
@Autowired
private SortGroupService sortGroupService;
//Guava Cache
private LoadingCache<String, List<String>> dateRange = CacheBuilder.newBuilder()
.maximumSize(30000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<String, List<String>>() {
public List<String> load(String key) {
return getDateRange();
}
});
private List<String> getDateRange() {
IAggregation aggregation = new ProductAnalysisDateAggregation();
SearchParam searchParam = new SearchParam();
searchParam.setQuery(QueryBuilders.matchAllQuery());
searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
try {
JSONObject aggResult = aggregationsService.getAggNameAndResponse(aggregation, searchParam, ISearchConstants.INDEX_NAME_YOHO_PRODUCT_ANALYSIS);
if (aggResult != null && !CollectionUtils.isEmpty(aggResult.getJSONArray(aggregation.aggName()))) {
List<String> dateList = JSON.parseArray(JSON.toJSONString(aggResult.get(aggregation.aggName())), String.class);
if (dateList.size() != 2 || StringUtils.isBlank(dateList.get(0)) || StringUtils.isBlank(dateList.get(1))) {
return Collections.emptyList();
}
return dateList;
}
}catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return Collections.emptyList();
}
private boolean isAfterSixOclock() {
Date sixOclock = DateUtils.truncate(DateUtils.setHours(new Date(), 6), Calendar.HOUR);
return new Date().compareTo(sixOclock) > 0;
}
public SearchApiResult recommendSort(Map<String, String> paramMap) {
... ... @@ -108,12 +150,13 @@ public class RecommendOnProductAnalysisService {
List<Integer> shopIds = new ArrayList<>();
try {
addDefaultParamsToParamMap(paramMapClone);
JSONObject aggResult = aggregationsService.getShopAggregationResult(paramMapClone, 8);
JSONObject aggResult = aggregationsService.getShopAggregationResultWithOrderByShopCreateTime(paramMapClone, 20);
if (aggResult != null && !CollectionUtils.isEmpty(aggResult.getJSONArray("shopAgg"))) {
JSONArray shopList = aggResult.getJSONArray("shopAgg");
for (Object shop : shopList) {
shopIds.add(((JSONObject)shop).getInteger("shop_id"));
}
shopIds = SearchCollectionUtils.safeSubList(shopIds, 0, 8);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
... ... @@ -200,7 +243,7 @@ public class RecommendOnProductAnalysisService {
JSONObject jsonObject = aggregationsService.getAggNameAndResponseWithCache(aggregation, searchParam, ISearchConstants.INDEX_NAME_YOHO_PRODUCT_ANALYSIS);
List<Integer> aggShopIds = new ArrayList<>();
if (jsonObject != null && jsonObject.get(aggregation.aggName()) != null) {
aggShopIds = JSON.parseArray(jsonObject.toJSONString(jsonObject.get(aggregation.aggName())), Integer.class);
aggShopIds = JSON.parseArray(JSON.toJSONString(jsonObject.get(aggregation.aggName())), Integer.class);
}
return aggShopIds;
}
... ... @@ -213,7 +256,7 @@ public class RecommendOnProductAnalysisService {
JSONObject jsonObject = aggregationsService.getAggNameAndResponseWithCache(aggregation, searchParam);
List<AggKeyCountTwoLevel> aggKeyCountTwoLevels = new ArrayList<>();
if (jsonObject != null && jsonObject.get(aggregation.aggName()) != null) {
aggKeyCountTwoLevels = JSON.parseArray(jsonObject.toJSONString(jsonObject.get(aggregation.aggName())), AggKeyCountTwoLevel.class);
aggKeyCountTwoLevels = JSON.parseArray(JSON.toJSONString(jsonObject.get(aggregation.aggName())), AggKeyCountTwoLevel.class);
}
return aggKeyCountTwoLevels;
}
... ... @@ -359,7 +402,7 @@ public class RecommendOnProductAnalysisService {
JSONObject jsonObject = aggregationsService.getAggNameAndResponseWithCache(aggregation, searchParam, ISearchConstants.INDEX_NAME_YOHO_PRODUCT_ANALYSIS);
List<Integer> aggSortIds = new ArrayList<>();
if (jsonObject != null && jsonObject.get(aggregation.aggName()) != null) {
aggSortIds = JSON.parseArray(jsonObject.toJSONString(jsonObject.get(aggregation.aggName())), Integer.class);
aggSortIds = JSON.parseArray(JSON.toJSONString(jsonObject.get(aggregation.aggName())), Integer.class);
}
return aggSortIds;
}
... ... @@ -405,6 +448,18 @@ public class RecommendOnProductAnalysisService {
addMustIntTermsQuery(boolFilter, paramMap, SearchRequestParams.PARAM_SEARCH_GENDER, ProductIndexEsField.gender);
addMustIntTermsQuery(boolFilter, paramMap, SearchRequestParams.PARAM_SEARCH_YH_CHANNEL, "yhChannel");
addMustDoubleRangeQuery(boolFilter, paramMap, SearchRequestParams.PARAM_SEARCH_SHELVETIME, ProductIndexEsField.shelveTime);
try {
List<String> day_date = dateRange.get("DAY_DATE");
if (!CollectionUtils.isEmpty(day_date)) {
if (isAfterSixOclock()) {
boolFilter.must(QueryBuilders.rangeQuery("dayDate").gte(day_date.get(0)).lte(day_date.get(0)));
} else {
boolFilter.must(QueryBuilders.rangeQuery("dayDate").gte(day_date.get(1)).lte(day_date.get(1)));
}
}
}catch (Exception e) {
}
SearchParam searchParam = new SearchParam();
searchParam.setFiter(boolFilter);
searchParam.setQuery(queryBuilder);
... ...