RecallRequest.java
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.yoho.search.models.recall;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.model.ICacheRequest;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.service.recall.strategy.IStrategy;
import com.yoho.search.service.recall.strategy.StrategyEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import java.util.Arrays;
/**
* 召回请求
*/
public class RecallRequest implements ICacheRequest, IRecallRequest {
private ParamQueryFilter paramQueryFilter;
private IStrategy strategy;
public RecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy) {
this.paramQueryFilter = paramQueryFilter;
this.strategy = strategy;
this.redisKeyBuilder = genRedisKeyBuilder();
}
private RedisKeyBuilder redisKeyBuilder;
public RecallRequest(ParamQueryFilter paramQueryFilter, IStrategy strategy,String extendId) {
this.paramQueryFilter = paramQueryFilter;
this.strategy = strategy;
this.redisKeyBuilder = genRedisKeyBuilder();
}
private RedisKeyBuilder genRedisKeyBuilder() {
StringBuilder sb = new StringBuilder();
sb.append("paramMd5Key:").append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key());
sb.append("strategyCacheKey:").append(strategy == null ? "" : strategy.strategyCacheKey());
String cacheKey = MD5Util.string2MD5(sb.toString());
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH:").appendFixed("RECALL:");
redisKeyBuilder.appendFixed(strategy.strategtEnum().name()).appendFixed(":");
redisKeyBuilder.appendVar(cacheTimeInMinute()).appendFixed(":");
redisKeyBuilder.appendVar(cacheKey);
return redisKeyBuilder;
}
@Override
public RedisKeyBuilder redisKeyBuilder() {
return this.redisKeyBuilder;
}
@Override
public int cacheTimeInMinute() {
return this.strategy.cacheTimeInMinute();
}
@Override
public IStrategy strategy() {
return strategy;
}
public StrategyEnum requestStrategy() {
return this.strategy.strategtEnum();
}
public String intervalKey(){
return this.strategy.intervalKey();
}
@Override
public SearchParam searchParam() {
SearchParam searchParam = new SearchParam();
if (paramQueryFilter != null && paramQueryFilter.getParamQuery() != null) {
searchParam.setQuery(this.paramQueryFilter.getParamQuery());
}
searchParam.setFiter(this.getRealFilter());
searchParam.setIncludeFields(this.includeFields());
searchParam.setSortBuilders(Arrays.asList(this.strategy.sortBuilder()));
searchParam.setOffset(0);
searchParam.setSize(this.strategy.size());
return searchParam;
}
private QueryBuilder getRealFilter() {
BoolQueryBuilder realFilter = QueryBuilders.boolQuery();
if (strategy != null && strategy.extendFilter() != null) {
realFilter.must(this.strategy.extendFilter());
}
if (paramQueryFilter != null && paramQueryFilter.getParamFilter() != null) {
realFilter.must(this.paramQueryFilter.getParamFilter());
}
if (realFilter.hasClauses()) {
return realFilter;
}
return null;
}
}