Authored by hugufei

添加按productId实时召回的逻辑

1 -package com.yoho.search.recall.scene.beans.persional;  
2 -  
3 -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;  
4 -import com.yoho.search.base.utils.ISearchConstants;  
5 -import com.yoho.search.base.utils.ProductIndexEsField;  
6 -import com.yoho.search.core.es.model.SearchParam;  
7 -import com.yoho.search.core.es.model.SearchResult;  
8 -import com.yoho.search.recall.scene.constants.CacheTimeConstants;  
9 -import com.yoho.search.recall.scene.models.common.ParamQueryFilter;  
10 -import com.yoho.search.recall.scene.models.personal.PageSknBitSet;  
11 -import com.yoho.search.service.base.SearchCommonService;  
12 -import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;  
13 -import org.elasticsearch.search.aggregations.Aggregation;  
14 -import org.elasticsearch.search.aggregations.AggregationBuilders;  
15 -import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;  
16 -import org.elasticsearch.search.aggregations.bucket.terms.Terms;  
17 -import org.springframework.beans.factory.annotation.Autowired;  
18 -import org.springframework.stereotype.Component;  
19 -  
20 -import java.util.ArrayList;  
21 -import java.util.Iterator;  
22 -import java.util.List;  
23 -import java.util.Map;  
24 -  
25 -@Component  
26 -public class PageSknBitSetComponent extends AbstractPageComponent {  
27 -  
28 - @Autowired  
29 - private SearchCommonService searchCommonService;  
30 -  
31 - /**  
32 - * 获取页面上的skn的bitset  
33 - *  
34 - * @param paramQueryFilter  
35 - * @return  
36 - */  
37 - public PageSknBitSet queryPageSknBitSet(ParamQueryFilter paramQueryFilter) {  
38 - Object value = super.queryWithCache(paramQueryFilter);  
39 - return value==null?null:(PageSknBitSet)value;  
40 - }  
41 -  
42 - @Override  
43 - protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {  
44 - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:PAGE_SKN_BITSET:").appendVar(paramQueryFilter.getParamMd5Key());  
45 - }  
46 -  
47 - @Override  
48 - protected int cacheTimeInSecond() {  
49 - return CacheTimeConstants.PAGE_SKN_BITSET;  
50 - }  
51 -  
52 - @Override  
53 - protected Object doRealQuery(ParamQueryFilter paramQueryFilter) {  
54 - //1、构造请求参数  
55 - SearchParam searchParam = new SearchParam();  
56 - searchParam.setQuery(paramQueryFilter.getParamQuery());  
57 - searchParam.setFiter(paramQueryFilter.getParamFilter());  
58 - searchParam.setSize(0);  
59 -  
60 - //2、构造聚合参数  
61 - List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();  
62 - aggregationBuilders.add(AggregationBuilders.terms("productIdAgg").field(ProductIndexEsField.productId).size(10000).order(Terms.Order.term(false)));//品类-品牌聚合  
63 - searchParam.setAggregationBuilders(aggregationBuilders);  
64 -  
65 - //3、执行查询  
66 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);  
67 -  
68 - //4、构造结果  
69 - Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();  
70 - PageSknBitSet pageSknBitSet = this.getPageSknBitSetFromAggregationMap(aggregationMap,"productIdAgg");  
71 - return pageSknBitSet;  
72 - }  
73 -  
74 - private PageSknBitSet getPageSknBitSetFromAggregationMap(Map<String, Aggregation> aggregationMap,String firstAggName){  
75 - if(!aggregationMap.containsKey(firstAggName)){  
76 - return null;  
77 - }  
78 - List<Integer> productIdList = new ArrayList<Integer>();  
79 - MultiBucketsAggregation firstAggregation = (MultiBucketsAggregation) aggregationMap.get(firstAggName);  
80 - Iterator<? extends MultiBucketsAggregation.Bucket> firstAggregationIterator = firstAggregation.getBuckets().iterator();  
81 - while (firstAggregationIterator.hasNext()) {  
82 - MultiBucketsAggregation.Bucket bucket = firstAggregationIterator.next();  
83 - Integer value = Integer.valueOf(bucket.getKeyAsString());  
84 - productIdList.add(value);  
85 - }  
86 - PageSknBitSet pageSknBitSet = new PageSknBitSet();  
87 - for (Integer productId: productIdList) {  
88 - pageSknBitSet.add(productId);  
89 - }  
90 - return pageSknBitSet;  
91 - }  
92 -  
93 -  
94 -}  
1 -package com.yoho.search.recall.scene.models.personal;  
2 -  
3 -import java.io.Serializable;  
4 -import java.util.BitSet;  
5 -  
6 -public class PageSknBitSet implements Serializable{  
7 -  
8 - public static final int maxValue = 2000000;  
9 -  
10 - private static final long serialVersionUID = 7185024266096124078L;  
11 - private BitSet sknBitSet;  
12 -  
13 - public PageSknBitSet(){  
14 - this.sknBitSet = new BitSet(maxValue);  
15 - }  
16 -  
17 - public void add(int bitSetIndex) {  
18 - if(bitSetIndex>maxValue){  
19 - return;  
20 - }  
21 - this.sknBitSet.set(bitSetIndex);  
22 - }  
23 -  
24 - public boolean exist(int bitSetIndex) {  
25 - return sknBitSet.get(bitSetIndex);  
26 - }  
27 -}