...
|
...
|
@@ -6,9 +6,12 @@ import com.yoho.search.common.cache.SearchCacheFactory; |
|
|
import com.yoho.search.common.cache.model.SearchCache;
|
|
|
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.core.personalized.models.SortPriceArea;
|
|
|
import com.yoho.search.recall.scene.models.ParamQueryFilter;
|
|
|
import com.yoho.search.recall.scene.models.PagePersonalFactor;
|
|
|
import com.yoho.search.recall.scene.models.personal.PagePersonalFactor;
|
|
|
import com.yoho.search.recall.scene.models.personal.PageSortBrand;
|
|
|
import com.yoho.search.recall.scene.models.personal.PageSortPriceArea;
|
|
|
import com.yoho.search.service.base.SearchCacheService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
...
|
...
|
@@ -20,10 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
class PagePersionalFactorComponent {
|
...
|
...
|
@@ -49,21 +49,17 @@ class PagePersionalFactorComponent { |
|
|
* @return
|
|
|
*/
|
|
|
public PagePersonalFactor queryPagePersionalFactor(ParamQueryFilter paramQueryFilter) {
|
|
|
//0、构造参数
|
|
|
//1、构造参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(paramQueryFilter.getParamQuery());
|
|
|
searchParam.setFiter(paramQueryFilter.getParamFilter());
|
|
|
searchParam.setSize(0);
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
|
|
|
//1、品牌id聚合
|
|
|
aggregationBuilders.add(AggregationBuilders.terms("brandIdAgg").field(ProductIndexEsField.brandId).size(200));//品牌id聚合
|
|
|
|
|
|
//2、品类价格带聚合
|
|
|
TermsAggregationBuilder middleAggBuilder = AggregationBuilders.terms("middleSortIdAgg").field(ProductIndexEsField.middleSortId).size(100);
|
|
|
TermsAggregationBuilder genderAggBuilder = AggregationBuilders.terms("priceAreaAgg").field(ProductIndexEsField.priceArea).size(7);
|
|
|
middleAggBuilder.subAggregation(genderAggBuilder);
|
|
|
aggregationBuilders.add(middleAggBuilder);//品类性格聚合
|
|
|
//2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
aggregationBuilders.add(brandIdAggBuilder());//品牌id聚合
|
|
|
aggregationBuilders.add(sortPriceAreaAggBuilder());//品类-价格带聚合
|
|
|
aggregationBuilders.add(sortBrandAggBuilder());//品类-品牌聚合
|
|
|
searchParam.setAggregationBuilders(aggregationBuilders);
|
|
|
|
|
|
//3、缓存中获取
|
...
|
...
|
@@ -79,15 +75,25 @@ class PagePersionalFactorComponent { |
|
|
//5、构造结果
|
|
|
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
|
|
|
List<Integer> brandIds = this.getBrandIdsFromAggregationMap(aggregationMap);
|
|
|
List<SortPriceArea> sortPriceAreas = this.getSortPriceAreasFromAggregationMap(aggregationMap);
|
|
|
pagePagePersonalFactor = new PagePersonalFactor(brandIds, sortPriceAreas);
|
|
|
List<PageSortPriceArea> sortPriceAreas = this.getSortPriceAreasFromAggregationMap(aggregationMap);
|
|
|
List<PageSortBrand> sortBrands = this.getSortBrandsFromAggregationMap(aggregationMap);
|
|
|
|
|
|
pagePagePersonalFactor = new PagePersonalFactor(brandIds, sortPriceAreas,sortBrands);
|
|
|
//6、加入缓存
|
|
|
searchCacheService.addSerializableObjectToCache(searchCache,cacheKey, pagePagePersonalFactor,true);
|
|
|
return pagePagePersonalFactor;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 品牌id聚合
|
|
|
* @return
|
|
|
*/
|
|
|
private TermsAggregationBuilder brandIdAggBuilder() {
|
|
|
return AggregationBuilders.terms("brandIdAgg").field(ProductIndexEsField.brandId).size(200);
|
|
|
}
|
|
|
|
|
|
private List<Integer> getBrandIdsFromAggregationMap(Map<String, Aggregation> aggregationMap) {
|
|
|
List<Integer> brandIds = new ArrayList<Integer>();
|
|
|
List<Integer> brandIds = new ArrayList<>();
|
|
|
MultiBucketsAggregation aggregation = (MultiBucketsAggregation) aggregationMap.get("brandIdAgg");
|
|
|
if (aggregation == null) {
|
|
|
return brandIds;
|
...
|
...
|
@@ -101,29 +107,89 @@ class PagePersionalFactorComponent { |
|
|
return brandIds;
|
|
|
}
|
|
|
|
|
|
private List<SortPriceArea> getSortPriceAreasFromAggregationMap(Map<String, Aggregation> aggregationMap) {
|
|
|
List<SortPriceArea> sortPrices = new ArrayList<>();
|
|
|
MultiBucketsAggregation aggregation = (MultiBucketsAggregation) aggregationMap.get("middleSortIdAgg");
|
|
|
if (aggregation == null) {
|
|
|
return new ArrayList<>();
|
|
|
/**
|
|
|
* 品类-价格带聚合
|
|
|
* @return
|
|
|
*/
|
|
|
private TermsAggregationBuilder sortPriceAreaAggBuilder() {
|
|
|
TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortPriceMiddleSortIdAgg").field(ProductIndexEsField.middleSortId).size(100);
|
|
|
TermsAggregationBuilder priceAreaAggBuilder = AggregationBuilders.terms("sortPricePriceAreaAgg").field(ProductIndexEsField.priceArea).size(10);
|
|
|
middleSortAggBuilder.subAggregation(priceAreaAggBuilder);
|
|
|
return middleSortAggBuilder;
|
|
|
}
|
|
|
|
|
|
private List<PageSortPriceArea> getSortPriceAreasFromAggregationMap(Map<String, Aggregation> aggregationMap) {
|
|
|
Map<Integer,List<Integer>> sortPriceAreaMap = this.getValueFromAggregationMap(aggregationMap,"sortPriceMiddleSortIdAgg","sortPricePriceAreaAgg");
|
|
|
List<PageSortPriceArea> sortPrices = new ArrayList<>();
|
|
|
for (Map.Entry<Integer,List<Integer>> entry: sortPriceAreaMap.entrySet()) {
|
|
|
Integer middleSortId = entry.getKey();
|
|
|
List<Integer> priceAreas = entry.getValue();
|
|
|
sortPrices.add(new PageSortPriceArea(middleSortId,priceAreas));
|
|
|
}
|
|
|
return sortPrices;
|
|
|
}
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> iterator = aggregation.getBuckets().iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
MultiBucketsAggregation.Bucket middleSortIdBucket = iterator.next();
|
|
|
Integer middleSortId = Integer.valueOf(middleSortIdBucket.getKeyAsString());
|
|
|
|
|
|
MultiBucketsAggregation priceAreaAggregation = ((MultiBucketsAggregation) middleSortIdBucket.getAggregations().asMap().get("priceAreaAgg"));
|
|
|
if(priceAreaAggregation==null){
|
|
|
/**
|
|
|
* 品类-品牌聚合
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
private TermsAggregationBuilder sortBrandAggBuilder() {
|
|
|
TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortBrandMiddleSortIdAgg").field(ProductIndexEsField.middleSortId).size(100);
|
|
|
middleSortAggBuilder.subAggregation(AggregationBuilders.terms("sortBrandBrandIdAgg").field(ProductIndexEsField.brandId).size(200));
|
|
|
return middleSortAggBuilder;
|
|
|
}
|
|
|
|
|
|
private List<PageSortBrand> getSortBrandsFromAggregationMap(Map<String, Aggregation> aggregationMap) {
|
|
|
Map<Integer,List<Integer>> sortBrandMap = this.getValueFromAggregationMap(aggregationMap,"sortBrandMiddleSortIdAgg","sortBrandBrandIdAgg");
|
|
|
List<PageSortBrand> pageSortBrands = new ArrayList<>();
|
|
|
for (Map.Entry<Integer,List<Integer>> entry: sortBrandMap.entrySet()) {
|
|
|
Integer middleSortId = entry.getKey();
|
|
|
List<Integer> brandIds = entry.getValue();
|
|
|
pageSortBrands.add(new PageSortBrand(middleSortId,brandIds));
|
|
|
}
|
|
|
return pageSortBrands;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从聚合结果中获取参数,仅支持二层聚合
|
|
|
* @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;
|
|
|
}
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> priceAreaIterator = priceAreaAggregation.getBuckets().iterator();
|
|
|
while (priceAreaIterator.hasNext()) {
|
|
|
MultiBucketsAggregation.Bucket genderBucket = priceAreaIterator.next();
|
|
|
Integer priceArea = Integer.valueOf(genderBucket.getKeyAsString());
|
|
|
sortPrices.add(new SortPriceArea(middleSortId, priceArea));
|
|
|
List<Integer> secondAggregationBucketKeys = this.getAggValuesFromMultiBucketsAggregation((MultiBucketsAggregation)secondAggregationMap.get(secondAggName));
|
|
|
aggResultMap.put(firstAggregationBucketKey,secondAggregationBucketKeys);
|
|
|
}
|
|
|
return aggResultMap;
|
|
|
}
|
|
|
return sortPrices;
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|