BaseRecallService.java
3.87 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
102
103
package com.yoho.search.recall.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.recall.common.model.CommonRecallResult;
import com.yoho.search.recall.common.model.CommonRecallSkn;
@Component
public class BaseRecallService {
private static List<String> commonRecallFields = new ArrayList<String>();
@Autowired
protected SearchParamHelper searchParamHelper;
@Autowired
protected SearchCommonService searchCommonService;
@PostConstruct
void init() {
commonRecallFields.add(ProductIndexEsField.productSkn);
commonRecallFields.add(ProductIndexEsField.brandId);
commonRecallFields.add(ProductIndexEsField.smallSortId);
commonRecallFields.add(ProductIndexEsField.productFeatureFactor);
}
protected SearchParam buildSearchParam(Map<String, String> paramMap, BoolQueryBuilder mustFilter, List<SortBuilder<?>> sortBuilders, int size) throws Exception {
// 1、构建filter
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilter);
// 2、构建size
searchParam.setOffset(0);
searchParam.setSize(size);
// 3、设置返回字段
searchParam.setIncludeFields(commonRecallFields);
// 4、设置orde
searchParam.setSortBuilders(sortBuilders);
return searchParam;
}
protected int getViewNum(Map<String,String> paramMap){
return MapUtils.getIntValue(paramMap, "viewNum", 60);
}
/**
* 合并召回结果,默认最后一个包含了总数量
*
* @param searchResults
* @return
*/
protected CommonRecallResult getCommonRecallResult(List<SearchResult> searchResults) {
// 1、获取值
List<CommonRecallSkn> productList = new ArrayList<CommonRecallSkn>();
Set<Integer> existsProductSkn = new HashSet<Integer>();
for (SearchResult searchResult : searchResults) {
productList.addAll(this.getCommonRecallSknList(searchResult, existsProductSkn));
}
long totalCount = searchResults.get(searchResults.size() - 1).getTotal();
// 2、构造返回结果
CommonRecallResult commonRecallResults = new CommonRecallResult();
commonRecallResults.setTotalCount(totalCount);
commonRecallResults.setRecallSknList(productList);
return commonRecallResults;
}
private List<CommonRecallSkn> getCommonRecallSknList(SearchResult searchResult, Set<Integer> existsProductSkn) {
List<CommonRecallSkn> commonRecallSkns = new ArrayList<CommonRecallSkn>();
List<Map<String, Object>> productList = searchResult.getResultList();
for (Map<String, Object> product : productList) {
Integer productSkn = MapUtils.getIntValue(product, ProductIndexEsField.productSkn, 0);
if (existsProductSkn.contains(productSkn)) {
continue;
}
existsProductSkn.add(productSkn);
CommonRecallSkn commonRecallSkn = new CommonRecallSkn();
commonRecallSkn.setProductSkn(productSkn);
commonRecallSkn.setBrandId(MapUtils.getIntValue(product, ProductIndexEsField.brandId, 0));
commonRecallSkn.setSmallSortId(MapUtils.getIntValue(product, ProductIndexEsField.smallSortId, 0));
commonRecallSkn.setProductFeatureFactor(MapUtils.getString(product, ProductIndexEsField.productFeatureFactor, ""));
commonRecallSkns.add(commonRecallSkn);
}
return commonRecallSkns;
}
}