搜索时支持从conversion搜索提取推荐词
Showing
1 changed file
with
57 additions
and
2 deletions
@@ -247,11 +247,27 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl | @@ -247,11 +247,27 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl | ||
247 | return null; | 247 | return null; |
248 | } | 248 | } |
249 | 249 | ||
250 | - // 5) 将suggest分为两类:一类是从query分词出来的单个term 一类是从suggest获取的优先级较高的keyword | ||
251 | - List<String> resultTerms = new ArrayList<>(); | 250 | + final List<String> resultTerms = new ArrayList<>(); |
251 | + if (CollectionUtils.isNotEmpty(searchResult.getResultList())) { | ||
252 | + // 5.1) 从suggest获取的keyword列表 | ||
252 | searchResult.getResultList().forEach(map -> { | 253 | searchResult.getResultList().forEach(map -> { |
253 | resultTerms.add((String) map.get("keyword")); | 254 | resultTerms.add((String) map.get("keyword")); |
254 | }); | 255 | }); |
256 | + } else { | ||
257 | + // 5.2) 从conversion获取keyword列表 | ||
258 | + String dest = getTermsBySuggestConversion(queryWord); | ||
259 | + if (StringUtils.isNotEmpty(dest)) { | ||
260 | + int count = 0; | ||
261 | + for (String keyword : dest.split(",")) { | ||
262 | + count++; | ||
263 | + resultTerms.add(keyword); | ||
264 | + // 最多返回五个推荐词 | ||
265 | + if (count == 5) { | ||
266 | + break; | ||
267 | + } | ||
268 | + } | ||
269 | + } | ||
270 | + } | ||
255 | 271 | ||
256 | // 6) 加入缓存 | 272 | // 6) 加入缓存 |
257 | suggestResult = new JSONObject(); | 273 | suggestResult = new JSONObject(); |
@@ -265,6 +281,45 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl | @@ -265,6 +281,45 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl | ||
265 | } | 281 | } |
266 | } | 282 | } |
267 | 283 | ||
284 | + private String getTermsBySuggestConversion(String queryWord) { | ||
285 | + long begin = System.currentTimeMillis(); | ||
286 | + logger.info("[func=getTermsBySuggestConversion][query={}][begin={}]", queryWord, begin); | ||
287 | + | ||
288 | + // 2) 根据terms搜索构造搜索请求 | ||
289 | + SearchParam searchParam = new SearchParam(); | ||
290 | + MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("source", queryWord); | ||
291 | + searchParam.setQuery(queryBuilder); | ||
292 | + searchParam.setPage(1); | ||
293 | + searchParam.setSize(1); | ||
294 | + | ||
295 | + // 3) 先从缓存中获取 | ||
296 | + final String indexName = ISearchConstants.INDEX_NAME_CONVERSION; | ||
297 | + JSONObject suggestResult = searchCacheService.getJSONObjectFromCache(indexName, searchParam); | ||
298 | + if (suggestResult != null) { | ||
299 | + CACHE_MATCH_REQUEST.info("match cache for product list terms conversion, keyword = {}.", queryWord); | ||
300 | + return suggestResult.getString("dest"); | ||
301 | + } | ||
302 | + | ||
303 | + // 4) 调用ES执行搜索 | ||
304 | + SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam); | ||
305 | + if (searchResult == null) { | ||
306 | + return null; | ||
307 | + } | ||
308 | + | ||
309 | + String dest = ""; | ||
310 | + if (CollectionUtils.isNotEmpty(searchResult.getResultList())) { | ||
311 | + // 5) 从conversion获取的keyword列表 | ||
312 | + dest = (String) searchResult.getResultList().get(0).get("dest"); | ||
313 | + } | ||
314 | + | ||
315 | + // 6) 加入缓存 | ||
316 | + suggestResult = new JSONObject(); | ||
317 | + suggestResult.put("dest", dest); | ||
318 | + searchCacheService.addJSONObjectToCache(indexName, searchParam, suggestResult); | ||
319 | + logger.info("[func=getTermsBySuggestConversion][query={}][cost={}]", queryWord, System.currentTimeMillis() - begin); | ||
320 | + return dest; | ||
321 | + } | ||
322 | + | ||
268 | private SearchParam buildSearchParamForTerms(Map<String, String> paramMap) { | 323 | private SearchParam buildSearchParamForTerms(Map<String, String> paramMap) { |
269 | final String countField = getCountField(paramMap); | 324 | final String countField = getCountField(paramMap); |
270 | SearchParam searchParam = new SearchParam(); | 325 | SearchParam searchParam = new SearchParam(); |
-
Please register or login to post a comment