|
|
package com.yoho.search.service.servicenew.scene.common;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
...
|
...
|
@@ -21,9 +22,10 @@ import com.yoho.search.service.cache.SearchCacheMatchLogger; |
|
|
import com.yoho.search.service.cache.model.SearchCache;
|
|
|
import com.yoho.search.service.service.SearchCacheService;
|
|
|
import com.yoho.search.service.service.SearchCommonService;
|
|
|
import com.yoho.search.service.service.SearchKeyWordService;
|
|
|
import com.yoho.search.service.service.base.ProductIndexBaseService;
|
|
|
import com.yoho.search.service.service.helper.SearchCommonHelper;
|
|
|
import com.yoho.search.service.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.service.helper.SearchSortHelper;
|
|
|
import com.yoho.search.service.servicenew.impl.BaseService;
|
|
|
import com.yoho.search.service.vo.SearchApiResult;
|
|
|
|
...
|
...
|
@@ -35,19 +37,21 @@ public class SceneProductListService extends BaseService { |
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchKeyWordService searchKeyWordService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private SearchCacheService searchCacheService;
|
|
|
@Autowired
|
|
|
private SearchCacheFactory searchCacheFactory;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchSortHelper searchSortHelper;
|
|
|
|
|
|
private SearchCache productListSearchCache;
|
|
|
private SearchCache searchCache;
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
productListSearchCache = searchCacheFactory.getProductListSearchCache();
|
|
|
searchCache = searchCacheFactory.getProductListSearchCache();
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -63,7 +67,7 @@ public class SceneProductListService extends BaseService { |
|
|
|
|
|
// 2)从缓存中获取数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam);
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/split/productList.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheObject);
|
...
|
...
|
@@ -75,26 +79,27 @@ public class SceneProductListService extends BaseService { |
|
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
|
|
}
|
|
|
|
|
|
// 4)记录关键字对应的查询结果
|
|
|
String queryWord = paramMap.get("query");
|
|
|
if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySkn(queryWord)) {
|
|
|
searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());
|
|
|
}
|
|
|
|
|
|
// 5)构造返回结果
|
|
|
// 4)构造返回结果
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
dataMap.put("total", searchResult.getTotal());
|
|
|
dataMap.put("page", searchResult.getPage());
|
|
|
dataMap.put("page_size", searchParam.getSize());
|
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null);
|
|
|
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));// 处理一下商品的顺序
|
|
|
|
|
|
// 6)将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
|
|
|
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));
|
|
|
|
|
|
// 5)将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 品类列表页针对第一页的商品,将价格高的往前面平移8个
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @param product_list
|
|
|
* @return
|
|
|
*/
|
|
|
private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
|
|
|
// 判断页面合法性
|
|
|
if (!searchCommonHelper.isSortPageDefault(paramMap)) {
|
...
|
...
|
@@ -133,4 +138,40 @@ public class SceneProductListService extends BaseService { |
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
|
|
|
// 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) {
|
|
|
throw new IllegalArgumentException("分页参数不合法");
|
|
|
}
|
|
|
if (pageSize > 500) {
|
|
|
pageSize = 500;
|
|
|
}
|
|
|
// 2)构建基本查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true);
|
|
|
setHighlight(paramMap, searchParam);
|
|
|
searchParam.setAggregationBuilders(null);
|
|
|
searchParam.setPage(page);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
// 3)设置排序字段
|
|
|
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
|
|
|
// 4)设置查询结果返回字段
|
|
|
if (StringUtils.isNotBlank(paramMap.get("resultFields"))) {
|
|
|
String resultFields = paramMap.get("resultFields");
|
|
|
searchParam.setResultFields(Arrays.asList(resultFields.split(",")));
|
|
|
}
|
|
|
return searchParam;
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|