添加按推荐出来的productId实时召回的逻辑
Showing
7 changed files
with
403 additions
and
403 deletions
1 | -package com.yoho.search.recall.scene.beans.cache; | ||
2 | - | ||
3 | -import com.yoho.search.base.utils.ISearchConstants; | ||
4 | -import com.yoho.search.base.utils.ProductIndexEsField; | ||
5 | -import com.yoho.search.core.es.model.SearchParam; | ||
6 | -import com.yoho.search.core.es.model.SearchResult; | ||
7 | -import com.yoho.search.recall.scene.beans.helper.RecallResponseHelper; | ||
8 | -import com.yoho.search.recall.scene.beans.strategy.impls.RecommendProductStrategy; | ||
9 | -import com.yoho.search.recall.scene.models.common.IRecallRequest; | ||
10 | -import com.yoho.search.recall.scene.models.req.*; | ||
11 | -import com.yoho.search.service.base.SearchCommonService; | ||
12 | -import org.apache.commons.collections.MapUtils; | ||
13 | -import org.elasticsearch.index.query.QueryBuilders; | ||
14 | -import org.springframework.beans.factory.annotation.Autowired; | ||
15 | -import org.springframework.stereotype.Component; | ||
16 | - | ||
17 | -import java.util.*; | ||
18 | - | ||
19 | -@Component | ||
20 | -public class RecommendProductRecallCacheBean extends AbstractCacheBean<RecommendProductRequest, RecallResponse, RecommendProductRequestResponse> { | ||
21 | - | ||
22 | - @Autowired | ||
23 | - private SearchCommonService searchCommonService; | ||
24 | - | ||
25 | - /** | ||
26 | - * 按productId召回的入口 | ||
27 | - * @param productIds | ||
28 | - * @return | ||
29 | - */ | ||
30 | - public List<RecallRequestResponse> batchRecallAndCache(List<Integer> productIds) { | ||
31 | - //1、参数判断 | ||
32 | - if(productIds==null || productIds.isEmpty()){ | ||
33 | - return new ArrayList<>(); | ||
34 | - } | ||
35 | - | ||
36 | - //2、执行查询 | ||
37 | - List<RecommendProductRequestResponse> responses = this.batchQuery(productIds); | ||
38 | - | ||
39 | - //3、数据转换 | ||
40 | - List<RecallRequestResponse> results = new ArrayList<>(); | ||
41 | - for (RecommendProductRequestResponse recommendProductRequestResponse: responses){ | ||
42 | - RecommendProductRequest request = recommendProductRequestResponse.getRequest(); | ||
43 | - RecallResponse response = recommendProductRequestResponse.getResponse(); | ||
44 | - if(request==null || response==null){ | ||
45 | - continue; | ||
46 | - } | ||
47 | - Integer productId = request.getProductId(); | ||
48 | - RecallRequest recallRequest = new RecallRequest(new RecommendProductStrategy(productId)); | ||
49 | - results.add(new RecallRequestResponse(recallRequest,response)); | ||
50 | - } | ||
51 | - return results; | ||
52 | - } | ||
53 | - | ||
54 | - private List<RecommendProductRequestResponse> batchQuery(List<Integer> productIds){ | ||
55 | - //1、构造结果 | ||
56 | - final List<RecommendProductRequestResponse> results = new ArrayList<>(); | ||
57 | - for (Integer productId : productIds) { | ||
58 | - results.add(new RecommendProductRequestResponse(new RecommendProductRequest(productId))); | ||
59 | - } | ||
60 | - //2、调父类方法 | ||
61 | - super.bacthFillResponseWithCache(results,productIds.size()); | ||
62 | - //3、返回结果 | ||
63 | - return results; | ||
64 | - } | ||
65 | - | ||
66 | - public void batchAddProductRecallInfoToCache(Map<Integer,RecallResponse> productVectors){ | ||
67 | - List<RecommendProductRequestResponse> results = new ArrayList<>(); | ||
68 | - for (Map.Entry<Integer,RecallResponse> entry: productVectors.entrySet()) { | ||
69 | - RecommendProductRequestResponse result = new RecommendProductRequestResponse(new RecommendProductRequest(entry.getKey())); | ||
70 | - result.setResponse(entry.getValue(),true); | ||
71 | - results.add(result); | ||
72 | - } | ||
73 | - super.batchAddResponseToCache(results); | ||
74 | - } | ||
75 | - | ||
76 | - @Override | ||
77 | - protected boolean useEhCache() { | ||
78 | - return false; | ||
79 | - } | ||
80 | - | ||
81 | - @Override | ||
82 | - protected Map<RecommendProductRequest, RecallResponse> queryMissCacheRequestResults(List<RecommendProductRequestResponse> missCacheRequests) { | ||
83 | - //1、合法性判断 | ||
84 | - Map<RecommendProductRequest,RecallResponse> results = new HashMap<>(); | ||
85 | - if(missCacheRequests==null||missCacheRequests.isEmpty()){ | ||
86 | - return results; | ||
87 | - } | ||
88 | - | ||
89 | - //2、获取productId | ||
90 | - List<Integer> productIds = new ArrayList<>(); | ||
91 | - for (RecommendProductRequestResponse recommendProductRequestResponse : missCacheRequests) { | ||
92 | - productIds.add(recommendProductRequestResponse.getRequest().getProductId()); | ||
93 | - } | ||
94 | - | ||
95 | - //3、构建SearchParam并查询 | ||
96 | - SearchParam searchParam = new SearchParam(); | ||
97 | - searchParam.setOffset(0); | ||
98 | - searchParam.setSize(productIds.size()); | ||
99 | - searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productId, productIds)); | ||
100 | - searchParam.setIncludeFields( missCacheRequests.get(0).getRequest().includeFields()); | ||
101 | - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); | ||
102 | - | ||
103 | - //4、构建基于ProductId的临时结果 | ||
104 | - Map<Integer,RecallResponse> productTempMap = new HashMap<>(); | ||
105 | - for (Map<String, Object> productInfo: searchResult.getResultList()){ | ||
106 | - Integer productId = MapUtils.getIntValue(productInfo,ProductIndexEsField.productId,0); | ||
107 | - RecallResponse.RecallSkn recallSkn = RecallResponseHelper.buildRecallSkn(productInfo); | ||
108 | - RecallResponse recallResponse = RecallResponseHelper.buildRecallResponse(recallSkn); | ||
109 | - productTempMap.put(productId,recallResponse); | ||
110 | - } | ||
111 | - //5、构造最终结果 | ||
112 | - for (RecommendProductRequestResponse recommendProductRequestResponse :missCacheRequests ) { | ||
113 | - results.put(recommendProductRequestResponse.getRequest(),productTempMap.get(recommendProductRequestResponse.getRequest().getProductId())); | ||
114 | - } | ||
115 | - return results; | ||
116 | - } | ||
117 | - | ||
118 | -} | 1 | +package com.yoho.search.recall.scene.beans.cache; |
2 | + | ||
3 | +import com.yoho.search.base.utils.ISearchConstants; | ||
4 | +import com.yoho.search.base.utils.ProductIndexEsField; | ||
5 | +import com.yoho.search.core.es.model.SearchParam; | ||
6 | +import com.yoho.search.core.es.model.SearchResult; | ||
7 | +import com.yoho.search.recall.scene.beans.helper.RecallResponseHelper; | ||
8 | +import com.yoho.search.recall.scene.beans.strategy.impls.RecommendProductStrategy; | ||
9 | +import com.yoho.search.recall.scene.models.common.IRecallRequest; | ||
10 | +import com.yoho.search.recall.scene.models.req.*; | ||
11 | +import com.yoho.search.service.base.SearchCommonService; | ||
12 | +import org.apache.commons.collections.MapUtils; | ||
13 | +import org.elasticsearch.index.query.QueryBuilders; | ||
14 | +import org.springframework.beans.factory.annotation.Autowired; | ||
15 | +import org.springframework.stereotype.Component; | ||
16 | + | ||
17 | +import java.util.*; | ||
18 | + | ||
19 | +@Component | ||
20 | +public class RecommendProductRecallCacheBean extends AbstractCacheBean<RecommendProductRequest, RecallResponse, RecommendProductRequestResponse> { | ||
21 | + | ||
22 | + @Autowired | ||
23 | + private SearchCommonService searchCommonService; | ||
24 | + | ||
25 | + /** | ||
26 | + * 按productId召回的入口 | ||
27 | + * @param productIds | ||
28 | + * @return | ||
29 | + */ | ||
30 | + public List<RecallRequestResponse> batchRecallAndCache(List<Integer> productIds) { | ||
31 | + //1、参数判断 | ||
32 | + if(productIds==null || productIds.isEmpty()){ | ||
33 | + return new ArrayList<>(); | ||
34 | + } | ||
35 | + | ||
36 | + //2、执行查询 | ||
37 | + List<RecommendProductRequestResponse> responses = this.batchQuery(productIds); | ||
38 | + | ||
39 | + //3、数据转换 | ||
40 | + List<RecallRequestResponse> results = new ArrayList<>(); | ||
41 | + for (RecommendProductRequestResponse recommendProductRequestResponse: responses){ | ||
42 | + RecommendProductRequest request = recommendProductRequestResponse.getRequest(); | ||
43 | + RecallResponse response = recommendProductRequestResponse.getResponse(); | ||
44 | + if(request==null || response==null){ | ||
45 | + continue; | ||
46 | + } | ||
47 | + Integer productId = request.getProductId(); | ||
48 | + RecallRequest recallRequest = new RecallRequest(new RecommendProductStrategy(productId)); | ||
49 | + results.add(new RecallRequestResponse(recallRequest,response)); | ||
50 | + } | ||
51 | + return results; | ||
52 | + } | ||
53 | + | ||
54 | + private List<RecommendProductRequestResponse> batchQuery(List<Integer> productIds){ | ||
55 | + //1、构造结果 | ||
56 | + final List<RecommendProductRequestResponse> results = new ArrayList<>(); | ||
57 | + for (Integer productId : productIds) { | ||
58 | + results.add(new RecommendProductRequestResponse(new RecommendProductRequest(productId))); | ||
59 | + } | ||
60 | + //2、调父类方法 | ||
61 | + super.bacthFillResponseWithCache(results,productIds.size()); | ||
62 | + //3、返回结果 | ||
63 | + return results; | ||
64 | + } | ||
65 | + | ||
66 | + public void batchAddProductRecallInfoToCache(Map<Integer,RecallResponse> productVectors){ | ||
67 | + List<RecommendProductRequestResponse> results = new ArrayList<>(); | ||
68 | + for (Map.Entry<Integer,RecallResponse> entry: productVectors.entrySet()) { | ||
69 | + RecommendProductRequestResponse result = new RecommendProductRequestResponse(new RecommendProductRequest(entry.getKey())); | ||
70 | + result.setResponse(entry.getValue(),true); | ||
71 | + results.add(result); | ||
72 | + } | ||
73 | + super.batchAddResponseToCache(results); | ||
74 | + } | ||
75 | + | ||
76 | + @Override | ||
77 | + protected boolean useEhCache() { | ||
78 | + return false; | ||
79 | + } | ||
80 | + | ||
81 | + @Override | ||
82 | + protected Map<RecommendProductRequest, RecallResponse> queryMissCacheRequestResults(List<RecommendProductRequestResponse> missCacheRequests) { | ||
83 | + //1、合法性判断 | ||
84 | + Map<RecommendProductRequest,RecallResponse> results = new HashMap<>(); | ||
85 | + if(missCacheRequests==null||missCacheRequests.isEmpty()){ | ||
86 | + return results; | ||
87 | + } | ||
88 | + | ||
89 | + //2、获取productId | ||
90 | + List<Integer> productIds = new ArrayList<>(); | ||
91 | + for (RecommendProductRequestResponse recommendProductRequestResponse : missCacheRequests) { | ||
92 | + productIds.add(recommendProductRequestResponse.getRequest().getProductId()); | ||
93 | + } | ||
94 | + | ||
95 | + //3、构建SearchParam并查询 | ||
96 | + SearchParam searchParam = new SearchParam(); | ||
97 | + searchParam.setOffset(0); | ||
98 | + searchParam.setSize(productIds.size()); | ||
99 | + searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productId, productIds)); | ||
100 | + searchParam.setIncludeFields( missCacheRequests.get(0).getRequest().includeFields()); | ||
101 | + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); | ||
102 | + | ||
103 | + //4、构建基于ProductId的临时结果 | ||
104 | + Map<Integer,RecallResponse> productTempMap = new HashMap<>(); | ||
105 | + for (Map<String, Object> productInfo: searchResult.getResultList()){ | ||
106 | + Integer productId = MapUtils.getIntValue(productInfo,ProductIndexEsField.productId,0); | ||
107 | + RecallResponse.RecallSkn recallSkn = RecallResponseHelper.buildRecallSkn(productInfo); | ||
108 | + RecallResponse recallResponse = RecallResponseHelper.buildRecallResponse(recallSkn); | ||
109 | + productTempMap.put(productId,recallResponse); | ||
110 | + } | ||
111 | + //5、构造最终结果 | ||
112 | + for (RecommendProductRequestResponse recommendProductRequestResponse :missCacheRequests ) { | ||
113 | + results.put(recommendProductRequestResponse.getRequest(),productTempMap.get(recommendProductRequestResponse.getRequest().getProductId())); | ||
114 | + } | ||
115 | + return results; | ||
116 | + } | ||
117 | + | ||
118 | +} |
1 | -package com.yoho.search.recall.scene.beans.helper; | ||
2 | - | ||
3 | -import com.yoho.search.base.utils.ProductIndexEsField; | ||
4 | -import com.yoho.search.recall.scene.models.req.RecallResponse; | ||
5 | -import org.apache.commons.collections.MapUtils; | ||
6 | - | ||
7 | -import java.util.ArrayList; | ||
8 | -import java.util.List; | ||
9 | -import java.util.Map; | ||
10 | - | ||
11 | -public class RecallResponseHelper { | ||
12 | - | ||
13 | - public static RecallResponse.RecallSkn buildRecallSkn(Map<String, Object> productInfo) { | ||
14 | - Integer productSkn = MapUtils.getInteger(productInfo, ProductIndexEsField.productSkn, 0); | ||
15 | - Integer brandId = MapUtils.getInteger(productInfo, ProductIndexEsField.brandId, 0); | ||
16 | - Integer middleSortId = MapUtils.getInteger(productInfo, ProductIndexEsField.middleSortId, 0); | ||
17 | - Integer priceArea = MapUtils.getInteger(productInfo, ProductIndexEsField.priceArea, 0); | ||
18 | - return new RecallResponse.RecallSkn(productSkn, brandId, middleSortId,priceArea); | ||
19 | - } | ||
20 | - | ||
21 | - public static RecallResponse buildRecallResponse(RecallResponse.RecallSkn recallSkn) { | ||
22 | - List<RecallResponse.RecallSkn> recallSknList = new ArrayList<>(); | ||
23 | - recallSknList.add(recallSkn); | ||
24 | - return new RecallResponse(1, recallSknList); | ||
25 | - } | ||
26 | - | ||
27 | - public static RecallResponse buildRecallResponse(List<RecallResponse.RecallSkn> recallSknList,long total) { | ||
28 | - return new RecallResponse(total, recallSknList); | ||
29 | - } | ||
30 | - | ||
31 | -} | 1 | +package com.yoho.search.recall.scene.beans.helper; |
2 | + | ||
3 | +import com.yoho.search.base.utils.ProductIndexEsField; | ||
4 | +import com.yoho.search.recall.scene.models.req.RecallResponse; | ||
5 | +import org.apache.commons.collections.MapUtils; | ||
6 | + | ||
7 | +import java.util.ArrayList; | ||
8 | +import java.util.List; | ||
9 | +import java.util.Map; | ||
10 | + | ||
11 | +public class RecallResponseHelper { | ||
12 | + | ||
13 | + public static RecallResponse.RecallSkn buildRecallSkn(Map<String, Object> productInfo) { | ||
14 | + Integer productSkn = MapUtils.getInteger(productInfo, ProductIndexEsField.productSkn, 0); | ||
15 | + Integer brandId = MapUtils.getInteger(productInfo, ProductIndexEsField.brandId, 0); | ||
16 | + Integer middleSortId = MapUtils.getInteger(productInfo, ProductIndexEsField.middleSortId, 0); | ||
17 | + Integer priceArea = MapUtils.getInteger(productInfo, ProductIndexEsField.priceArea, 0); | ||
18 | + return new RecallResponse.RecallSkn(productSkn, brandId, middleSortId,priceArea); | ||
19 | + } | ||
20 | + | ||
21 | + public static RecallResponse buildRecallResponse(RecallResponse.RecallSkn recallSkn) { | ||
22 | + List<RecallResponse.RecallSkn> recallSknList = new ArrayList<>(); | ||
23 | + recallSknList.add(recallSkn); | ||
24 | + return new RecallResponse(1, recallSknList); | ||
25 | + } | ||
26 | + | ||
27 | + public static RecallResponse buildRecallResponse(List<RecallResponse.RecallSkn> recallSknList,long total) { | ||
28 | + return new RecallResponse(total, recallSknList); | ||
29 | + } | ||
30 | + | ||
31 | +} |
service/src/main/java/com/yoho/search/recall/scene/beans/persional/PageProductIdBitSetComponent.java
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.PageProductIdBitSet; | ||
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 PageProductIdBitSetComponent extends AbstractPageComponent { | ||
27 | - | ||
28 | - @Autowired | ||
29 | - private SearchCommonService searchCommonService; | ||
30 | - | ||
31 | - /** | ||
32 | - * 获取页面上的skn的bitset | ||
33 | - * | ||
34 | - * @param paramQueryFilter | ||
35 | - * @return | ||
36 | - */ | ||
37 | - public PageProductIdBitSet queryPageProductIdBitSet(ParamQueryFilter paramQueryFilter) { | ||
38 | - Object value = super.queryWithCache(paramQueryFilter); | ||
39 | - return value==null?null:(PageProductIdBitSet)value; | ||
40 | - } | ||
41 | - | ||
42 | - @Override | ||
43 | - protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) { | ||
44 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:PAGE_PRODUCT_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 | - PageProductIdBitSet pageProductIdBitSet = this.getPageSknBitSetFromAggregationMap(aggregationMap,"productIdAgg"); | ||
71 | - return pageProductIdBitSet; | ||
72 | - } | ||
73 | - | ||
74 | - private PageProductIdBitSet 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 | - PageProductIdBitSet pageProductIdBitSet = new PageProductIdBitSet(); | ||
87 | - for (Integer productId: productIdList) { | ||
88 | - pageProductIdBitSet.add(productId); | ||
89 | - } | ||
90 | - return pageProductIdBitSet; | ||
91 | - } | ||
92 | - | ||
93 | - | ||
94 | -} | 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.PageProductIdBitSet; | ||
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 PageProductIdBitSetComponent extends AbstractPageComponent { | ||
27 | + | ||
28 | + @Autowired | ||
29 | + private SearchCommonService searchCommonService; | ||
30 | + | ||
31 | + /** | ||
32 | + * 获取页面上的skn的bitset | ||
33 | + * | ||
34 | + * @param paramQueryFilter | ||
35 | + * @return | ||
36 | + */ | ||
37 | + public PageProductIdBitSet queryPageProductIdBitSet(ParamQueryFilter paramQueryFilter) { | ||
38 | + Object value = super.queryWithCache(paramQueryFilter); | ||
39 | + return value==null?null:(PageProductIdBitSet)value; | ||
40 | + } | ||
41 | + | ||
42 | + @Override | ||
43 | + protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) { | ||
44 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:PAGE_PRODUCT_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 | + PageProductIdBitSet pageProductIdBitSet = this.getPageSknBitSetFromAggregationMap(aggregationMap,"productIdAgg"); | ||
71 | + return pageProductIdBitSet; | ||
72 | + } | ||
73 | + | ||
74 | + private PageProductIdBitSet 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 | + PageProductIdBitSet pageProductIdBitSet = new PageProductIdBitSet(); | ||
87 | + for (Integer productId: productIdList) { | ||
88 | + pageProductIdBitSet.add(productId); | ||
89 | + } | ||
90 | + return pageProductIdBitSet; | ||
91 | + } | ||
92 | + | ||
93 | + | ||
94 | +} |
1 | -package com.yoho.search.recall.scene.beans.strategy.impls; | ||
2 | - | ||
3 | -import com.yoho.search.recall.scene.beans.helper.ExtendFilterHelper; | ||
4 | -import com.yoho.search.recall.scene.beans.helper.SortBuilderHelper; | ||
5 | -import com.yoho.search.recall.scene.beans.strategy.IStrategy; | ||
6 | -import com.yoho.search.recall.scene.beans.strategy.StrategyEnum; | ||
7 | -import com.yoho.search.recall.scene.constants.CacheTimeConstants; | ||
8 | -import org.elasticsearch.index.query.QueryBuilder; | ||
9 | -import org.elasticsearch.search.sort.SortBuilder; | ||
10 | - | ||
11 | -/** | ||
12 | - * 按productId的召回策略 | ||
13 | - * | ||
14 | - * @author gufei.hu | ||
15 | - * | ||
16 | - */ | ||
17 | - | ||
18 | -public class RecommendProductStrategy implements IStrategy { | ||
19 | - | ||
20 | - private Integer productId; | ||
21 | - | ||
22 | - public RecommendProductStrategy(Integer productId) { | ||
23 | - this.productId = productId; | ||
24 | - } | ||
25 | - | ||
26 | - @Override | ||
27 | - public StrategyEnum strategtEnum() { | ||
28 | - return StrategyEnum.RECOMMEND_PRODUCT; | ||
29 | - } | ||
30 | - | ||
31 | - @Override | ||
32 | - public QueryBuilder extendFilter() { | ||
33 | - return ExtendFilterHelper.productIdFilter(this.productId); | ||
34 | - } | ||
35 | - | ||
36 | - @Override | ||
37 | - public SortBuilder<?> sortBuilder() { | ||
38 | - return SortBuilderHelper.getIdDescSort(); | ||
39 | - } | ||
40 | - | ||
41 | - @Override | ||
42 | - public int size() { | ||
43 | - return 1; | ||
44 | - } | ||
45 | - | ||
46 | - @Override | ||
47 | - public int cacheTimeInSecond() { | ||
48 | - return CacheTimeConstants.PRODUCTID_TO_RECALL_RESPONSE; | ||
49 | - } | ||
50 | - | ||
51 | - @Override | ||
52 | - public String strategyCacheKey() { | ||
53 | - StringBuilder sb = defaultStrategyKey(); | ||
54 | - sb.append(this.productId==null?"0":productId.toString()); | ||
55 | - return sb.toString(); | ||
56 | - } | ||
57 | - | ||
58 | -} | 1 | +package com.yoho.search.recall.scene.beans.strategy.impls; |
2 | + | ||
3 | +import com.yoho.search.recall.scene.beans.helper.ExtendFilterHelper; | ||
4 | +import com.yoho.search.recall.scene.beans.helper.SortBuilderHelper; | ||
5 | +import com.yoho.search.recall.scene.beans.strategy.IStrategy; | ||
6 | +import com.yoho.search.recall.scene.beans.strategy.StrategyEnum; | ||
7 | +import com.yoho.search.recall.scene.constants.CacheTimeConstants; | ||
8 | +import org.elasticsearch.index.query.QueryBuilder; | ||
9 | +import org.elasticsearch.search.sort.SortBuilder; | ||
10 | + | ||
11 | +/** | ||
12 | + * 按productId的召回策略 | ||
13 | + * | ||
14 | + * @author gufei.hu | ||
15 | + * | ||
16 | + */ | ||
17 | + | ||
18 | +public class RecommendProductStrategy implements IStrategy { | ||
19 | + | ||
20 | + private Integer productId; | ||
21 | + | ||
22 | + public RecommendProductStrategy(Integer productId) { | ||
23 | + this.productId = productId; | ||
24 | + } | ||
25 | + | ||
26 | + @Override | ||
27 | + public StrategyEnum strategtEnum() { | ||
28 | + return StrategyEnum.RECOMMEND_PRODUCT; | ||
29 | + } | ||
30 | + | ||
31 | + @Override | ||
32 | + public QueryBuilder extendFilter() { | ||
33 | + return ExtendFilterHelper.productIdFilter(this.productId); | ||
34 | + } | ||
35 | + | ||
36 | + @Override | ||
37 | + public SortBuilder<?> sortBuilder() { | ||
38 | + return SortBuilderHelper.getIdDescSort(); | ||
39 | + } | ||
40 | + | ||
41 | + @Override | ||
42 | + public int size() { | ||
43 | + return 1; | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public int cacheTimeInSecond() { | ||
48 | + return CacheTimeConstants.PRODUCTID_TO_RECALL_RESPONSE; | ||
49 | + } | ||
50 | + | ||
51 | + @Override | ||
52 | + public String strategyCacheKey() { | ||
53 | + StringBuilder sb = defaultStrategyKey(); | ||
54 | + sb.append(this.productId==null?"0":productId.toString()); | ||
55 | + return sb.toString(); | ||
56 | + } | ||
57 | + | ||
58 | +} |
1 | -package com.yoho.search.recall.scene.models.personal; | ||
2 | - | ||
3 | -import java.io.Serializable; | ||
4 | -import java.util.BitSet; | ||
5 | - | ||
6 | -public class PageProductIdBitSet 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 PageProductIdBitSet(){ | ||
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 | -} | 1 | +package com.yoho.search.recall.scene.models.personal; |
2 | + | ||
3 | +import java.io.Serializable; | ||
4 | +import java.util.BitSet; | ||
5 | + | ||
6 | +public class PageProductIdBitSet 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 PageProductIdBitSet(){ | ||
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 | +} |
1 | -package com.yoho.search.recall.scene.models.req; | ||
2 | - | ||
3 | -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | -import com.yoho.search.base.utils.ProductIndexEsField; | ||
5 | -import com.yoho.search.core.es.model.SearchParam; | ||
6 | -import com.yoho.search.recall.scene.beans.strategy.StrategyEnum; | ||
7 | -import com.yoho.search.recall.scene.constants.CacheTimeConstants; | ||
8 | -import com.yoho.search.recall.scene.models.common.ICacheRequest; | ||
9 | -import com.yoho.search.recall.scene.models.common.IRecallRequest; | ||
10 | - | ||
11 | -import java.util.Arrays; | ||
12 | -import java.util.List; | ||
13 | - | ||
14 | -public class RecommendProductRequest implements ICacheRequest,IRecallRequest { | ||
15 | - | ||
16 | - private Integer productId; | ||
17 | - | ||
18 | - public RecommendProductRequest(Integer productId){ | ||
19 | - this.productId = productId; | ||
20 | - } | ||
21 | - | ||
22 | - @Override | ||
23 | - public RedisKeyBuilder redisKeyBuilder() { | ||
24 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("RECOMMEND_PRODUCT:").appendVar(cacheTimeInSecond()).appendFixed(":").appendVar(productId); | ||
25 | - } | ||
26 | - | ||
27 | - @Override | ||
28 | - public int cacheTimeInSecond() { | ||
29 | - return CacheTimeConstants.PRODUCTID_TO_RECALL_RESPONSE; | ||
30 | - } | ||
31 | - | ||
32 | - public Integer getProductId() { | ||
33 | - return productId; | ||
34 | - } | ||
35 | - | ||
36 | - @Override | ||
37 | - public StrategyEnum requestStrategy() { | ||
38 | - return StrategyEnum.RECOMMEND_PRODUCT; | ||
39 | - } | ||
40 | - | ||
41 | - @Override | ||
42 | - public SearchParam searchParam() { | ||
43 | - return null; | ||
44 | - } | ||
45 | - | ||
46 | - /** | ||
47 | - * 请求返回的字段 | ||
48 | - * @return | ||
49 | - */ | ||
50 | - public List<String> includeFields(){ | ||
51 | - return Arrays.asList(ProductIndexEsField.productId,ProductIndexEsField.productSkn,ProductIndexEsField.brandId,ProductIndexEsField.middleSortId,ProductIndexEsField.productFeatureFactor,ProductIndexEsField.priceArea); | ||
52 | - } | ||
53 | - | ||
54 | -} | 1 | +package com.yoho.search.recall.scene.models.req; |
2 | + | ||
3 | +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | +import com.yoho.search.base.utils.ProductIndexEsField; | ||
5 | +import com.yoho.search.core.es.model.SearchParam; | ||
6 | +import com.yoho.search.recall.scene.beans.strategy.StrategyEnum; | ||
7 | +import com.yoho.search.recall.scene.constants.CacheTimeConstants; | ||
8 | +import com.yoho.search.recall.scene.models.common.ICacheRequest; | ||
9 | +import com.yoho.search.recall.scene.models.common.IRecallRequest; | ||
10 | + | ||
11 | +import java.util.Arrays; | ||
12 | +import java.util.List; | ||
13 | + | ||
14 | +public class RecommendProductRequest implements ICacheRequest,IRecallRequest { | ||
15 | + | ||
16 | + private Integer productId; | ||
17 | + | ||
18 | + public RecommendProductRequest(Integer productId){ | ||
19 | + this.productId = productId; | ||
20 | + } | ||
21 | + | ||
22 | + @Override | ||
23 | + public RedisKeyBuilder redisKeyBuilder() { | ||
24 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("RECOMMEND_PRODUCT:").appendVar(cacheTimeInSecond()).appendFixed(":").appendVar(productId); | ||
25 | + } | ||
26 | + | ||
27 | + @Override | ||
28 | + public int cacheTimeInSecond() { | ||
29 | + return CacheTimeConstants.PRODUCTID_TO_RECALL_RESPONSE; | ||
30 | + } | ||
31 | + | ||
32 | + public Integer getProductId() { | ||
33 | + return productId; | ||
34 | + } | ||
35 | + | ||
36 | + @Override | ||
37 | + public StrategyEnum requestStrategy() { | ||
38 | + return StrategyEnum.RECOMMEND_PRODUCT; | ||
39 | + } | ||
40 | + | ||
41 | + @Override | ||
42 | + public SearchParam searchParam() { | ||
43 | + return null; | ||
44 | + } | ||
45 | + | ||
46 | + /** | ||
47 | + * 请求返回的字段 | ||
48 | + * @return | ||
49 | + */ | ||
50 | + public List<String> includeFields(){ | ||
51 | + return Arrays.asList(ProductIndexEsField.productId,ProductIndexEsField.productSkn,ProductIndexEsField.brandId,ProductIndexEsField.middleSortId,ProductIndexEsField.productFeatureFactor,ProductIndexEsField.priceArea); | ||
52 | + } | ||
53 | + | ||
54 | +} |
1 | -package com.yoho.search.recall.scene.models.req; | ||
2 | - | ||
3 | -import com.yoho.search.base.utils.Transfer; | ||
4 | -import com.yoho.search.recall.scene.models.common.AbstractCacheRequestResponse; | ||
5 | - | ||
6 | -public class RecommendProductRequestResponse extends AbstractCacheRequestResponse<RecommendProductRequest, RecallResponse> { | ||
7 | - | ||
8 | - public RecommendProductRequestResponse(RecommendProductRequest request) { | ||
9 | - super(request); | ||
10 | - } | ||
11 | - | ||
12 | - @Override | ||
13 | - public Transfer<String, RecallResponse> getToResponseTransfer() { | ||
14 | - return RecallRequestResponse.toResponseTransfer; | ||
15 | - } | ||
16 | - | ||
17 | - @Override | ||
18 | - public Transfer<RecallResponse, String> getFromResponseTransfer() { | ||
19 | - return RecallRequestResponse.fromResponseTransfer; | ||
20 | - } | ||
21 | -} | 1 | +package com.yoho.search.recall.scene.models.req; |
2 | + | ||
3 | +import com.yoho.search.base.utils.Transfer; | ||
4 | +import com.yoho.search.recall.scene.models.common.AbstractCacheRequestResponse; | ||
5 | + | ||
6 | +public class RecommendProductRequestResponse extends AbstractCacheRequestResponse<RecommendProductRequest, RecallResponse> { | ||
7 | + | ||
8 | + public RecommendProductRequestResponse(RecommendProductRequest request) { | ||
9 | + super(request); | ||
10 | + } | ||
11 | + | ||
12 | + @Override | ||
13 | + public Transfer<String, RecallResponse> getToResponseTransfer() { | ||
14 | + return RecallRequestResponse.toResponseTransfer; | ||
15 | + } | ||
16 | + | ||
17 | + @Override | ||
18 | + public Transfer<RecallResponse, String> getFromResponseTransfer() { | ||
19 | + return RecallRequestResponse.fromResponseTransfer; | ||
20 | + } | ||
21 | +} |
-
Please register or login to post a comment