Authored by hugufei

每种策略根据参数定义自己的缓存key,不依赖es的报文toString

... ... @@ -3,6 +3,7 @@ package com.yoho.search.recall.scene.models;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.recall.scene.strategy.IStrategy;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
... ... @@ -18,37 +19,26 @@ import java.util.List;
public class RecallRequest implements IRecallRequest {
private ParamQueryFilter paramQueryFilter;
private QueryBuilder extendFilter;
private List<SortBuilder<?>> sortBuilders;
private Integer size;
private String requestType;
private IStrategy strategy;
private RedisKeyBuilder redisKeyBuilder;
private int cacheTimeInSecond;
public RecallRequest(ParamQueryFilter paramQueryFilter, QueryBuilder extendFilter, SortBuilder<?> sortBuilder, Integer size, String requestType, int cacheTimeInSecond) {
public RecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) {
this.paramQueryFilter = paramQueryFilter;
this.extendFilter = extendFilter;
this.sortBuilders = Arrays.asList(sortBuilder);
this.size = size;
this.requestType = requestType;
this.cacheTimeInSecond = cacheTimeInSecond;
this.strategy = strategy;
this.redisKeyBuilder = genRedisKeyBuilder();
}
private RedisKeyBuilder genRedisKeyBuilder() {
StringBuilder sb = new StringBuilder();
sb.append("paramMd5Key:").append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key());
sb.append("extendFilter:").append(extendFilter == null ? "" : extendFilter.toString());
sb.append("sortBuilders:").append(sortBuilders == null ? "" : sortBuilders.toString());
sb.append("size:").append(size == null ? "0" : size.toString());
sb.append("cacheTimeInSecond:").append(cacheTimeInSecond);
sb.append("strategyCacheKey:").append(strategy == null ? "" : strategy.strategyCacheKey());
String cacheKey = MD5Util.string2MD5(sb.toString());
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendVar(requestType).appendFixed(":").appendVar(cacheKey);
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("RECALL:").appendVar(cacheKey);
}
@Override
public String requestType() {
return this.requestType;
return this.strategy.nameEnum().name();
}
@Override
... ... @@ -58,7 +48,7 @@ public class RecallRequest implements IRecallRequest {
@Override
public int cacheTimeInSecond() {
return this.cacheTimeInSecond;
return this.strategy.cacheTimeInSecond();
}
@Override
... ... @@ -67,19 +57,19 @@ public class RecallRequest implements IRecallRequest {
searchParam.setQuery(this.paramQueryFilter.getParamQuery());
searchParam.setFiter(this.getRealFilter());
searchParam.setIncludeFields(this.includeFields());
searchParam.setSortBuilders(this.sortBuilders);
searchParam.setSortBuilders(Arrays.asList(this.strategy.sortBuilder()));
searchParam.setOffset(0);
searchParam.setSize(this.size);
searchParam.setSize(this.strategy.size());
return searchParam;
}
private QueryBuilder getRealFilter() {
if (extendFilter == null) {
if (this.strategy==null || strategy.extendFilter() == null) {
return this.paramQueryFilter.getParamFilter();
}
BoolQueryBuilder realFilter = QueryBuilders.boolQuery();
realFilter.must(this.paramQueryFilter.getParamFilter());
realFilter.must(this.extendFilter);
realFilter.must(this.strategy.extendFilter());
return realFilter;
}
... ...
... ... @@ -7,7 +7,7 @@ import com.yoho.search.recall.scene.strategy.IStrategy;
public abstract class BaseRecallRequest {
protected RecallRequest buildRecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) {
return new RecallRequest(paramQueryFilter, strategy.extendFilter(), strategy.sortBuilder(), strategy.size(), strategy.nameEnum().name(),strategy.cacheTimeInSecond());
return new RecallRequest(paramQueryFilter, strategy);
}
}
... ...
... ... @@ -13,6 +13,16 @@ public interface IStrategy {
SortBuilder<?> sortBuilder();// 排序策略
String strategyCacheKey();
int cacheTimeInSecond();//缓存时间
default StringBuilder defaultStrategyKey(){
StringBuilder sb = new StringBuilder();
sb.append(this.nameEnum().name());
sb.append(this.cacheTimeInSecond());
sb.append(this.size());
return sb;
}
}
... ...
package com.yoho.search.recall.scene.strategy.impls;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.recall.scene.constants.RecallConstants;
import com.yoho.search.recall.scene.helper.SortBuilderHelper;
... ... @@ -61,4 +62,11 @@ public class BrandHeatValueStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_BRAND;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds));
return sb.toString();
}
}
... ...
package com.yoho.search.recall.scene.strategy.impls;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.recall.scene.constants.RecallConstants;
import com.yoho.search.recall.scene.helper.SortBuilderHelper;
... ... @@ -60,4 +61,11 @@ public class BrandNewShelveStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_BRAND;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds));
return sb.toString();
}
}
... ...
package com.yoho.search.recall.scene.strategy.impls;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.recall.scene.constants.RecallConstants;
... ... @@ -78,4 +79,11 @@ public class BrandPromotionStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_BRAND;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds));
return sb.toString();
}
}
... ...
package com.yoho.search.recall.scene.strategy.impls;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.recall.scene.constants.RecallConstants;
import com.yoho.search.recall.scene.helper.SortBuilderHelper;
... ... @@ -61,4 +62,11 @@ public class BrandReducePriceStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_BRAND;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
sb.append(this.brandIds==null?"": JSON.toJSONString(this.brandIds));
return sb.toString();
}
}
... ...
... ... @@ -52,4 +52,10 @@ public class CommonDirectTrainStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_COMMON;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
return sb.toString();
}
}
... ...
... ... @@ -52,4 +52,10 @@ public class CommonFirstSknStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_BRAND;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
return sb.toString();
}
}
... ...
... ... @@ -46,4 +46,10 @@ public class CommonHeatValueStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_COMMON;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
return sb.toString();
}
}
... ...
... ... @@ -55,4 +55,10 @@ public class CommonNewShopStrategy implements IStrategy {
return RecallConstants.CACHE_TIME_IN_SECOND_COMMON;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
return sb.toString();
}
}
... ...
package com.yoho.search.recall.scene.strategy.impls;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.recall.scene.constants.RecallConstants;
import com.yoho.search.recall.scene.helper.SortBuilderHelper;
... ... @@ -51,4 +52,11 @@ public class SortPriceStrategy implements IStrategy {
public int cacheTimeInSecond() {
return RecallConstants.CACHE_TIME_IN_SECOND_SORT_PRICE;
}
@Override
public String strategyCacheKey() {
StringBuilder sb = defaultStrategyKey();
sb.append(this.sortPriceArea==null?"": JSON.toJSONString(sortPriceArea));
return sb.toString();
}
}
... ...