|
|
package com.yoho.search.service.scene.activity;
|
|
|
|
|
|
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.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.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.TermsAggregationBuilder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class AggBrandService extends AbstractCacheComponent<List<AggBrand>> {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
|
|
|
public List<AggBrand> aggBrands(ParamQueryFilter paramQueryFilter) {
|
|
|
List<AggBrand> value;
|
|
|
try {
|
|
|
value = queryWithCache(paramQueryFilter, null);
|
|
|
} catch (Exception e) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
return value != null ? value : Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
private List<AggBrand> genAggBrand(Map<String, Aggregation> aggMaps) {
|
|
|
MultiBucketsAggregation aggregation = (MultiBucketsAggregation)aggMaps.get("brandAgg");
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> itSizeAgg = aggregation.getBuckets().iterator();
|
|
|
List<AggBrand> aggBrands = new ArrayList<>();
|
|
|
while (itSizeAgg.hasNext()) {
|
|
|
AggBrand aggBrand = new AggBrand();
|
|
|
MultiBucketsAggregation.Bucket ltSize = itSizeAgg.next();
|
|
|
aggBrand.setBrandId(Integer.valueOf(ltSize.getKeyAsString()));
|
|
|
aggBrand.setCount(Long.valueOf(ltSize.getDocCount()).intValue());
|
|
|
aggBrands.add(aggBrand);
|
|
|
}
|
|
|
return aggBrands;
|
|
|
}
|
|
|
|
|
|
@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 int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.CACHE_10_MINUTE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String cacheSceneKey() {
|
|
|
return "BRAND_PRODUCT_LIST_NEW";
|
|
|
}
|
|
|
|
|
|
} |