Authored by wangnan9279

ufo

... ... @@ -56,4 +56,11 @@ public class UfoProductListController {
return ufoProductListService.recommendList(paramMap);
}
@RequestMapping(method = RequestMethod.GET, value = "/ufo/suggest")
@ResponseBody
public SearchApiResult suggest(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return ufoProductListService.suggest(paramMap);
}
}
... ...
... ... @@ -123,6 +123,17 @@ public class UfoSearchQueryHelper extends BaseService {
return queryBuilder;
}
public QueryBuilder constructSuggestQueryBuilder(Map<String, String> paramMap) {
// 0、处理查询关键字
String query = SearchKeyWordUtils.getUfoParamKeyword(paramMap, SearchRequestParams.UFOPRODUCTINDEX_SEARCH_QUERY);
if (StringUtils.isBlank(query)) {
return QueryBuilders.matchAllQuery();
}
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery(UfoProductIndexEsField.productName_ik, "*" + query + "*");
return wildcardQueryBuilder;
}
/**
* 构造排序方式
*/
... ...
... ... @@ -38,6 +38,15 @@ public class UfoProductIndexBaseService {
return results;
}
public List<Map<String, Object>> buildProductReturnInfoList(List<Map<String, Object>> productEsSourceList) {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (Map<String, Object> productEsSource : productEsSourceList) {
results.add(this.getProductMapFromEsSource(productEsSource));
}
return results;
}
public Map<String, Object> getProductMapFromEsSource(Map<String, Object> map) {
Map<String, Object> productMap = new HashMap<String, Object>();
productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0));
... ... @@ -47,12 +56,18 @@ public class UfoProductIndexBaseService {
return productMap;
}
public List<Map<String, Object>> buildProductReturnInfoList(List<Map<String, Object>> productEsSourceList) {
public List<Map<String, Object>> buildSuggestReturnInfoList(List<Map<String, Object>> productEsSourceList) {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (Map<String, Object> productEsSource : productEsSourceList) {
results.add(this.getProductMapFromEsSource(productEsSource));
results.add(this.getSuggestFromEsSource(productEsSource));
}
return results;
}
public Map<String, Object> getSuggestFromEsSource(Map<String, Object> map) {
Map<String, Object> productMap = new HashMap<String, Object>();
productMap.put("product_name", MapUtils.getString(map, UfoProductIndexEsField.productName, ""));
return productMap;
}
}
... ...
... ... @@ -146,4 +146,41 @@ public class UfoProductListService {
}
}
public SearchApiResult suggest(Map<String, String> paramMap) {
try {
// 1、参数校验
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
if (pageSize < 0) {
return new SearchApiResult().setCode(400).setMessage("viewNum参数不合法");
}
if (pageSize > 100) {
paramMap.put("viewNum", "100");
}
// 2. 构建SearchParam
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = ufoSearchQueryHelper.constructFilterBuilder(paramMap, null);
searchParam.setFiter(boolFilter);
QueryBuilder queryBuilder = ufoSearchQueryHelper.constructSuggestQueryBuilder(paramMap);
searchParam.setQuery(queryBuilder);
searchParam.setAggregationBuilders(null);
searchParam.setSize(pageSize);
searchParam.setOffset(0);
// 设置返回的结果
List<String> includeFields = ufoProductIndexBaseService.getUfoProductIndexIncludeFields();
searchParam.setIncludeFields(includeFields);
// 执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
// 构造返回结果
List<Map<String, Object>> returnInfoList = ufoProductIndexBaseService.buildSuggestReturnInfoList(searchResult.getResultList());
JSONObject dataMap = new JSONObject();
dataMap.put("size", pageSize);
dataMap.put("total", searchResult.getTotalPage());
dataMap.put("suggest_list", returnInfoList);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setCode(500);
}
}
}
... ...