Authored by Gino Zhang

service增加给platform调用查询conversion索引的接口

1 package com.yoho.search.service.restapi; 1 package com.yoho.search.service.restapi;
2 2
3 -import java.util.Map;  
4 -  
5 -import javax.servlet.http.HttpServletRequest;  
6 - 3 +import com.yoho.search.service.downgrade.aop.DownGradeAble;
  4 +import com.yoho.search.service.servicenew.ISuggestService;
  5 +import com.yoho.search.service.utils.HttpServletRequestUtils;
  6 +import com.yoho.search.service.vo.SearchApiResult;
  7 +import com.yoho.search.service.vo.SuggestApiResult;
7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Controller; 9 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod; 11 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.ResponseBody; 12 import org.springframework.web.bind.annotation.ResponseBody;
12 13
13 -import com.yoho.search.service.downgrade.aop.DownGradeAble;  
14 -import com.yoho.search.service.servicenew.ISuggestService;  
15 -import com.yoho.search.service.utils.HttpServletRequestUtils;  
16 -import com.yoho.search.service.vo.SuggestApiResult; 14 +import javax.servlet.http.HttpServletRequest;
  15 +import java.util.Map;
17 16
18 @Controller 17 @Controller
19 public class SuggestController { 18 public class SuggestController {
@@ -34,4 +33,15 @@ public class SuggestController { @@ -34,4 +33,15 @@ public class SuggestController {
34 return suggestService.suggest(paramMap); 33 return suggestService.suggest(paramMap);
35 } 34 }
36 35
  36 + /**
  37 + * 搜索建议接口
  38 + *
  39 + * @return
  40 + */
  41 + @RequestMapping(method = RequestMethod.GET, value = "tools/suggestConversion")
  42 + @ResponseBody
  43 + public SearchApiResult suggestConversion(HttpServletRequest request) {
  44 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  45 + return suggestService.suggestConversion(paramMap);
  46 + }
37 } 47 }
1 package com.yoho.search.service.servicenew; 1 package com.yoho.search.service.servicenew;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.service.vo.SearchApiResult;
4 import com.yoho.search.service.vo.SuggestApiResult; 5 import com.yoho.search.service.vo.SuggestApiResult;
5 6
6 import java.util.Map; 7 import java.util.Map;
@@ -22,4 +23,11 @@ public interface ISuggestService { @@ -22,4 +23,11 @@ public interface ISuggestService {
22 * @return 包括term建议和phrase建议。 23 * @return 包括term建议和phrase建议。
23 */ 24 */
24 JSONObject suggestByTerms(Map<String, String> paramMap); 25 JSONObject suggestByTerms(Map<String, String> paramMap);
  26 +
  27 + /**
  28 + * 根据关键词和时间查询关键词转换关系
  29 + * @param paramMap 查询参数
  30 + * @return 满足查询要求的转换关系
  31 + */
  32 + SearchApiResult suggestConversion(Map<String, String> paramMap);
25 } 33 }
@@ -12,6 +12,7 @@ import com.yoho.search.service.service.SearchCommonService; @@ -12,6 +12,7 @@ import com.yoho.search.service.service.SearchCommonService;
12 import com.yoho.search.service.servicenew.ISuggestService; 12 import com.yoho.search.service.servicenew.ISuggestService;
13 import com.yoho.search.service.utils.HttpServletRequestUtils; 13 import com.yoho.search.service.utils.HttpServletRequestUtils;
14 import com.yoho.search.service.utils.SearchRequestParams; 14 import com.yoho.search.service.utils.SearchRequestParams;
  15 +import com.yoho.search.service.vo.SearchApiResult;
15 import com.yoho.search.service.vo.SuggestApiResult; 16 import com.yoho.search.service.vo.SuggestApiResult;
16 import org.apache.commons.collections.CollectionUtils; 17 import org.apache.commons.collections.CollectionUtils;
17 import org.apache.commons.lang.StringUtils; 18 import org.apache.commons.lang.StringUtils;
@@ -259,7 +260,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -259,7 +260,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
259 if (StringUtils.isNotEmpty(dest)) { 260 if (StringUtils.isNotEmpty(dest)) {
260 int count = 0; 261 int count = 0;
261 for (String keyword : dest.split(",")) { 262 for (String keyword : dest.split(",")) {
262 - if(!queryWord.equalsIgnoreCase(keyword)) { 263 + if (!queryWord.equalsIgnoreCase(keyword)) {
263 count++; 264 count++;
264 resultTerms.add(keyword); 265 resultTerms.add(keyword);
265 // 最多返回三个推荐词 266 // 最多返回三个推荐词
@@ -293,6 +294,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -293,6 +294,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
293 searchParam.setQuery(queryBuilder); 294 searchParam.setQuery(queryBuilder);
294 searchParam.setPage(1); 295 searchParam.setPage(1);
295 searchParam.setSize(1); 296 searchParam.setSize(1);
  297 + searchParam.setFiter(QueryBuilders.termQuery("status", 1));
296 298
297 // 3) 先从缓存中获取 299 // 3) 先从缓存中获取
298 final String indexName = ISearchConstants.INDEX_NAME_CONVERSION; 300 final String indexName = ISearchConstants.INDEX_NAME_CONVERSION;
@@ -343,4 +345,42 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -343,4 +345,42 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
343 searchParam.setFiter(QueryBuilders.rangeQuery(countField).gte(20)); 345 searchParam.setFiter(QueryBuilders.rangeQuery(countField).gte(20));
344 return searchParam; 346 return searchParam;
345 } 347 }
  348 +
  349 + @Override
  350 + public SearchApiResult suggestConversion(Map<String, String> paramMap) {
  351 + try {
  352 + long begin = System.currentTimeMillis();
  353 + logger.info("[func=suggestConversion][param={}][begin={}]", paramMap, begin);
  354 +
  355 + String queryWord = paramMap.get("query");
  356 + int updateTime = StringUtils.isBlank(paramMap.get("updateTime")) ? 0 : Integer.parseInt(paramMap.get("updateTime"));
  357 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  358 + if (page < 1) {
  359 + throw new IllegalArgumentException("分页参数不合法");
  360 + }
  361 +
  362 + // 1) 构建ES请求
  363 + SearchParam searchParam = new SearchParam();
  364 + QueryBuilder queryBuilder = StringUtils.isNotEmpty(queryWord) ? QueryBuilders.matchQuery("source", queryWord) : QueryBuilders.matchAllQuery();
  365 + searchParam.setQuery(queryBuilder);
  366 + searchParam.setPage(page);
  367 + searchParam.setSize(1);
  368 + if (updateTime > 0) {
  369 + searchParam.setFiter(QueryBuilders.rangeQuery("updateTime").gte(updateTime));
  370 + }
  371 +
  372 + // 2) 调用ES查询
  373 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CONVERSION, searchParam);
  374 + if (searchResult == null) {
  375 + return null;
  376 + }
  377 +
  378 + // 3) 返回结果
  379 + logger.info("[func=suggestConversion][cost={}]", System.currentTimeMillis() - begin);
  380 + return new SearchApiResult().setData(searchResult.getResultList());
  381 + } catch (Exception e) {
  382 + logger.error(e.getMessage(), e);
  383 + return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);
  384 + }
  385 + }
346 } 386 }