|
|
package com.yoho.search.service.restapi;
|
|
|
|
|
|
import com.yoho.search.service.utils.HttpServletRequestUtils;
|
|
|
import com.yoho.search.service.service.SearchServiceHelper;
|
|
|
import com.yoho.search.core.es.utils.SearchParamUtils;
|
|
|
import com.yoho.search.service.cache.LocalCacheService;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.service.personalized.UserFeaturesRedis;
|
|
|
import com.yoho.search.service.service.SearchKeyWordService;
|
|
|
import com.yoho.search.service.vo.KeyWordWithCount;
|
|
|
import com.yoho.search.service.vo.SearchApiResult;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -19,11 +15,8 @@ import org.springframework.web.bind.annotation.RequestMethod; |
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Controller
|
|
|
@RequestMapping(value = "/tools")
|
...
|
...
|
@@ -38,7 +31,7 @@ public class ToolsController { |
|
|
private LocalCacheService localCacheService;
|
|
|
|
|
|
@Autowired
|
|
|
private SearchServiceHelper searchServiceHelper;
|
|
|
private UserFeaturesRedis UserFeaturesRedis;
|
|
|
|
|
|
/**
|
|
|
* 获取热搜词结果
|
...
|
...
|
@@ -194,53 +187,15 @@ public class ToolsController { |
|
|
return new SearchApiResult().setMessage("清除本地缓存成功");
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/dsl")
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/pageBoost")
|
|
|
@ResponseBody
|
|
|
public SearchApiResult productList(HttpServletRequest request) throws Exception {
|
|
|
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
|
|
|
|
|
|
// 1)验证查询条数
|
|
|
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
if (page < 1 || pageSize < 0) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
|
|
|
}
|
|
|
if (pageSize > 500) {
|
|
|
pageSize = 500;
|
|
|
}
|
|
|
|
|
|
// 2)构建基本查询参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(searchServiceHelper.constructQueryBuilderForProductList(paramMap));
|
|
|
searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, null));
|
|
|
setHighlight(paramMap, searchParam);
|
|
|
searchParam.setAggregationBuilders(null);
|
|
|
searchParam.setPage(page);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
|
|
|
// 3)设置排序字段
|
|
|
// searchParam.setSortFields(searchServiceHelper.buildSort(paramMap));
|
|
|
searchParam.setSortBuilders(searchServiceHelper.buildSortList(paramMap));
|
|
|
|
|
|
// 4)设置查询结果返回字段
|
|
|
if (StringUtils.isNotBlank(paramMap.get("resultFields"))) {
|
|
|
String resultFields = paramMap.get("resultFields");
|
|
|
searchParam.setResultFields(Arrays.asList(resultFields.split(",")));
|
|
|
}
|
|
|
|
|
|
String dsl = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam).toString();
|
|
|
logger.info("[productListDSL]:{}", dsl);
|
|
|
dsl.replaceAll("\\n", "").replaceAll("\\t", "").replaceAll("\\r", "");
|
|
|
return new SearchApiResult().setMessage(dsl);
|
|
|
public SearchApiResult pageBoost(@RequestParam(defaultValue = "all") String pageId) throws Exception {
|
|
|
return new SearchApiResult().setData(UserFeaturesRedis.getBigDataPageBoosts(pageId));
|
|
|
}
|
|
|
|
|
|
private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) {
|
|
|
if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) {
|
|
|
searchParam.setHighlight(true);
|
|
|
List<String> highlightFields = new ArrayList<String>();
|
|
|
highlightFields.add("productName.productName_ansj");
|
|
|
searchParam.setHighlightFields(highlightFields);
|
|
|
}
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/userBoost")
|
|
|
@ResponseBody
|
|
|
public SearchApiResult userBoost(@RequestParam String uid) throws Exception {
|
|
|
return new SearchApiResult().setData(UserFeaturesRedis.getBigDataUserBoost(uid));
|
|
|
}
|
|
|
} |
...
|
...
|
|