Authored by 胡古飞

品类页默认商品列表将>=均价的商品往前平移8个

... ... @@ -41,6 +41,7 @@ public class SearchParamHelper {
if (mustFilter != null) {
filter.must(mustFilter);
}
searchParam.setFiter(filter);
// 2、构造query
if (needPersional) {
searchParam.setQuery(searchServiceHelper.constructQueryBuilderPersional(paramMap, filter));
... ...
... ... @@ -3,11 +3,13 @@ package com.yoho.search.service.servicenew.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
... ... @@ -126,7 +128,9 @@ public class ProductListServiceImpl implements IProductListService {
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("product_list", productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()));
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));// 处理一下商品的顺序
// 6)将结果存进缓存
searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
... ... @@ -161,6 +165,45 @@ public class ProductListServiceImpl implements IProductListService {
return searchDynamicConfigService.isSearchSuggestionTipsOpen();
}
private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
// 判断页面合法性
if (!searchCommonHelper.isSortPageDefault(paramMap)) {
return product_list;
}
String page = paramMap.get("page");
if (StringUtils.isNotBlank(page) && !page.equals("1")) {
return product_list;
}
// 判断总数
int total = product_list.size();
if (total <= 10) {
return product_list;
}
// 计算均价
double totalPrice = 0;
for (Map<String, Object> product : product_list) {
totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);
}
double averagePrice = totalPrice / total;
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
results.add(product_list.get(0));
product_list.remove(0);
Iterator<Map<String, Object>> iterator = product_list.iterator();
int moveCount = 0;
while (iterator.hasNext() && moveCount < 8) {
Map<String, Object> product = iterator.next();
if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {
results.add(product);
moveCount++;
iterator.remove();
}
}
if (!product_list.isEmpty()) {
results.addAll(product_list);
}
return results;
}
@Override
public SearchApiResult productListBySknList(String skns) {
Assert.notNull(skns);
... ...