每种策略根据参数定义自己的缓存key,不依赖es的报文toString
Showing
12 changed files
with
87 additions
and
23 deletions
@@ -3,6 +3,7 @@ package com.yoho.search.recall.scene.models; | @@ -3,6 +3,7 @@ package com.yoho.search.recall.scene.models; | ||
3 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | 3 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; |
4 | import com.yoho.search.base.utils.MD5Util; | 4 | import com.yoho.search.base.utils.MD5Util; |
5 | import com.yoho.search.core.es.model.SearchParam; | 5 | import com.yoho.search.core.es.model.SearchParam; |
6 | +import com.yoho.search.recall.scene.strategy.IStrategy; | ||
6 | import org.elasticsearch.index.query.BoolQueryBuilder; | 7 | import org.elasticsearch.index.query.BoolQueryBuilder; |
7 | import org.elasticsearch.index.query.QueryBuilder; | 8 | import org.elasticsearch.index.query.QueryBuilder; |
8 | import org.elasticsearch.index.query.QueryBuilders; | 9 | import org.elasticsearch.index.query.QueryBuilders; |
@@ -18,37 +19,26 @@ import java.util.List; | @@ -18,37 +19,26 @@ import java.util.List; | ||
18 | public class RecallRequest implements IRecallRequest { | 19 | public class RecallRequest implements IRecallRequest { |
19 | 20 | ||
20 | private ParamQueryFilter paramQueryFilter; | 21 | private ParamQueryFilter paramQueryFilter; |
21 | - private QueryBuilder extendFilter; | ||
22 | - private List<SortBuilder<?>> sortBuilders; | ||
23 | - private Integer size; | ||
24 | - private String requestType; | 22 | + private IStrategy strategy; |
25 | private RedisKeyBuilder redisKeyBuilder; | 23 | private RedisKeyBuilder redisKeyBuilder; |
26 | - private int cacheTimeInSecond; | ||
27 | 24 | ||
28 | - public RecallRequest(ParamQueryFilter paramQueryFilter, QueryBuilder extendFilter, SortBuilder<?> sortBuilder, Integer size, String requestType, int cacheTimeInSecond) { | 25 | + public RecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) { |
29 | this.paramQueryFilter = paramQueryFilter; | 26 | this.paramQueryFilter = paramQueryFilter; |
30 | - this.extendFilter = extendFilter; | ||
31 | - this.sortBuilders = Arrays.asList(sortBuilder); | ||
32 | - this.size = size; | ||
33 | - this.requestType = requestType; | ||
34 | - this.cacheTimeInSecond = cacheTimeInSecond; | 27 | + this.strategy = strategy; |
35 | this.redisKeyBuilder = genRedisKeyBuilder(); | 28 | this.redisKeyBuilder = genRedisKeyBuilder(); |
36 | } | 29 | } |
37 | 30 | ||
38 | private RedisKeyBuilder genRedisKeyBuilder() { | 31 | private RedisKeyBuilder genRedisKeyBuilder() { |
39 | StringBuilder sb = new StringBuilder(); | 32 | StringBuilder sb = new StringBuilder(); |
40 | sb.append("paramMd5Key:").append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key()); | 33 | sb.append("paramMd5Key:").append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key()); |
41 | - sb.append("extendFilter:").append(extendFilter == null ? "" : extendFilter.toString()); | ||
42 | - sb.append("sortBuilders:").append(sortBuilders == null ? "" : sortBuilders.toString()); | ||
43 | - sb.append("size:").append(size == null ? "0" : size.toString()); | ||
44 | - sb.append("cacheTimeInSecond:").append(cacheTimeInSecond); | 34 | + sb.append("strategyCacheKey:").append(strategy == null ? "" : strategy.strategyCacheKey()); |
45 | String cacheKey = MD5Util.string2MD5(sb.toString()); | 35 | String cacheKey = MD5Util.string2MD5(sb.toString()); |
46 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendVar(requestType).appendFixed(":").appendVar(cacheKey); | 36 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("RECALL:").appendVar(cacheKey); |
47 | } | 37 | } |
48 | 38 | ||
49 | @Override | 39 | @Override |
50 | public String requestType() { | 40 | public String requestType() { |
51 | - return this.requestType; | 41 | + return this.strategy.nameEnum().name(); |
52 | } | 42 | } |
53 | 43 | ||
54 | @Override | 44 | @Override |
@@ -58,7 +48,7 @@ public class RecallRequest implements IRecallRequest { | @@ -58,7 +48,7 @@ public class RecallRequest implements IRecallRequest { | ||
58 | 48 | ||
59 | @Override | 49 | @Override |
60 | public int cacheTimeInSecond() { | 50 | public int cacheTimeInSecond() { |
61 | - return this.cacheTimeInSecond; | 51 | + return this.strategy.cacheTimeInSecond(); |
62 | } | 52 | } |
63 | 53 | ||
64 | @Override | 54 | @Override |
@@ -67,19 +57,19 @@ public class RecallRequest implements IRecallRequest { | @@ -67,19 +57,19 @@ public class RecallRequest implements IRecallRequest { | ||
67 | searchParam.setQuery(this.paramQueryFilter.getParamQuery()); | 57 | searchParam.setQuery(this.paramQueryFilter.getParamQuery()); |
68 | searchParam.setFiter(this.getRealFilter()); | 58 | searchParam.setFiter(this.getRealFilter()); |
69 | searchParam.setIncludeFields(this.includeFields()); | 59 | searchParam.setIncludeFields(this.includeFields()); |
70 | - searchParam.setSortBuilders(this.sortBuilders); | 60 | + searchParam.setSortBuilders(Arrays.asList(this.strategy.sortBuilder())); |
71 | searchParam.setOffset(0); | 61 | searchParam.setOffset(0); |
72 | - searchParam.setSize(this.size); | 62 | + searchParam.setSize(this.strategy.size()); |
73 | return searchParam; | 63 | return searchParam; |
74 | } | 64 | } |
75 | 65 | ||
76 | private QueryBuilder getRealFilter() { | 66 | private QueryBuilder getRealFilter() { |
77 | - if (extendFilter == null) { | 67 | + if (this.strategy==null || strategy.extendFilter() == null) { |
78 | return this.paramQueryFilter.getParamFilter(); | 68 | return this.paramQueryFilter.getParamFilter(); |
79 | } | 69 | } |
80 | BoolQueryBuilder realFilter = QueryBuilders.boolQuery(); | 70 | BoolQueryBuilder realFilter = QueryBuilders.boolQuery(); |
81 | realFilter.must(this.paramQueryFilter.getParamFilter()); | 71 | realFilter.must(this.paramQueryFilter.getParamFilter()); |
82 | - realFilter.must(this.extendFilter); | 72 | + realFilter.must(this.strategy.extendFilter()); |
83 | return realFilter; | 73 | return realFilter; |
84 | } | 74 | } |
85 | 75 |
@@ -7,7 +7,7 @@ import com.yoho.search.recall.scene.strategy.IStrategy; | @@ -7,7 +7,7 @@ import com.yoho.search.recall.scene.strategy.IStrategy; | ||
7 | public abstract class BaseRecallRequest { | 7 | public abstract class BaseRecallRequest { |
8 | 8 | ||
9 | protected RecallRequest buildRecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) { | 9 | protected RecallRequest buildRecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) { |
10 | - return new RecallRequest(paramQueryFilter, strategy.extendFilter(), strategy.sortBuilder(), strategy.size(), strategy.nameEnum().name(),strategy.cacheTimeInSecond()); | 10 | + return new RecallRequest(paramQueryFilter, strategy); |
11 | } | 11 | } |
12 | 12 | ||
13 | } | 13 | } |
@@ -13,6 +13,16 @@ public interface IStrategy { | @@ -13,6 +13,16 @@ public interface IStrategy { | ||
13 | 13 | ||
14 | SortBuilder<?> sortBuilder();// 排序策略 | 14 | SortBuilder<?> sortBuilder();// 排序策略 |
15 | 15 | ||
16 | + String strategyCacheKey(); | ||
17 | + | ||
16 | int cacheTimeInSecond();//缓存时间 | 18 | int cacheTimeInSecond();//缓存时间 |
17 | 19 | ||
20 | + default StringBuilder defaultStrategyKey(){ | ||
21 | + StringBuilder sb = new StringBuilder(); | ||
22 | + sb.append(this.nameEnum().name()); | ||
23 | + sb.append(this.cacheTimeInSecond()); | ||
24 | + sb.append(this.size()); | ||
25 | + return sb; | ||
26 | + } | ||
27 | + | ||
18 | } | 28 | } |
1 | package com.yoho.search.recall.scene.strategy.impls; | 1 | package com.yoho.search.recall.scene.strategy.impls; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.yoho.search.base.utils.ProductIndexEsField; | 4 | import com.yoho.search.base.utils.ProductIndexEsField; |
4 | import com.yoho.search.recall.scene.constants.RecallConstants; | 5 | import com.yoho.search.recall.scene.constants.RecallConstants; |
5 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; | 6 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; |
@@ -61,4 +62,11 @@ public class BrandHeatValueStrategy implements IStrategy { | @@ -61,4 +62,11 @@ public class BrandHeatValueStrategy implements IStrategy { | ||
61 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; | 62 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; |
62 | } | 63 | } |
63 | 64 | ||
65 | + @Override | ||
66 | + public String strategyCacheKey() { | ||
67 | + StringBuilder sb = defaultStrategyKey(); | ||
68 | + sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds)); | ||
69 | + return sb.toString(); | ||
70 | + } | ||
71 | + | ||
64 | } | 72 | } |
1 | package com.yoho.search.recall.scene.strategy.impls; | 1 | package com.yoho.search.recall.scene.strategy.impls; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.yoho.search.base.utils.ProductIndexEsField; | 4 | import com.yoho.search.base.utils.ProductIndexEsField; |
4 | import com.yoho.search.recall.scene.constants.RecallConstants; | 5 | import com.yoho.search.recall.scene.constants.RecallConstants; |
5 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; | 6 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; |
@@ -60,4 +61,11 @@ public class BrandNewShelveStrategy implements IStrategy { | @@ -60,4 +61,11 @@ public class BrandNewShelveStrategy implements IStrategy { | ||
60 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; | 61 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; |
61 | } | 62 | } |
62 | 63 | ||
64 | + @Override | ||
65 | + public String strategyCacheKey() { | ||
66 | + StringBuilder sb = defaultStrategyKey(); | ||
67 | + sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds)); | ||
68 | + return sb.toString(); | ||
69 | + } | ||
70 | + | ||
63 | } | 71 | } |
1 | package com.yoho.search.recall.scene.strategy.impls; | 1 | package com.yoho.search.recall.scene.strategy.impls; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.yoho.search.base.utils.DateUtil; | 4 | import com.yoho.search.base.utils.DateUtil; |
4 | import com.yoho.search.base.utils.ProductIndexEsField; | 5 | import com.yoho.search.base.utils.ProductIndexEsField; |
5 | import com.yoho.search.recall.scene.constants.RecallConstants; | 6 | import com.yoho.search.recall.scene.constants.RecallConstants; |
@@ -78,4 +79,11 @@ public class BrandPromotionStrategy implements IStrategy { | @@ -78,4 +79,11 @@ public class BrandPromotionStrategy implements IStrategy { | ||
78 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; | 79 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; |
79 | } | 80 | } |
80 | 81 | ||
82 | + @Override | ||
83 | + public String strategyCacheKey() { | ||
84 | + StringBuilder sb = defaultStrategyKey(); | ||
85 | + sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds)); | ||
86 | + return sb.toString(); | ||
87 | + } | ||
88 | + | ||
81 | } | 89 | } |
1 | package com.yoho.search.recall.scene.strategy.impls; | 1 | package com.yoho.search.recall.scene.strategy.impls; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.yoho.search.base.utils.ProductIndexEsField; | 4 | import com.yoho.search.base.utils.ProductIndexEsField; |
4 | import com.yoho.search.recall.scene.constants.RecallConstants; | 5 | import com.yoho.search.recall.scene.constants.RecallConstants; |
5 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; | 6 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; |
@@ -61,4 +62,11 @@ public class BrandReducePriceStrategy implements IStrategy { | @@ -61,4 +62,11 @@ public class BrandReducePriceStrategy implements IStrategy { | ||
61 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; | 62 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; |
62 | } | 63 | } |
63 | 64 | ||
65 | + @Override | ||
66 | + public String strategyCacheKey() { | ||
67 | + StringBuilder sb = defaultStrategyKey(); | ||
68 | + sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds)); | ||
69 | + return sb.toString(); | ||
70 | + } | ||
71 | + | ||
64 | } | 72 | } |
@@ -52,4 +52,10 @@ public class CommonDirectTrainStrategy implements IStrategy { | @@ -52,4 +52,10 @@ public class CommonDirectTrainStrategy implements IStrategy { | ||
52 | return RecallConstants.CACHE_TIME_IN_SECOND_COMMON; | 52 | return RecallConstants.CACHE_TIME_IN_SECOND_COMMON; |
53 | } | 53 | } |
54 | 54 | ||
55 | + @Override | ||
56 | + public String strategyCacheKey() { | ||
57 | + StringBuilder sb = defaultStrategyKey(); | ||
58 | + return sb.toString(); | ||
59 | + } | ||
60 | + | ||
55 | } | 61 | } |
@@ -52,4 +52,10 @@ public class CommonFirstSknStrategy implements IStrategy { | @@ -52,4 +52,10 @@ public class CommonFirstSknStrategy implements IStrategy { | ||
52 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; | 52 | return RecallConstants.CACHE_TIME_IN_SECOND_BRAND; |
53 | } | 53 | } |
54 | 54 | ||
55 | + @Override | ||
56 | + public String strategyCacheKey() { | ||
57 | + StringBuilder sb = defaultStrategyKey(); | ||
58 | + return sb.toString(); | ||
59 | + } | ||
60 | + | ||
55 | } | 61 | } |
@@ -46,4 +46,10 @@ public class CommonHeatValueStrategy implements IStrategy { | @@ -46,4 +46,10 @@ public class CommonHeatValueStrategy implements IStrategy { | ||
46 | return RecallConstants.CACHE_TIME_IN_SECOND_COMMON; | 46 | return RecallConstants.CACHE_TIME_IN_SECOND_COMMON; |
47 | } | 47 | } |
48 | 48 | ||
49 | + @Override | ||
50 | + public String strategyCacheKey() { | ||
51 | + StringBuilder sb = defaultStrategyKey(); | ||
52 | + return sb.toString(); | ||
53 | + } | ||
54 | + | ||
49 | } | 55 | } |
@@ -55,4 +55,10 @@ public class CommonNewShopStrategy implements IStrategy { | @@ -55,4 +55,10 @@ public class CommonNewShopStrategy implements IStrategy { | ||
55 | return RecallConstants.CACHE_TIME_IN_SECOND_COMMON; | 55 | return RecallConstants.CACHE_TIME_IN_SECOND_COMMON; |
56 | } | 56 | } |
57 | 57 | ||
58 | + @Override | ||
59 | + public String strategyCacheKey() { | ||
60 | + StringBuilder sb = defaultStrategyKey(); | ||
61 | + return sb.toString(); | ||
62 | + } | ||
63 | + | ||
58 | } | 64 | } |
1 | package com.yoho.search.recall.scene.strategy.impls; | 1 | package com.yoho.search.recall.scene.strategy.impls; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.yoho.search.base.utils.ProductIndexEsField; | 4 | import com.yoho.search.base.utils.ProductIndexEsField; |
4 | import com.yoho.search.recall.scene.constants.RecallConstants; | 5 | import com.yoho.search.recall.scene.constants.RecallConstants; |
5 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; | 6 | import com.yoho.search.recall.scene.helper.SortBuilderHelper; |
@@ -51,4 +52,11 @@ public class SortPriceStrategy implements IStrategy { | @@ -51,4 +52,11 @@ public class SortPriceStrategy implements IStrategy { | ||
51 | public int cacheTimeInSecond() { | 52 | public int cacheTimeInSecond() { |
52 | return RecallConstants.CACHE_TIME_IN_SECOND_SORT_PRICE; | 53 | return RecallConstants.CACHE_TIME_IN_SECOND_SORT_PRICE; |
53 | } | 54 | } |
55 | + | ||
56 | + @Override | ||
57 | + public String strategyCacheKey() { | ||
58 | + StringBuilder sb = defaultStrategyKey(); | ||
59 | + sb.append(this.sortPriceArea==null?"": JSON.toJSONString(sortPriceArea)); | ||
60 | + return sb.toString(); | ||
61 | + } | ||
54 | } | 62 | } |
-
Please register or login to post a comment