Authored by Gino Zhang

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

package com.yoho.search.service.restapi;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.yoho.search.service.downgrade.aop.DownGradeAble;
import com.yoho.search.service.servicenew.ISuggestService;
import com.yoho.search.service.utils.HttpServletRequestUtils;
import com.yoho.search.service.vo.SearchApiResult;
import com.yoho.search.service.vo.SuggestApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yoho.search.service.downgrade.aop.DownGradeAble;
import com.yoho.search.service.servicenew.ISuggestService;
import com.yoho.search.service.utils.HttpServletRequestUtils;
import com.yoho.search.service.vo.SuggestApiResult;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Controller
public class SuggestController {
... ... @@ -34,4 +33,15 @@ public class SuggestController {
return suggestService.suggest(paramMap);
}
/**
* 搜索建议接口
*
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "tools/suggestConversion")
@ResponseBody
public SearchApiResult suggestConversion(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return suggestService.suggestConversion(paramMap);
}
}
... ...
package com.yoho.search.service.servicenew;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.service.vo.SearchApiResult;
import com.yoho.search.service.vo.SuggestApiResult;
import java.util.Map;
... ... @@ -22,4 +23,11 @@ public interface ISuggestService {
* @return 包括term建议和phrase建议。
*/
JSONObject suggestByTerms(Map<String, String> paramMap);
/**
* 根据关键词和时间查询关键词转换关系
* @param paramMap 查询参数
* @return 满足查询要求的转换关系
*/
SearchApiResult suggestConversion(Map<String, String> paramMap);
}
... ...
... ... @@ -12,6 +12,7 @@ import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.servicenew.ISuggestService;
import com.yoho.search.service.utils.HttpServletRequestUtils;
import com.yoho.search.service.utils.SearchRequestParams;
import com.yoho.search.service.vo.SearchApiResult;
import com.yoho.search.service.vo.SuggestApiResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
... ... @@ -259,7 +260,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
if (StringUtils.isNotEmpty(dest)) {
int count = 0;
for (String keyword : dest.split(",")) {
if(!queryWord.equalsIgnoreCase(keyword)) {
if (!queryWord.equalsIgnoreCase(keyword)) {
count++;
resultTerms.add(keyword);
// 最多返回三个推荐词
... ... @@ -293,6 +294,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
searchParam.setQuery(queryBuilder);
searchParam.setPage(1);
searchParam.setSize(1);
searchParam.setFiter(QueryBuilders.termQuery("status", 1));
// 3) 先从缓存中获取
final String indexName = ISearchConstants.INDEX_NAME_CONVERSION;
... ... @@ -343,4 +345,42 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
searchParam.setFiter(QueryBuilders.rangeQuery(countField).gte(20));
return searchParam;
}
@Override
public SearchApiResult suggestConversion(Map<String, String> paramMap) {
try {
long begin = System.currentTimeMillis();
logger.info("[func=suggestConversion][param={}][begin={}]", paramMap, begin);
String queryWord = paramMap.get("query");
int updateTime = StringUtils.isBlank(paramMap.get("updateTime")) ? 0 : Integer.parseInt(paramMap.get("updateTime"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1) {
throw new IllegalArgumentException("分页参数不合法");
}
// 1) 构建ES请求
SearchParam searchParam = new SearchParam();
QueryBuilder queryBuilder = StringUtils.isNotEmpty(queryWord) ? QueryBuilders.matchQuery("source", queryWord) : QueryBuilders.matchAllQuery();
searchParam.setQuery(queryBuilder);
searchParam.setPage(page);
searchParam.setSize(1);
if (updateTime > 0) {
searchParam.setFiter(QueryBuilders.rangeQuery("updateTime").gte(updateTime));
}
// 2) 调用ES查询
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CONVERSION, searchParam);
if (searchResult == null) {
return null;
}
// 3) 返回结果
logger.info("[func=suggestConversion][cost={}]", System.currentTimeMillis() - begin);
return new SearchApiResult().setData(searchResult.getResultList());
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);
}
}
}
... ...