1
|
package com.yoho.search.service.scene.searchlike;
|
1
|
package com.yoho.search.service.scene.searchlike;
|
2
|
|
2
|
|
3
|
import com.alibaba.fastjson.JSONObject;
|
3
|
import com.alibaba.fastjson.JSONObject;
|
|
|
4
|
+import com.yoho.search.aop.cache.SearchCacheAble;
|
4
|
import com.yoho.search.base.utils.CollectionUtils;
|
5
|
import com.yoho.search.base.utils.CollectionUtils;
|
5
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
6
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
6
|
-import com.yoho.search.aop.cache.SearchCacheAble;
|
7
|
+import com.yoho.search.common.SearchRequestParams;
|
7
|
import com.yoho.search.core.es.model.SearchParam;
|
8
|
import com.yoho.search.core.es.model.SearchParam;
|
8
|
import com.yoho.search.models.SearchApiResult;
|
9
|
import com.yoho.search.models.SearchApiResult;
|
9
|
-import com.yoho.search.common.SearchRequestParams;
|
|
|
10
|
-import com.yoho.search.service.index.ProductIndexBaseService;
|
|
|
11
|
-
|
|
|
12
|
import com.yoho.search.service.helper.ProductListHelper;
|
10
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
11
|
+import com.yoho.search.service.index.BigDataSimilarSknImgAliIndexBaseService;
|
|
|
12
|
+import com.yoho.search.service.index.ProductIndexBaseService;
|
13
|
import org.apache.commons.lang.StringUtils;
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
14
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
15
|
import org.elasticsearch.index.query.QueryBuilder;
|
15
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
@@ -29,114 +29,152 @@ import java.util.Map; |
|
@@ -29,114 +29,152 @@ import java.util.Map; |
29
|
|
29
|
|
30
|
/**
|
30
|
/**
|
31
|
* 店铺内找相似
|
31
|
* 店铺内找相似
|
32
|
- *
|
32
|
+ *
|
33
|
* @author gufei.hu
|
33
|
* @author gufei.hu
|
34
|
*/
|
34
|
*/
|
35
|
@Service
|
35
|
@Service
|
36
|
public class SearchLikeInShopService {
|
36
|
public class SearchLikeInShopService {
|
37
|
|
37
|
|
38
|
- private static final Logger logger = LoggerFactory.getLogger(SearchLikeInShopService.class);
|
38
|
+ private static final Logger logger = LoggerFactory.getLogger(SearchLikeInShopService.class);
|
|
|
39
|
+
|
|
|
40
|
+ @Autowired
|
|
|
41
|
+ private SearchLikeHelper searchLikeHelper;
|
|
|
42
|
+ @Autowired
|
|
|
43
|
+ private ProductIndexBaseService productIndexBaseService;
|
|
|
44
|
+ @Autowired
|
|
|
45
|
+ private ProductListHelper productListHelper;
|
|
|
46
|
+ @Autowired
|
|
|
47
|
+ private BigDataSimilarSknImgAliIndexBaseService bigDataSimilarSknImgAliIndexBaseService;
|
|
|
48
|
+
|
|
|
49
|
+ /**
|
|
|
50
|
+ * 店铺内推荐
|
|
|
51
|
+ */
|
|
|
52
|
+ @SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP_NEW", includeParams = {"product_skn"})
|
|
|
53
|
+ public SearchApiResult searchLikeInShop(Map<String, String> paramMap) {
|
|
|
54
|
+ try {
|
|
|
55
|
+ // 1、获取参数
|
|
|
56
|
+ String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
|
|
|
57
|
+ if (StringUtils.isBlank(productSkn)) {
|
|
|
58
|
+ return new SearchApiResult().setCode(400).setMessage("请输入SKN");
|
|
|
59
|
+ }
|
|
|
60
|
+ // 2、检测分页参数【默认30条】
|
|
|
61
|
+ int viewNum = 30;
|
|
|
62
|
+ // 3、获取当前查询的SKN的基本信息
|
|
|
63
|
+ JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
64
|
+ if (productInfoInEs == null) {
|
|
|
65
|
+ return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
66
|
+ }
|
|
|
67
|
+ // 4、设置SearchParams
|
|
|
68
|
+ List<SearchParam> searchParams = new ArrayList<>();
|
|
|
69
|
+ //4.1图片相似skn
|
|
|
70
|
+ List<String> similarProductSknList = bigDataSimilarSknImgAliIndexBaseService.getSimilarProductSknListInShop(productSkn);
|
|
|
71
|
+ if (similarProductSknList != null && !similarProductSknList.isEmpty()) {
|
|
|
72
|
+ searchParams.add(this.builderSearchParamForSimilarImg(productInfoInEs, similarProductSknList, viewNum));
|
|
|
73
|
+ }
|
|
|
74
|
+ //4.2文字相似skn
|
|
|
75
|
+ searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), viewNum));
|
|
|
76
|
+ // 5、获取搜索结果[截取条数]
|
|
|
77
|
+ List<Map<String, Object>> tempProductList = new ArrayList<>();
|
|
|
78
|
+ //只包含文字相似
|
|
|
79
|
+ if (searchParams.size() == 1) {
|
|
|
80
|
+ tempProductList = searchLikeHelper.queryProductList(searchParams);
|
|
|
81
|
+ } else {
|
|
|
82
|
+ //包含图片+文字相似
|
|
|
83
|
+ tempProductList = searchLikeHelper.queryProductListWithSimilarImg(searchParams, similarProductSknList);
|
|
|
84
|
+ }
|
|
|
85
|
+ if (tempProductList.size() > viewNum) {
|
|
|
86
|
+ tempProductList = CollectionUtils.safeSubList(tempProductList, 0, viewNum);
|
|
|
87
|
+ }
|
|
|
88
|
+ //6、保留偶数
|
|
|
89
|
+ if (tempProductList.size() % 2 > 0) {
|
|
|
90
|
+ tempProductList = CollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
|
|
|
91
|
+ }
|
|
|
92
|
+ // 7、构造返回结果
|
|
|
93
|
+ List<Map<String, Object>> productListResults = new ArrayList<>();
|
|
|
94
|
+ if (!tempProductList.isEmpty()) {
|
|
|
95
|
+ productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
|
|
|
96
|
+ }
|
|
|
97
|
+ JSONObject result = new JSONObject();
|
|
|
98
|
+ result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
|
|
|
99
|
+ result.put("product_list", productListResults);
|
|
|
100
|
+ return new SearchApiResult().setData(result);
|
|
|
101
|
+ } catch (Exception e) {
|
|
|
102
|
+ logger.error(e.getMessage(), e);
|
|
|
103
|
+ return new SearchApiResult().setData(null).setMessage("searchLikeInShop Exception").setCode(500);
|
|
|
104
|
+ }
|
|
|
105
|
+ }
|
39
|
|
106
|
|
40
|
- @Autowired
|
|
|
41
|
- private SearchLikeHelper searchLikeHelper;
|
|
|
42
|
- @Autowired
|
|
|
43
|
- private ProductIndexBaseService productIndexBaseService;
|
|
|
44
|
- @Autowired
|
|
|
45
|
- private ProductListHelper productListHelper;
|
|
|
46
|
-
|
|
|
47
|
- /**
|
|
|
48
|
- * 店铺内推荐
|
|
|
49
|
- *
|
|
|
50
|
- * @param paramMap
|
|
|
51
|
- * @return
|
|
|
52
|
- */
|
|
|
53
|
- @SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP_NEW", includeParams = { "product_skn"})
|
|
|
54
|
- public SearchApiResult searchLikeInShop(Map<String, String> paramMap) {
|
|
|
55
|
- try {
|
|
|
56
|
- // 1、获取参数
|
|
|
57
|
- String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
|
|
|
58
|
- if (StringUtils.isBlank(productSkn)) {
|
|
|
59
|
- return new SearchApiResult().setCode(400).setMessage("请输入SKN");
|
|
|
60
|
- }
|
|
|
61
|
- // 2、检测分页参数【默认30条】
|
|
|
62
|
- int viewNum = 30;
|
|
|
63
|
- // 3、获取当前查询的SKN的基本信息
|
|
|
64
|
- JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
65
|
- if (productInfoInEs == null) {
|
|
|
66
|
- return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
67
|
- }
|
|
|
68
|
- // 4、设置SearchParams
|
|
|
69
|
- List<SearchParam> searchParams = new ArrayList<SearchParam>();
|
|
|
70
|
- searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), viewNum));
|
|
|
71
|
- // 5、获取搜索结果[截取条数]
|
|
|
72
|
- List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
|
|
|
73
|
- if (tempProductList.size() > viewNum) {
|
|
|
74
|
- tempProductList = CollectionUtils.safeSubList(tempProductList,0, viewNum);
|
|
|
75
|
- }
|
|
|
76
|
- //6、保留偶数
|
|
|
77
|
- if (tempProductList.size() % 2 > 0) {
|
|
|
78
|
- tempProductList = CollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
|
|
|
79
|
- }
|
|
|
80
|
- // 7、构造返回结果
|
|
|
81
|
- List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
|
|
|
82
|
- if (!tempProductList.isEmpty()) {
|
|
|
83
|
- productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
|
|
|
84
|
- }
|
|
|
85
|
- JSONObject result = new JSONObject();
|
|
|
86
|
- result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
|
|
|
87
|
- result.put("product_list", productListResults);
|
|
|
88
|
- return new SearchApiResult().setData(result);
|
|
|
89
|
- } catch (Exception e) {
|
|
|
90
|
- logger.error(e.getMessage(), e);
|
|
|
91
|
- return new SearchApiResult().setData(null).setMessage("searchLikeInShop Exception").setCode(500);
|
|
|
92
|
- }
|
|
|
93
|
- }
|
107
|
+ private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> productSkns, int pageSize) {
|
|
|
108
|
+ // 1、设置SearchParam
|
|
|
109
|
+ SearchParam searchParam = new SearchParam();
|
|
|
110
|
+ // 2)设置query和filter
|
|
|
111
|
+ searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, "20%"));
|
|
|
112
|
+ searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns));
|
|
|
113
|
+ // 3、设置排序规则[按打分排序]
|
|
|
114
|
+ List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
|
|
|
115
|
+ sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
|
|
|
116
|
+ searchParam.setSortBuilders(sortBuilders);
|
|
|
117
|
+ // 4、设置分页参数
|
|
|
118
|
+ searchParam.setOffset(0);
|
|
|
119
|
+ searchParam.setSize(pageSize);
|
|
|
120
|
+ // 5)设置返回的参数【节省带宽】
|
|
|
121
|
+ List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
122
|
+ searchParam.setIncludeFields(includeFields);
|
|
|
123
|
+ return searchParam;
|
|
|
124
|
+ }
|
94
|
|
125
|
|
95
|
- private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> productSkns, int pageSize) {
|
|
|
96
|
- // 1、设置SearchParam
|
|
|
97
|
- SearchParam searchParam = new SearchParam();
|
|
|
98
|
- // 2)设置query和filter
|
|
|
99
|
- searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, "20%"));
|
|
|
100
|
- searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns));
|
|
|
101
|
- // 3、设置排序规则[按打分排序]
|
|
|
102
|
- List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
|
|
|
103
|
- sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
|
|
|
104
|
- searchParam.setSortBuilders(sortBuilders);
|
|
|
105
|
- // 4、设置分页参数
|
|
|
106
|
- searchParam.setOffset(0);
|
|
|
107
|
- searchParam.setSize(pageSize);
|
|
|
108
|
- // 5)设置返回的参数【节省带宽】
|
|
|
109
|
- List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
110
|
- searchParam.setIncludeFields(includeFields);
|
|
|
111
|
- return searchParam;
|
|
|
112
|
- }
|
126
|
+ private QueryBuilder builderFilterBuilder(JSONObject productInfoInEs, List<String> notProductSkns) {
|
|
|
127
|
+ String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
128
|
+ boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
129
|
+ BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(notProductSkns, isGlobal);
|
|
|
130
|
+ // 1)设置此SKN相关的性别过滤条件
|
|
|
131
|
+ String gender = productInfoInEs.getString(ProductIndexEsField.gender);
|
|
|
132
|
+ List<String> genderList = searchLikeHelper.getGenderInfo(gender);
|
|
|
133
|
+ if (genderList != null && !genderList.isEmpty()) {
|
|
|
134
|
+ boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
|
|
|
135
|
+ }
|
|
|
136
|
+ // 2)设置品牌或店铺信息
|
|
|
137
|
+ Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
|
|
|
138
|
+ Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
|
|
|
139
|
+ if (searchLikeHelper.isLegalInteger(shopId)) {
|
|
|
140
|
+ boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
|
|
|
141
|
+ } else if (searchLikeHelper.isLegalInteger(brandId)) {
|
|
|
142
|
+ boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
|
|
|
143
|
+ }
|
|
|
144
|
+ return boolFilter;
|
|
|
145
|
+ }
|
113
|
|
146
|
|
114
|
- private QueryBuilder builderFilterBuilder(JSONObject productInfoInEs, List<String> notProductSkns) {
|
|
|
115
|
- String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
116
|
- boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
117
|
- BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(notProductSkns, isGlobal);
|
|
|
118
|
- // 1)设置此SKN相关的性别过滤条件
|
|
|
119
|
- String gender = productInfoInEs.getString(ProductIndexEsField.gender);
|
|
|
120
|
- List<String> genderList = searchLikeHelper.getGenderInfo(gender);
|
|
|
121
|
- if (genderList != null && !genderList.isEmpty()) {
|
|
|
122
|
- boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
|
|
|
123
|
- }
|
|
|
124
|
- // 2)设置品牌或店铺信息
|
|
|
125
|
- Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
|
|
|
126
|
- Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
|
|
|
127
|
- if (searchLikeHelper.isLegalInteger(shopId)) {
|
|
|
128
|
- boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
|
|
|
129
|
- } else if (searchLikeHelper.isLegalInteger(brandId)) {
|
|
|
130
|
- boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
|
|
|
131
|
- }
|
|
|
132
|
- return boolFilter;
|
|
|
133
|
- }
|
147
|
+ private QueryBuilder builderQueryBuilder(JSONObject productInfoInEs, String minimumShouldMatch) {
|
|
|
148
|
+ String queryString = searchLikeHelper.genYohoQueryStringWithBrandName(productInfoInEs);
|
|
|
149
|
+ String productFeatureFactor = productInfoInEs.getString(ProductIndexEsField.productFeatureFactor);
|
|
|
150
|
+ QueryBuilder queryBuilder = searchLikeHelper.genSearchLikeQueryBuilder(queryString, minimumShouldMatch, productFeatureFactor);
|
|
|
151
|
+ return queryBuilder;
|
|
|
152
|
+ }
|
134
|
|
153
|
|
135
|
- private QueryBuilder builderQueryBuilder(JSONObject productInfoInEs, String minimumShouldMatch) {
|
|
|
136
|
- String queryString = searchLikeHelper.genYohoQueryStringWithBrandName(productInfoInEs);
|
|
|
137
|
- String productFeatureFactor = productInfoInEs.getString(ProductIndexEsField.productFeatureFactor);
|
|
|
138
|
- QueryBuilder queryBuilder = searchLikeHelper.genSearchLikeQueryBuilder(queryString, minimumShouldMatch, productFeatureFactor);
|
|
|
139
|
- return queryBuilder;
|
|
|
140
|
- }
|
154
|
+ /**
|
|
|
155
|
+ * 根据阿里相似图片的skn构建SearchParam去查productindex
|
|
|
156
|
+ */
|
|
|
157
|
+ private SearchParam builderSearchParamForSimilarImg(JSONObject productInfoInEs, List<String> productSkns, int pageSize) {
|
|
|
158
|
+ // 1、设置SearchParam
|
|
|
159
|
+ SearchParam searchParam = new SearchParam();
|
|
|
160
|
+ // 2)设置filter
|
|
|
161
|
+ String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
162
|
+ boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
163
|
+ BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
|
|
|
164
|
+ boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
|
|
|
165
|
+ String gender = productInfoInEs.getString(ProductIndexEsField.gender);
|
|
|
166
|
+ List<String> genderList = searchLikeHelper.getGenderInfo(gender);
|
|
|
167
|
+ if (genderList != null && !genderList.isEmpty()) {
|
|
|
168
|
+ boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
|
|
|
169
|
+ }
|
|
|
170
|
+ searchParam.setFiter(boolFilter);
|
|
|
171
|
+ // 4、设置分页参数
|
|
|
172
|
+ searchParam.setOffset(0);
|
|
|
173
|
+ searchParam.setSize(pageSize);
|
|
|
174
|
+ // 5)设置返回的参数【节省带宽】
|
|
|
175
|
+ List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
176
|
+ searchParam.setIncludeFields(includeFields);
|
|
|
177
|
+ return searchParam;
|
|
|
178
|
+ }
|
141
|
|
179
|
|
142
|
} |
180
|
} |