|
|
package com.yoho.search.recall.scene.beans;
|
|
|
|
|
|
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent;
|
|
|
import com.yoho.search.recall.scene.cache.CacheSknInfoRequestResponse;
|
|
|
import com.yoho.search.recall.scene.models.SknInfoRequest;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
|
public class BacthSknInfoComponent {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private CacheRequestResponseComponent cacheRequestResponseComponent;
|
|
|
|
|
|
private static final boolean useEhCache = true;
|
|
|
|
|
|
/**
|
|
|
* 按skn查询并按顺序返回
|
|
|
*
|
|
|
* @param productSkns
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){
|
|
|
//1、批量查询SKN信息
|
|
|
List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = this.batchQuery(productSkns);
|
|
|
//2、构造返回结果
|
|
|
List<Map<String, Object>> finalResults = new ArrayList<>();
|
|
|
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : sknInfoCacheRequestRespons) {
|
|
|
if(sknInfoCacheRequestResponse !=null && sknInfoCacheRequestResponse.getResponse()!=null){
|
|
|
finalResults.add(sknInfoCacheRequestResponse.getResponse());
|
|
|
}
|
|
|
if(finalResults.size()>=size){
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return finalResults;
|
|
|
}
|
|
|
|
|
|
private List<CacheSknInfoRequestResponse> batchQuery(List<Integer> productSkns){
|
|
|
//1、构建请求与返回结果
|
|
|
final List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = new ArrayList<>();
|
|
|
for (Integer productSkn : productSkns) {
|
|
|
sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(new SknInfoRequest(productSkn)));
|
|
|
}
|
|
|
//2、批量从缓存中获取
|
|
|
cacheRequestResponseComponent.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache);
|
|
|
//3、获取未命中缓存的请求
|
|
|
List<CacheSknInfoRequestResponse> missCacheRequests = cacheRequestResponseComponent.filterMissCacheRequests(sknInfoCacheRequestRespons);
|
|
|
//4、执行批量查询
|
|
|
Map<String,Map<String, Object>> queryResults = this.batchQueryMissCacheRequests(missCacheRequests);
|
|
|
//5、填充查询结果
|
|
|
cacheRequestResponseComponent.batchFillResponseWithQueryResults(sknInfoCacheRequestRespons,queryResults);
|
|
|
//6、将CacheRequestResponse中需要缓存的key加入缓存
|
|
|
cacheRequestResponseComponent.batchAddResponseToCache(sknInfoCacheRequestRespons,useEhCache);
|
|
|
return sknInfoCacheRequestRespons;
|
|
|
}
|
|
|
|
|
|
private Map<String,Map<String, Object>> batchQueryMissCacheRequests(List<CacheSknInfoRequestResponse> notCachedRequestResponse) {
|
|
|
//1、合法性判断
|
|
|
Map<String,Map<String, Object>> results = new HashMap<>();
|
|
|
if(notCachedRequestResponse==null||notCachedRequestResponse.isEmpty()){
|
|
|
return results;
|
|
|
}
|
|
|
//2、获取skn
|
|
|
List<Integer> productSkns = new ArrayList<>();
|
|
|
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : notCachedRequestResponse) {
|
|
|
productSkns.add(sknInfoCacheRequestResponse.getRequest().getProductSkn());
|
|
|
}
|
|
|
//3、构建SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(productSkns.size());
|
|
|
searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
|
|
|
searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
List<Map<String, Object>> productList = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
|
|
|
//4、构建SKN临时结果
|
|
|
Map<Integer,Map<String, Object>> productTempMap = new HashMap<>();
|
|
|
for (Map<String, Object> product: productList) {
|
|
|
productTempMap.put(MapUtils.getIntValue(product,"product_skn",0),product);
|
|
|
}
|
|
|
//5、构造最终结果
|
|
|
for (CacheSknInfoRequestResponse requestResponse :notCachedRequestResponse ) {
|
|
|
results.put(requestResponse.getRequest().redisKeyBuilder().getKey(),productTempMap.get(requestResponse.getRequest().getProductSkn()));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
|
|
package com.yoho.search.recall.scene.beans;
|
|
|
|
|
|
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent;
|
|
|
import com.yoho.search.recall.scene.cache.CacheSknInfoRequestResponse;
|
|
|
import com.yoho.search.recall.scene.models.SknInfoRequest;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
|
public class BacthSknInfoComponent {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private CacheRequestResponseComponent cacheRequestResponseComponent;
|
|
|
|
|
|
private static final boolean useEhCache = true;
|
|
|
|
|
|
/**
|
|
|
* 按skn查询并按顺序返回
|
|
|
*
|
|
|
* @param productSkns
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){
|
|
|
//1、批量查询SKN信息
|
|
|
List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = this.batchQuery(productSkns);
|
|
|
//2、构造返回结果
|
|
|
List<Map<String, Object>> finalResults = new ArrayList<>();
|
|
|
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : sknInfoCacheRequestRespons) {
|
|
|
if(sknInfoCacheRequestResponse !=null && sknInfoCacheRequestResponse.getResponse()!=null){
|
|
|
finalResults.add(sknInfoCacheRequestResponse.getResponse());
|
|
|
}
|
|
|
if(finalResults.size()>=size){
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return finalResults;
|
|
|
}
|
|
|
|
|
|
private List<CacheSknInfoRequestResponse> batchQuery(List<Integer> productSkns){
|
|
|
//1、构建请求与返回结果
|
|
|
final List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = new ArrayList<>();
|
|
|
for (Integer productSkn : productSkns) {
|
|
|
sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(new SknInfoRequest(productSkn)));
|
|
|
}
|
|
|
//2、批量从缓存中获取
|
|
|
cacheRequestResponseComponent.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache);
|
|
|
//3、获取未命中缓存的请求
|
|
|
List<CacheSknInfoRequestResponse> missCacheRequests = cacheRequestResponseComponent.filterMissCacheRequests(sknInfoCacheRequestRespons);
|
|
|
//4、执行批量查询
|
|
|
Map<String,Map<String, Object>> queryResults = this.batchQueryMissCacheRequests(missCacheRequests);
|
|
|
//5、填充查询结果
|
|
|
cacheRequestResponseComponent.batchFillResponseWithQueryResults(sknInfoCacheRequestRespons,queryResults);
|
|
|
//6、将CacheRequestResponse中需要缓存的key加入缓存
|
|
|
cacheRequestResponseComponent.batchAddResponseToCache(sknInfoCacheRequestRespons,useEhCache);
|
|
|
return sknInfoCacheRequestRespons;
|
|
|
}
|
|
|
|
|
|
private Map<String,Map<String, Object>> batchQueryMissCacheRequests(List<CacheSknInfoRequestResponse> notCachedRequestResponse) {
|
|
|
//1、合法性判断
|
|
|
Map<String,Map<String, Object>> results = new HashMap<>();
|
|
|
if(notCachedRequestResponse==null||notCachedRequestResponse.isEmpty()){
|
|
|
return results;
|
|
|
}
|
|
|
//2、获取skn
|
|
|
List<Integer> productSkns = new ArrayList<>();
|
|
|
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : notCachedRequestResponse) {
|
|
|
productSkns.add(sknInfoCacheRequestResponse.getRequest().getProductSkn());
|
|
|
}
|
|
|
//3、构建SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(productSkns.size());
|
|
|
searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
|
|
|
searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
List<Map<String, Object>> productList = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
|
|
|
//4、构建SKN临时结果
|
|
|
Map<Integer,Map<String, Object>> productTempMap = new HashMap<>();
|
|
|
for (Map<String, Object> product: productList) {
|
|
|
productTempMap.put(MapUtils.getIntValue(product,"product_skn",0),product);
|
|
|
}
|
|
|
//5、构造最终结果
|
|
|
for (CacheSknInfoRequestResponse requestResponse :notCachedRequestResponse ) {
|
|
|
results.put(requestResponse.getRequest().redisKeyBuilder().getKey(),productTempMap.get(requestResponse.getRequest().getProductSkn()));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|