Authored by unknown

新版本发现好货接口

... ... @@ -3,6 +3,8 @@ package com.yoho.search.service.scene;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
... ... @@ -119,7 +121,7 @@ public class NewGoodProductSceneService {
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(goodProductSearchCache, indexName, searchParam);
if (cacheObject != null) {
return new SearchApiResult().setData(cacheObject);
//return new SearchApiResult().setData(cacheObject);
}
// 6、查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
... ... @@ -134,7 +136,7 @@ public class NewGoodProductSceneService {
dataMap.put("page_total", searchResult.getTotalPage());
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
dataMap.put("product_list", this.sortGoodProductList(product_list, paramMap, recProductSkns));// 处理一下商品的顺序;
dataMap.put("product_list", this.sortGoodProductList(product_list, recProductSkns));// 处理一下商品的顺序;
// 8)将结果存进缓存
searchCacheService.addJSONObjectToCache(goodProductSearchCache, indexName, searchParam, dataMap);
... ... @@ -247,6 +249,12 @@ public class NewGoodProductSceneService {
return result;
}
private String getSortKey(Map<String, Object> product) {
int brandId = MapUtils.getInteger(product, "brand_id", 0);
int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
return brandId + "_" + small_sort_id;
}
/**
* 二次排序
*
... ... @@ -254,12 +262,66 @@ public class NewGoodProductSceneService {
* @param recProductSkns
* @return
*/
private List<Map<String, Object>> sortGoodProductList(List<Map<String, Object>> product_list, Map<String, String> paramMap, List<String> recProductSkns) {
List<Map<String, Object>> productList = productListSortService.sortProductList(product_list, paramMap);
List<Map<String, Object>>
private List<Map<String, Object>> sortGoodProductList(List<Map<String, Object>> product_list, List<String> recProductSkns) {
if (product_list == null || product_list.isEmpty()) {
return product_list;
}
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
Map<String, Integer> keyCount = new HashMap<String, Integer>();
Iterator<Map<String, Object>> iterator = product_list.iterator();
// 1、非推荐skn的品牌品类打散
while (iterator.hasNext()) {
Map<String, Object> product = iterator.next();
if (recProductSkns.contains(MapUtils.getString(product, "product_skn"))) {
continue;
}
String key = this.getSortKey(product);
int count = keyCount.getOrDefault(key, 1);
if (count <= 1) {
results.add(product);
iterator.remove();
}
keyCount.put(key, count + 1);
}
// 2、处理剩余的非推荐skn
iterator = product_list.iterator();
while (iterator.hasNext()) {
Map<String, Object> product = iterator.next();
if (recProductSkns.contains(MapUtils.getString(product, "product_skn"))) {
continue;
}
results.add(product);
iterator.remove();
}
// 3、将推荐的skn按顺序插到指定位置
Map<String, Map<String, Object>> productInfo = new HashMap<String, Map<String, Object>>();
for (Map<String, Object> product : product_list) {
productInfo.put(MapUtils.getString(product, "product_skn"), product);
}
int orginSize = results.size();
for (int i = 0; i < recProductSkns.size(); i++) {
Map<String, Object> product = productInfo.get(recProductSkns.get(i));
if(product==null){
continue;
}
int index = getIndex(orginSize,results);
results.add(index,product);
}
return results;
}
return productList;
private int getIndex(int orginSize,List<Map<String, Object>> results){
int currenSize = results.size();
if(currenSize==orginSize){
return 0;
}
if(currenSize-orginSize==1){
return currenSize>4?4:currenSize;
}
if(currenSize-orginSize==2){
return currenSize>7?7:currenSize;
}
return currenSize;
}
}
... ...