|
|
package com.yoho.search.service.scene.activity;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.cache.CacheTimeConstants;
|
|
|
import com.yoho.search.cache.beans.AbstractPageComponent;
|
|
|
import com.yoho.search.cache.model.CacheObject;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheComponent;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
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 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.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class AggBrandService extends AbstractPageComponent<List<AggBrand>> {
|
|
|
public class AggBrandService extends AbstractCacheComponent<List<AggBrand>> {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
...
|
...
|
@@ -41,39 +29,15 @@ public class AggBrandService extends AbstractPageComponent<List<AggBrand>> { |
|
|
private SearchParamHelper searchParamHelper;
|
|
|
|
|
|
public List<AggBrand> aggBrands(ParamQueryFilter paramQueryFilter) {
|
|
|
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
|
|
|
List<AggBrand> value = queryFromCache(redisKeyBuilder);
|
|
|
if (CollectionUtils.isEmpty(value)) {
|
|
|
value = getFromEs(searchParamHelper.buildSearchParam(paramQueryFilter));
|
|
|
if (!CollectionUtils.isEmpty(value)) {
|
|
|
addToRedisAndEhcache(redisKeyBuilder, value);
|
|
|
}
|
|
|
List<AggBrand> value;
|
|
|
try {
|
|
|
value = queryWithCache(paramQueryFilter, null);
|
|
|
} catch (Exception e) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
return value != null ? value : Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("BRAND_PRODUCT_LIST:").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.CACHE_10_MINUTE;
|
|
|
}
|
|
|
|
|
|
private List<AggBrand> getFromEs(SearchParam searchParam) {
|
|
|
//2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(500);
|
|
|
aggregationBuilders.add(firstAggregationBuilder);
|
|
|
searchParam.setAggregationBuilders(aggregationBuilders);
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
|
|
|
return this.genAggBrand(aggregationMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
private List<AggBrand> genAggBrand(Map<String, Aggregation> aggMaps) {
|
|
|
MultiBucketsAggregation aggregation = (MultiBucketsAggregation)aggMaps.get("brandAgg");
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> itSizeAgg = aggregation.getBuckets().iterator();
|
...
|
...
|
@@ -88,13 +52,27 @@ public class AggBrandService extends AbstractPageComponent<List<AggBrand>> { |
|
|
return aggBrands;
|
|
|
}
|
|
|
|
|
|
private void addToRedisAndEhcache(RedisKeyBuilder redisKeyBuilder, List<AggBrand> result) {
|
|
|
addJavaObjectToEhcache(redisKeyBuilder, result);
|
|
|
addToRedis(redisKeyBuilder, JSON.toJSONString(result, SerializerFeature.WriteMapNullValue));
|
|
|
@Override
|
|
|
protected List<AggBrand> doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) {
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(500);
|
|
|
aggregationBuilders.add(firstAggregationBuilder);
|
|
|
|
|
|
SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
|
|
|
searchParam.setAggregationBuilders(aggregationBuilders);
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
|
|
|
return this.genAggBrand(aggregationMap);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("BRAND_PRODUCT_LIST_NEW:").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected List<AggBrand> doRealQuery(ParamQueryFilter paramQueryFilter) {
|
|
|
return null;
|
|
|
protected int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.CACHE_10_MINUTE;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|