1
|
package com.yoho.search.service.servicenew.impl;
|
1
|
package com.yoho.search.service.servicenew.impl;
|
2
|
|
2
|
|
3
|
-import java.util.ArrayList;
|
|
|
4
|
-import java.util.Arrays;
|
|
|
5
|
-import java.util.HashMap;
|
|
|
6
|
-import java.util.List;
|
|
|
7
|
-import java.util.Map;
|
|
|
8
|
-import java.util.Set;
|
|
|
9
|
-import java.util.stream.Collectors;
|
|
|
10
|
-
|
|
|
11
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
12
|
-import org.apache.commons.lang.StringUtils;
|
|
|
13
|
-import org.elasticsearch.common.lucene.search.function.CombineFunction;
|
|
|
14
|
-import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
15
|
-import org.elasticsearch.index.query.MatchQueryBuilder;
|
|
|
16
|
-import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
|
|
17
|
-import org.elasticsearch.index.query.QueryBuilder;
|
|
|
18
|
-import org.elasticsearch.index.query.QueryBuilders;
|
|
|
19
|
-import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
|
|
20
|
-import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
|
|
21
|
-import org.elasticsearch.search.sort.SortBuilder;
|
|
|
22
|
-import org.elasticsearch.search.sort.SortBuilders;
|
|
|
23
|
-import org.elasticsearch.search.sort.SortOrder;
|
|
|
24
|
-import org.slf4j.Logger;
|
|
|
25
|
-import org.slf4j.LoggerFactory;
|
|
|
26
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
27
|
-import org.springframework.stereotype.Component;
|
|
|
28
|
-import org.springframework.util.Assert;
|
|
|
29
|
-
|
|
|
30
|
import com.alibaba.fastjson.JSONObject;
|
3
|
import com.alibaba.fastjson.JSONObject;
|
31
|
import com.yoho.search.base.utils.CharUtils;
|
4
|
import com.yoho.search.base.utils.CharUtils;
|
32
|
import com.yoho.search.base.utils.ISearchConstants;
|
5
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
@@ -43,6 +16,23 @@ import com.yoho.search.service.servicenew.ISearchRecommendService; |
|
@@ -43,6 +16,23 @@ import com.yoho.search.service.servicenew.ISearchRecommendService; |
43
|
import com.yoho.search.service.servicenew.ISuggestService;
|
16
|
import com.yoho.search.service.servicenew.ISuggestService;
|
44
|
import com.yoho.search.service.utils.SearchRequestParams;
|
17
|
import com.yoho.search.service.utils.SearchRequestParams;
|
45
|
import com.yoho.search.service.vo.SearchApiResult;
|
18
|
import com.yoho.search.service.vo.SearchApiResult;
|
|
|
19
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
20
|
+import org.apache.commons.lang.StringUtils;
|
|
|
21
|
+import org.elasticsearch.common.lucene.search.function.CombineFunction;
|
|
|
22
|
+import org.elasticsearch.index.query.*;
|
|
|
23
|
+import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
|
|
24
|
+import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
|
|
25
|
+import org.elasticsearch.search.sort.SortBuilder;
|
|
|
26
|
+import org.elasticsearch.search.sort.SortBuilders;
|
|
|
27
|
+import org.elasticsearch.search.sort.SortOrder;
|
|
|
28
|
+import org.slf4j.Logger;
|
|
|
29
|
+import org.slf4j.LoggerFactory;
|
|
|
30
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
31
|
+import org.springframework.stereotype.Component;
|
|
|
32
|
+import org.springframework.util.Assert;
|
|
|
33
|
+
|
|
|
34
|
+import java.util.*;
|
|
|
35
|
+import java.util.stream.Collectors;
|
46
|
|
36
|
|
47
|
/**
|
37
|
/**
|
48
|
* Created by ginozhang on 2017/3/22.
|
38
|
* Created by ginozhang on 2017/3/22.
|
|
@@ -101,6 +91,7 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
|
@@ -101,6 +91,7 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
101
|
String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY);
|
91
|
String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY);
|
102
|
long begin = System.currentTimeMillis();
|
92
|
long begin = System.currentTimeMillis();
|
103
|
logger.info("[func=recommend][queryWord={}][begin={}]", queryWord, begin);
|
93
|
logger.info("[func=recommend][queryWord={}][begin={}]", queryWord, begin);
|
|
|
94
|
+ String caseType = "DEFAULT_CASE";
|
104
|
|
95
|
|
105
|
try {
|
96
|
try {
|
106
|
Assert.isTrue(StringUtils.isNotEmpty(queryWord), "query keyword cannot be empty.");
|
97
|
Assert.isTrue(StringUtils.isNotEmpty(queryWord), "query keyword cannot be empty.");
|
|
@@ -111,12 +102,14 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
|
@@ -111,12 +102,14 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
111
|
// 1) 第一部分是最常见的情况,包括有结果、根据SKN搜索、关键词未出现在空结果Redis ZSet里
|
102
|
// 1) 第一部分是最常见的情况,包括有结果、根据SKN搜索、关键词未出现在空结果Redis ZSet里
|
112
|
if (containsProductInSearchResult(searchResult)) {
|
103
|
if (containsProductInSearchResult(searchResult)) {
|
113
|
// 1.1) 搜索有结果的 优先从搜索结果聚合出品牌等关键词进行查询
|
104
|
// 1.1) 搜索有结果的 优先从搜索结果聚合出品牌等关键词进行查询
|
|
|
105
|
+ caseType = "BY_PRODUCT_LIST_CASE";
|
114
|
JSONObject dataMap = ((JSONObject) searchResult.getData());
|
106
|
JSONObject dataMap = ((JSONObject) searchResult.getData());
|
115
|
List<Map<String, Object>> productList = (List<Map<String, Object>>) dataMap.get("product_list");
|
107
|
List<Map<String, Object>> productList = (List<Map<String, Object>>) dataMap.get("product_list");
|
116
|
String aggKeywords = aggKeywordsByProductList(productList);
|
108
|
String aggKeywords = aggKeywordsByProductList(productList);
|
117
|
keywordsToSearch = keywordsToSearch + " " + aggKeywords;
|
109
|
keywordsToSearch = keywordsToSearch + " " + aggKeywords;
|
118
|
} else if (isQuerySkn(queryWord)) {
|
110
|
} else if (isQuerySkn(queryWord)) {
|
119
|
// 1.2) 如果是查询SKN 没有查询到的 后续的逻辑也无法推荐 所以直接到ES里去获取关键词
|
111
|
// 1.2) 如果是查询SKN 没有查询到的 后续的逻辑也无法推荐 所以直接到ES里去获取关键词
|
|
|
112
|
+ caseType = "BY_SKN_PARAMS_CASE";
|
120
|
keywordsToSearch = aggKeywordsBySkns(queryWord);
|
113
|
keywordsToSearch = aggKeywordsBySkns(queryWord);
|
121
|
if (StringUtils.isEmpty(keywordsToSearch)) {
|
114
|
if (StringUtils.isEmpty(keywordsToSearch)) {
|
122
|
return defaultSuggestRecommendation();
|
115
|
return defaultSuggestRecommendation();
|
|
@@ -141,12 +134,15 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
|
@@ -141,12 +134,15 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
141
|
String spellingCorrentWord = null, dest = null;
|
134
|
String spellingCorrentWord = null, dest = null;
|
142
|
if (allowGetingDest(queryWord) && StringUtils.isNotEmpty((dest = getSuggestConversionDestBySource(queryWord)))) {
|
135
|
if (allowGetingDest(queryWord) && StringUtils.isNotEmpty((dest = getSuggestConversionDestBySource(queryWord)))) {
|
143
|
// 2.1) 爬虫和自定义的Conversion处理
|
136
|
// 2.1) 爬虫和自定义的Conversion处理
|
|
|
137
|
+ caseType = "BY_CONVERSION_CASE";
|
144
|
keywordsToSearch = dest;
|
138
|
keywordsToSearch = dest;
|
145
|
} else if (allowSpellingCorrent(queryWord) && StringUtils.isNotEmpty((spellingCorrentWord = suggestService.getSpellingCorrectKeyword(queryWord)))) {
|
139
|
} else if (allowSpellingCorrent(queryWord) && StringUtils.isNotEmpty((spellingCorrentWord = suggestService.getSpellingCorrectKeyword(queryWord)))) {
|
146
|
// 2.2) 执行拼写检查 由于在搜索建议的时候会进行拼写检查 所以缓存命中率高
|
140
|
// 2.2) 执行拼写检查 由于在搜索建议的时候会进行拼写检查 所以缓存命中率高
|
|
|
141
|
+ caseType = "BY_SPELLING_CORRECT_CASE";
|
147
|
keywordsToSearch = spellingCorrentWord;
|
142
|
keywordsToSearch = spellingCorrentWord;
|
148
|
} else {
|
143
|
} else {
|
149
|
// 2.3) 如果两者都没有 则直接返回
|
144
|
// 2.3) 如果两者都没有 则直接返回
|
|
|
145
|
+ caseType = "WORST_CASE";
|
150
|
return defaultSuggestRecommendation();
|
146
|
return defaultSuggestRecommendation();
|
151
|
}
|
147
|
}
|
152
|
|
148
|
|
|
@@ -158,10 +154,11 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
|
@@ -158,10 +154,11 @@ public class SearchRecommendServiceImpl implements ISearchRecommendService { |
158
|
logger.info("[func=recommend][queryWord={}][cost={}]", queryWord, System.currentTimeMillis() - begin);
|
154
|
logger.info("[func=recommend][queryWord={}][cost={}]", queryWord, System.currentTimeMillis() - begin);
|
159
|
return recommendResult;
|
155
|
return recommendResult;
|
160
|
} catch (Exception e) {
|
156
|
} catch (Exception e) {
|
|
|
157
|
+ caseType = "EXCEPTION_CASE";
|
161
|
logger.error("[func=recommend][queryWord=" + queryWord + "]", e);
|
158
|
logger.error("[func=recommend][queryWord=" + queryWord + "]", e);
|
162
|
return defaultSuggestRecommendation();
|
159
|
return defaultSuggestRecommendation();
|
163
|
} finally {
|
160
|
} finally {
|
164
|
- searchKeyWordService.recordSuggestRecom(queryWord);
|
161
|
+ searchKeyWordService.recordSearchRecommendCase(caseType);
|
165
|
}
|
162
|
}
|
166
|
}
|
163
|
}
|
167
|
|
164
|
|