Authored by zhaojun2

修改groupshop

... ... @@ -158,4 +158,11 @@ public class SearchDynamicConfigService {
return configReader.getBoolean("search.tbl.use.new.index", true);
}
/**
* group_shop新实现
*/
public boolean groupShopNew() {
return configReader.getBoolean("search.shop.group.new", true);
}
}
... ...
... ... @@ -5,6 +5,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.yoho.search.aop.downgrade.PersionalRateLimit;
import com.yoho.search.common.SearchDynamicConfigService;
import com.yoho.search.service.scene.shopbrand.RecommendShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
... ... @@ -32,7 +33,8 @@ public class ShopsController {
private BrandWithShopsService brandWithShopsService;
@Autowired
private RecommendShopService recommendShopService;
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
/**
* 按关键字搜出一个符合条件的品牌[待删除]
*
... ... @@ -97,6 +99,9 @@ public class ShopsController {
@ResponseBody
public SearchApiResult group_shops(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
if (searchDynamicConfigService.groupShopNew()) {
return shopsService.groupShopsV2(paramMap);
}
return shopsService.group_shops(paramMap);
}
... ...
package com.yoho.search.service.scene.shopbrand;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.cache.beans.AbstractCacheBean;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.helper.ProductListHelper;
import com.yoho.search.service.index.ShopsIndexBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, ShopProductResponse, ShopProductRequestResponse> {
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private ShopsIndexBaseService shopsIndexBaseService;
@Autowired
private ProductListHelper productListHelper;
public Map<String, ShopProductResponse> queryShopProductList(List<ShopProductRequest> requests) {
List<ShopProductRequestResponse> requestResponses = new ArrayList<>();
requests.forEach(e -> requestResponses.add(new ShopProductRequestResponse(e)));
//2、执行查询
this.bacthFillResponseWithCache(requestResponses,60);
//3、返回结果
Map<String, ShopProductResponse> responseMap = new HashMap<>();
requestResponses.forEach(e -> {
if (!CollectionUtils.isEmpty(e.getResponse().getProduct_list())) {
responseMap.put(e.getRequest().getShopId().toString(), e.getResponse());
}
});
return responseMap;
}
@Override
protected boolean useEhCache() {
return false;
}
@Override
protected Map<ShopProductRequest, ShopProductResponse> queryMissCacheRequestResults(List<ShopProductRequestResponse> missCacheRequests) {
Map<ShopProductRequest, ShopProductResponse> results = new HashMap<>();
if (missCacheRequests == null || missCacheRequests.isEmpty()) {
return results;
}
List<SearchParam> searchParams = new ArrayList<>();
for (ShopProductRequestResponse requestResponse : missCacheRequests) {
searchParams.add(requestResponse.getRequest().searchParam());
}
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
Map<String, List<Map<String, Object>>> shopProductListMap = new HashMap<>();
Map<String, Long> shopProductCountMap = new HashMap<>();
List<String> shopIds = new ArrayList<>();
for (SearchResult searchResult : searchResults) {
if (!CollectionUtils.isEmpty(searchResult.getResultList())) {
String shopIdString = String.valueOf(searchResult.getResultList().get(0).get("shopId"));
if (StringUtils.isNotBlank(shopIdString) && !shopIds.contains(shopIdString)) {
shopIds.add(shopIdString);
}
shopProductListMap.put(shopIdString, searchResult.getResultList());
shopProductCountMap.put(shopIdString, searchResult.getTotal());
}
}
Map<String, Map<String, Object>> shopMap = shopsIndexBaseService.getShopsMapByIds(shopIds);
shopProductListMap = productListHelper.buildReturnInfoByEsSourceListMap(shopProductListMap);
for (ShopProductRequestResponse requestResponse : missCacheRequests) {
String shopId = requestResponse.getRequest().getShopId().toString();
ShopProductResponse response = new ShopProductResponse();
response.setCount(shopProductCountMap.get(shopId) != null ? shopProductCountMap.get(shopId).intValue() : 0);
response.setInfo(shopMap.get(shopId));
response.setProduct_list(shopProductListMap.get(shopId));
results.put(requestResponse.getRequest(), response);
}
return results;
}
@Override
protected void batchAddResponseToCache(List<ShopProductRequestResponse> shopProductRequestResponses) {
super.batchAddResponseToCache(shopProductRequestResponses);
}
}
... ...
package com.yoho.search.service.scene.shopbrand;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.model.ICacheRequest;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import java.util.Arrays;
public class ShopProductRequest implements ICacheRequest {
private ParamQueryFilter paramQueryFilter;
private Integer shopId;
private Integer topHitCount;
private RedisKeyBuilder redisKeyBuilder;
public ShopProductRequest(ParamQueryFilter paramQueryFilter, Integer shopId, Integer topHitCount) {
this.paramQueryFilter = paramQueryFilter;
this.shopId = shopId;
this.topHitCount = topHitCount;
this.redisKeyBuilder = genRedisKeyBuilder();
}
private RedisKeyBuilder genRedisKeyBuilder() {
StringBuilder sb = new StringBuilder();
sb.append("paramMd5Key:").append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key());
sb.append("shopCacheKey:").append(shopId == null ? "" : shopId);
String cacheKey = MD5Util.string2MD5(sb.toString());
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH:").appendFixed("SHOP:");
redisKeyBuilder.appendFixed(shopId).appendFixed(":");
redisKeyBuilder.appendVar(cacheTimeInMinute()).appendFixed(":");
redisKeyBuilder.appendVar(cacheKey);
return redisKeyBuilder;
}
@Override
public RedisKeyBuilder redisKeyBuilder() {
return this.redisKeyBuilder;
}
@Override
public int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_10_MINUTE;
}
public SearchParam searchParam() {
SearchParam searchParam = new SearchParam();
if (paramQueryFilter != null && paramQueryFilter.getParamQuery() != null) {
searchParam.setQuery(this.paramQueryFilter.getParamQuery());
}
searchParam.setFiter(paramQueryFilter.getParamFilter());
searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort("heatValue").order(SortOrder.DESC)));
searchParam.setOffset(0);
searchParam.setSize(topHitCount);
return searchParam;
}
public Integer getShopId() {
return shopId;
}
public void setShopId(Integer shopId) {
this.shopId = shopId;
}
}
... ...
package com.yoho.search.service.scene.shopbrand;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.cache.model.AbstractCacheRequestResponse;
public class ShopProductRequestResponse extends AbstractCacheRequestResponse<ShopProductRequest, ShopProductResponse> {
public ShopProductRequestResponse(ShopProductRequest request) {
super(request);
}
@Override
public Transfer<String, ShopProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, ShopProductResponse.class);
}
@Override
public Transfer<ShopProductResponse, String> getFromResponseTransfer() {
return (v) -> JSON.toJSONString(v);
}
}
... ...
package com.yoho.search.service.scene.shopbrand;
import java.util.List;
import java.util.Map;
public class ShopProductResponse {
private Integer count;
private Map<String, Object> info;
private List<Map<String, Object>> product_list;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Map<String, Object> getInfo() {
return info;
}
public void setInfo(Map<String, Object> info) {
this.info = info;
}
public List<Map<String, Object>> getProduct_list() {
return product_list;
}
public void setProduct_list(List<Map<String, Object>> product_list) {
this.product_list = product_list;
}
}
... ...
... ... @@ -20,6 +20,7 @@ import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.common.BaseService;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -37,6 +38,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ShopsService extends BaseService implements ApplicationEventPublisherAware {
... ... @@ -53,6 +55,8 @@ public class ShopsService extends BaseService implements ApplicationEventPublish
private AggregationFactory aggregationFactory;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private ShopProductCacheBean shopProductCacheBean;
private ApplicationEventPublisher publisher;
... ... @@ -257,5 +261,36 @@ public class ShopsService extends BaseService implements ApplicationEventPublish
}
return searchResult.getResultList().get(0).get(esFieldName).toString();
}
public SearchApiResult groupShopsV2(Map<String, String> paramMap) {
try {
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP))) {
return new SearchApiResult().setCode(400).setMessage("请传shop参数");
}
// 1、获取topHitCount
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) || Integer.parseInt(paramMap.get("viewNum")) == 0 ? 10 : Integer.parseInt(paramMap.get("viewNum"));
// 4、从ES中获取
String[] shopIds = paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP).split(",");
List<ShopProductRequest> requests = new ArrayList<>();
for (String shopId : shopIds) {
paramMap.put(SearchRequestParams.PARAM_SEARCH_SHOP, shopId);
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
ParamQueryFilter paramQueryFilter = new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter());
requests.add(new ShopProductRequest(paramQueryFilter, Integer.valueOf(shopId), topHitCount));
}
Map<String, ShopProductResponse> responseMap = shopProductCacheBean.queryShopProductList(requests);
// 5、返回生成结果
Integer total = responseMap.values().stream().collect(Collectors.summingInt(ShopProductResponse::getCount));
JSONObject realResult = new JSONObject();
realResult.put("total", total);
realResult.put("shops", responseMap);
return new SearchApiResult().setData(realResult).setMessage("groupShops new List.");
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult("group_shops", paramMap, e);
}
}
}
... ...