Authored by Gino Zhang

增加两个获取个性化搜索数据的接口

... ... @@ -15,10 +15,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Service
public class UserFeaturesRedis {
... ... @@ -149,4 +146,30 @@ public class UserFeaturesRedis {
}
return fbRelas;
}
/**
* 返回大数据pageId权重信息,用于调试和问题定位。
* @param pageId 页面ID,非法的ID则查询全部
* @return 大数据pageId权重信息
*/
public Map<String, Map<String, String>> getBigDataPageBoosts(String pageId) {
Map<String, Map<String, String>> pageBoosts = new HashMap<String, Map<String, String>>();
if(Arrays.asList(PConsts.pageIds).contains(pageId)) {
// 查询单独的pageId
String key = PConsts.PAGE_BOOSTS_SUFFIX + pageId;
pageBoosts.put(pageId, redisOper.entries(key));
return pageBoosts;
}
for (int i = 0; i < PConsts.pageIds.length; ++i) {
String key = PConsts.PAGE_BOOSTS_SUFFIX + PConsts.pageIds[i];
pageBoosts.put(PConsts.pageIds[i], redisOper.entries(key));
}
return pageBoosts;
}
public Map<String, String> getBigDataUserBoost(String uid) {
String key = PConsts.USER_FEATURE_SUFFIX + uid;
return redisOper.entries(key);
}
}
... ...
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));
}
}
... ...