|
|
package com.yoho.search.service.recall.beans.persional;
|
|
|
|
|
|
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.beans.AbstractCacheComponent;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.personalized.models.SortBrand;
|
|
|
import com.yoho.search.service.recall.beans.helper.ExtendFilterHelper;
|
|
|
import com.yoho.search.cache.CacheTimeConstants;
|
|
|
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
|
|
|
import com.yoho.search.service.recall.models.personal.PagePersonalFactor;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
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 CachePersionalFactorComponent extends AbstractCacheComponent<PagePersonalFactor> {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
/**
|
|
|
* 查询个性化因子
|
|
|
*
|
|
|
* @param paramQueryFilter
|
|
|
* @return
|
|
|
*/
|
|
|
public PagePersonalFactor queryPagePersionalFactor(ParamQueryFilter paramQueryFilter) {
|
|
|
Object value = super.queryWithCache(paramQueryFilter);
|
|
|
return value == null ? null : (PagePersonalFactor) value;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("PAGE_FACTORS_NEW:").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.PAGE_PERSIONAL_FACTOR;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected PagePersonalFactor doRealQuery(ParamQueryFilter paramQueryFilter) {
|
|
|
//1、构造参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(paramQueryFilter.getParamQuery());
|
|
|
|
|
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
|
|
filter.must(paramQueryFilter.getParamFilter());
|
|
|
filter.mustNot(ExtendFilterHelper.notRecallFilter());//聚合的时候带上不召回的数据
|
|
|
searchParam.setFiter(filter);
|
|
|
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
//2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
aggregationBuilders.add(brandSortAggBuilder());//品类-品牌聚合
|
|
|
searchParam.setAggregationBuilders(aggregationBuilders);
|
|
|
|
|
|
//3、执行查询
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
|
|
|
//4、构造结果
|
|
|
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
|
|
|
List<SortBrand> sortBrands = this.getBrandSortsFromAggregationMap(aggregationMap);
|
|
|
List<Integer> misortIds = this.getMisortIds(sortBrands);
|
|
|
List<Integer> brandIds = this.getBrandIds(sortBrands);
|
|
|
return new PagePersonalFactor(misortIds, brandIds, sortBrands);
|
|
|
}
|
|
|
|
|
|
private List<Integer> getMisortIds(List<SortBrand> sortBrands) {
|
|
|
if (sortBrands == null || sortBrands.isEmpty()) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<Integer> results = new ArrayList<>();
|
|
|
for (SortBrand sortBrand : sortBrands) {
|
|
|
Integer misort = sortBrand.getMisort();
|
|
|
if (!results.contains(misort)) {
|
|
|
results.add(misort);
|
|
|
}
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<Integer> getBrandIds(List<SortBrand> sortBrands) {
|
|
|
if (sortBrands == null || sortBrands.isEmpty()) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<Integer> results = new ArrayList<>();
|
|
|
for (SortBrand sortBrand : sortBrands) {
|
|
|
Integer brandId = sortBrand.getBrandId();
|
|
|
if (!results.contains(brandId)) {
|
|
|
results.add(brandId);
|
|
|
}
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 品类+品牌聚合
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
private TermsAggregationBuilder brandSortAggBuilder() {
|
|
|
TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortBrandBrandIdAgg").field(ProductIndexEsField.brandId).size(1000);
|
|
|
middleSortAggBuilder.subAggregation(AggregationBuilders.terms("sortBrandMiddleSortAgg").field(ProductIndexEsField.middleSortId).size(200));
|
|
|
return middleSortAggBuilder;
|
|
|
}
|
|
|
|
|
|
private List<SortBrand> getBrandSortsFromAggregationMap(Map<String, Aggregation> aggregationMap) {
|
|
|
Map<Integer, List<Integer>> brand2MiSortIdsMap = this.getValueFromAggregationMap(aggregationMap, "sortBrandBrandIdAgg", "sortBrandMiddleSortAgg");
|
|
|
List<SortBrand> pageBrandSorts = new ArrayList<>();
|
|
|
for (Map.Entry<Integer, List<Integer>> entry : brand2MiSortIdsMap.entrySet()) {
|
|
|
Integer brandId = entry.getKey();
|
|
|
List<Integer> misorts = entry.getValue();
|
|
|
for (Integer misort : misorts) {
|
|
|
pageBrandSorts.add(new SortBrand(misort, brandId));
|
|
|
}
|
|
|
}
|
|
|
return pageBrandSorts;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从聚合结果中获取参数,仅支持二层聚合
|
|
|
*
|
|
|
* @param aggregationMap
|
|
|
* @param firstAggName
|
|
|
* @param secondAggName
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<Integer, List<Integer>> getValueFromAggregationMap(Map<String, Aggregation> aggregationMap, String firstAggName, String secondAggName) {
|
|
|
Map<Integer, List<Integer>> aggResultMap = new HashMap<>();
|
|
|
if (!aggregationMap.containsKey(firstAggName)) {
|
|
|
return aggResultMap;
|
|
|
}
|
|
|
MultiBucketsAggregation firstAggregation = (MultiBucketsAggregation) aggregationMap.get(firstAggName);
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> firstAggregationIterator = firstAggregation.getBuckets().iterator();
|
|
|
while (firstAggregationIterator.hasNext()) {
|
|
|
MultiBucketsAggregation.Bucket firstAggregationBucket = firstAggregationIterator.next();
|
|
|
Integer firstAggregationBucketKey = Integer.valueOf(firstAggregationBucket.getKeyAsString());
|
|
|
Map<String, Aggregation> secondAggregationMap = firstAggregationBucket.getAggregations().asMap();
|
|
|
if (secondAggregationMap == null || !secondAggregationMap.containsKey(secondAggName)) {
|
|
|
continue;
|
|
|
}
|
|
|
List<Integer> secondAggregationBucketKeys = this.getAggValuesFromMultiBucketsAggregation((MultiBucketsAggregation) secondAggregationMap.get(secondAggName));
|
|
|
aggResultMap.put(firstAggregationBucketKey, secondAggregationBucketKeys);
|
|
|
}
|
|
|
return aggResultMap;
|
|
|
}
|
|
|
|
|
|
private List<Integer> getAggValuesFromMultiBucketsAggregation(MultiBucketsAggregation aggregation) {
|
|
|
List<Integer> results = new ArrayList<>();
|
|
|
if (aggregation == null) {
|
|
|
return results;
|
|
|
}
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> bucketsIterator = aggregation.getBuckets().iterator();
|
|
|
while (bucketsIterator.hasNext()) {
|
|
|
MultiBucketsAggregation.Bucket bucket = bucketsIterator.next();
|
|
|
Integer value = Integer.valueOf(bucket.getKeyAsString());
|
|
|
results.add(value);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|