Authored by hugufei

使用泛型重构ProductListSortService

package com.yoho.search.recall.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ISearchConstants;
... ... @@ -29,6 +10,11 @@ import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.personalized.PersonalizedSearch;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.recall.common.cacheable.CacheAbleServiceHelper;
import com.yoho.search.recall.common.cacheable.CommonPageRecallService;
import com.yoho.search.recall.common.model.CommonRecallParam;
import com.yoho.search.recall.common.model.CommonRecallResult;
import com.yoho.search.recall.common.model.CommonRecallSkn;
import com.yoho.search.service.base.ProductListSortKey;
import com.yoho.search.service.base.ProductListSortService;
import com.yoho.search.service.base.SearchCommonService;
... ... @@ -36,12 +22,20 @@ import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.helper.SearchSortHelper;
import com.yoho.search.recall.common.cacheable.CacheAbleServiceHelper;
import com.yoho.search.recall.common.cacheable.CommonPageRecallService;
import com.yoho.search.recall.common.model.CommonRecallParam;
import com.yoho.search.recall.common.model.CommonRecallResult;
import com.yoho.search.recall.common.model.CommonRecallSkn;
import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class ProductListServiceHelper {
... ... @@ -129,18 +123,16 @@ public class ProductListServiceHelper {
results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn())));
}
// 4、品牌打散
ProductListSortKey productSortKey = new ProductListSortKey() {
results = productListSortService.sortProductList(results, new ProductListSortKey<JSONObject>() {
@Override
public String getSortKey(Map<?, ?> product) {
public String getSortKey(JSONObject product) {
return MapUtils.getString(product, "brand_id", "0");
}
@Override
public int getMaxCount() {
return 2;
}
};
results = productListSortService.sortProductJSONObjectList(results, productSortKey);
});
return results;
}
... ... @@ -262,7 +254,7 @@ public class ProductListServiceHelper {
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序;
dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序;
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...
... ... @@ -111,7 +111,7 @@ public class CacheAbleServiceHelper {
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
// 5)构造返回结果
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
product_list = productListSortService.sortProductList(product_list, paramMap);// 处理一下商品的顺序;
product_list = productListSortService.sortProductList(product_list);// 处理一下商品的顺序;
return new SearchApiResult().setData(product_list);
} catch (Exception e) {
logger.error(e.getMessage(),e);
... ...
... ... @@ -2,9 +2,9 @@ package com.yoho.search.service.base;
import java.util.Map;
public interface ProductListSortKey {
public interface ProductListSortKey<T> {
public String getSortKey(Map<?, ?> product);
public String getSortKey(T product);
public int getMaxCount();
... ...
package com.yoho.search.service.base;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import java.util.*;
@Service
public class ProductListSortService {
private String getSortKey(Map<String, Object> product) {
public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list) {
ProductListSortKey<Map<String, Object>> productSortKey = new ProductListSortKey<Map<String, Object>>() {
@Override
public 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;
}
/**
* 第一页的商品按品类+品牌维度分组,每组取第一个放前面
*
* @param product_list
* @param paramMap
* @return
*/
public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list, Map<String, String> paramMap) {
if (product_list == null || product_list.isEmpty()) {
return product_list;
@Override
public int getMaxCount() {
return 2;
}
if (StringUtils.isNotBlank(paramMap.get("order"))) {
return product_list;
}
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page != 1) {
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();
while (iterator.hasNext()) {
Map<String, Object> product = iterator.next();
String key = this.getSortKey(product);
int count = keyCount.getOrDefault(key, 1);
if (count <= 2) {
results.add(product);
iterator.remove();
}
keyCount.put(key, count + 1);
}
if (!product_list.isEmpty()) {
results.addAll(product_list);
}
return results;
};
return this.sortProductList(product_list,productSortKey);
}
/**
* 品牌打散
*
* @param product_list
* @param paramMap
* @return
*/
public List<Map<String, Object>> sortProductMapList(List<Map<String, Object>> product_list, ProductListSortKey productSortKey) {
public <T> List<T> sortProductList(List<T> product_list, ProductListSortKey<T> productSortKey) {
if (product_list == null || product_list.isEmpty()) {
return product_list;
}
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
List<T> results = new ArrayList<T>();
while (!product_list.isEmpty()) {
results.addAll(getTempProductMapList(product_list, productSortKey));
}
return results;
}
private List<Map<String, Object>> getTempProductMapList(List<Map<String, Object>> product_list, ProductListSortKey productSortKey) {
List<Map<String, Object>> tempResults = new ArrayList<Map<String, Object>>();
private <T> List<T> getTempProductMapList(List<T> product_list, ProductListSortKey<T> productSortKey) {
List<T> tempResults = new ArrayList<T>();
Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
Iterator<Map<String, Object>> iterator = product_list.iterator();
Iterator<T> iterator = product_list.iterator();
while (iterator.hasNext()) {
Map<String, Object> product = iterator.next();
T product = iterator.next();
String sortKey = productSortKey.getSortKey(product);
int count = tempKeyCount.getOrDefault(sortKey, 1);
if (count <= productSortKey.getMaxCount()) {
... ... @@ -93,39 +53,4 @@ public class ProductListSortService {
return tempResults;
}
/**
* 品牌打散
*
* @param product_list
* @param paramMap
* @return
*/
public List<JSONObject> sortProductJSONObjectList(List<JSONObject> product_list, ProductListSortKey productListSortKey) {
if (product_list == null || product_list.isEmpty()) {
return product_list;
}
List<JSONObject> results = new ArrayList<JSONObject>();
while (!product_list.isEmpty()) {
results.addAll(getTempProductJSONObjectList(product_list, productListSortKey));
}
return results;
}
private List<JSONObject> getTempProductJSONObjectList(List<JSONObject> product_list, ProductListSortKey productListSortKey) {
List<JSONObject> tempResults = new ArrayList<JSONObject>();
Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
Iterator<JSONObject> iterator = product_list.iterator();
while (iterator.hasNext()) {
JSONObject product = iterator.next();
String sortKey = productListSortKey.getSortKey(product);
int count = tempKeyCount.getOrDefault(sortKey, 1);
if (count <= productListSortKey.getMaxCount()) {
tempResults.add(product);
iterator.remove();
}
tempKeyCount.put(sortKey, count + 1);
}
return tempResults;
}
}
... ...
... ... @@ -133,7 +133,7 @@ public class WebProductListService extends AbstractCacheAbleService {
dataMap.put("page_total", searchResult.getTotalPage());
List<Map<String, Object>> product_list = webProductIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序
dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序
// 9)将结果存进缓存
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
... ...
... ... @@ -75,7 +75,7 @@ public class FuzzySceneProductListService extends AbstractCacheAbleService {
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序;
dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序;
// 5)将结果存进缓存
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
... ...
... ... @@ -134,7 +134,7 @@ public class ProductListServiceImpl implements IProductListService {
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序
dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error("[func=productListBySkn][params=" + paramMap + "]", e);
... ...