Authored by hugufei

重命名

... ... @@ -4,7 +4,7 @@ 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.dal.model.SimilarSkn;
import com.yoho.search.dal.model.BidataSimilarSkn;
import com.yoho.search.common.SearchCommonService;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -26,7 +26,7 @@ public class BigdataSimilarSknIndexBaseService {
@Autowired
private SearchCommonService searchCommonService;
public SimilarSkn querySimilarSkn(String productSkn) {
public BidataSimilarSkn querySimilarSkn(String productSkn) {
try {
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
... ... @@ -45,9 +45,9 @@ public class BigdataSimilarSknIndexBaseService {
return null;
}
Map<String, Object> result = similarSknResults.get(0);
SimilarSkn similarSkn = new SimilarSkn();
BeanUtils.populate(similarSkn, result);
return similarSkn;
BidataSimilarSkn bidataSimilarSkn = new BidataSimilarSkn();
BeanUtils.populate(bidataSimilarSkn, result);
return bidataSimilarSkn;
}catch (Exception e){
logger.error(e.getMessage(),e);
return null;
... ...
package com.yoho.search.service.scene.searchlike;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.SearchCollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.dal.model.SimilarSkn;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
import com.yoho.search.service.index.ProductIndexBaseService;
import com.yoho.search.service.helper.ProductListHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
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 BigdataSimilarSknService {
private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknService.class);
@Autowired
private SearchLikeHelper searchLikeHelper;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private ProductListHelper productListHelper;
@Autowired
private BigdataSimilarSknIndexBaseService bigdataSimilarSknIndexBaseService;
/**
* 获取非重复skn
*
* @param productSknStr
* @return
*/
private List<String> getDistinctSknList(String productSknStr) {
List<String> results = new ArrayList<>();
String[] productSkns = productSknStr.split(",");
for (String productSkn : productSkns) {
if (!results.contains(productSkn) && StringUtils.isNumeric(productSkn)) {
results.add(productSkn);
}
}
return results;
}
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_NOT_IN_SHOP_SIMILAR_NEW", includeParams = {"product_skn"})
public SearchApiResult searchLikeSimilarSknNotInShop(Map<String, String> paramMap) {
try {
// 1、获取参数
String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
if (StringUtils.isBlank(productSkn)) {
return new SearchApiResult().setCode(400).setMessage("请输入SKN");
}
// 2、检测分页参数【默认30条,最多60条】
int pageSize = 30;
// 3、获取当前查询的SKN的基本信息
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
if (productInfoInEs == null) {
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
}
//4、获取similarskn
SimilarSkn similarSkn = bigdataSimilarSknIndexBaseService.querySimilarSkn(productSkn);
//5、构造searchParams
List<SearchParam> searchParams = new ArrayList<>();
//5.1)图片[性别+不同店铺]
List<String> diffShopImgSimilarSkns = new ArrayList<>();
if (similarSkn != null && StringUtils.isNotEmpty(similarSkn.getDiffShopImgSimilarSkns())) {
diffShopImgSimilarSkns = this.getDistinctSknList(similarSkn.getDiffShopImgSimilarSkns());
}
searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopImgSimilarSkns, true));
//5.2)行为[性别+不同店铺+同品类]
List<String> diffShopActionSimilarSkns = new ArrayList<>();
if (similarSkn != null && StringUtils.isNotEmpty(similarSkn.getDiffShopActionSimilarSkns())) {
diffShopActionSimilarSkns = this.getDistinctSknList(similarSkn.getDiffShopActionSimilarSkns());
}
searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopActionSimilarSkns, true));
//5.3)文字兜底[性别+不同店铺+文字相似性]
searchParams.add(searchLikeHelper.builderSearchLikeNotInShopCharactersSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize, true));
// 6、获取搜索结果
List<List<Map<String, Object>>> queryResults = searchLikeHelper.queryProductLists(searchParams);
// 7、处理图片和行为的顺序
List<Map<String, Object>> diffShopImgSimilarProducts = this.sortProductList(queryResults.get(0), diffShopImgSimilarSkns, pageSize / 3);
List<Map<String, Object>> diffShopActionSimilarProducts = this.sortProductList(queryResults.get(1), diffShopActionSimilarSkns, pageSize / 3);
// 8、获取临时结果
List<Map<String, Object>> tempProductList = new ArrayList<>();
tempProductList.addAll(diffShopImgSimilarProducts);
tempProductList.addAll(diffShopActionSimilarProducts);
tempProductList.addAll(queryResults.get(2));
if (tempProductList.size() > pageSize) {
tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, pageSize);
}
// 8.1 保留偶数
if (tempProductList.size() % 2 > 0) {
tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
}
// 9、构造真实返回结果
List<Map<String, Object>> productListResults = new ArrayList<>();
if (!tempProductList.isEmpty()) {
productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
}
JSONObject result = new JSONObject();
result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
result.put("product_list", productListResults);
return new SearchApiResult().setData(result);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
}
}
/**
* 按productSkn顺序截取商品条数-注意循环引用的问题
*
* @param esProductList
* @param sortedProductSkns
* @param size
* @return
*/
private List<Map<String, Object>> sortProductList(List<Map<String, Object>> esProductList, List<String> sortedProductSkns, int size) {
List<Map<String, Object>> results = new ArrayList<>();
if (esProductList == null || esProductList.isEmpty()) {
return results;
}
if (sortedProductSkns == null || sortedProductSkns.isEmpty() || size <= 0) {
return results;
}
Map<String, Map<String, Object>> productMap = SearchCollectionUtils.toMap(esProductList, (product) -> MapUtils.getString(product, ProductIndexEsField.productSkn, ""));
for (String productSkn : sortedProductSkns) {
if (productMap.containsKey(productSkn)) {
results.add(new HashMap<>(productMap.get(productSkn)));//注意循环引用
}
if (results.size() >= size) {
break;
}
}
return results;
}
/**
* 构建SimilarSknSearchParam[考虑productSkns为空的情况]
*
* @param productInfoInEs
* @param productSkns
* @param filterSameSort
* @return
*/
private SearchParam builderSimilarSknSearchParam(JSONObject productInfoInEs, List<String> productSkns, boolean filterSameSort) {
// 1、设置SearchParam
SearchParam searchParam = new SearchParam();
// 2)设置query和filter
searchParam.setFiter(this.builderSimilarSknFilter(productInfoInEs, productSkns, filterSameSort));
// 3、设置分页参数
searchParam.setOffset(0);
searchParam.setSize(productSkns.size());
// 4)设置返回的参数【节省带宽】
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
searchParam.setIncludeFields(includeFields);
return searchParam;
}
private QueryBuilder builderSimilarSknFilter(JSONObject productInfoInEs, List<String> inProductSkns, boolean filterSameSort) {
String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
// 1)设置此SKN相关的性别过滤条件
String gender = productInfoInEs.getString(ProductIndexEsField.gender);
List<String> genderList = searchLikeHelper.getGenderInfo(gender);
if (genderList != null && !genderList.isEmpty()) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
}
// 2)设置productskn
if (inProductSkns != null && !inProductSkns.isEmpty()) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, inProductSkns));
}
// 3)设置品牌或店铺信息
Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
if (searchLikeHelper.isLegalInteger(shopId)) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
} else if (searchLikeHelper.isLegalInteger(brandId)) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
}
// 4)是否需要同品类
Integer middleSortId = productInfoInEs.getInteger(ProductIndexEsField.middleSortId);
if (filterSameSort && searchLikeHelper.isLegalInteger(middleSortId)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
}
return boolFilter;
}
}
package com.yoho.search.service.scene.searchlike;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.SearchCollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.dal.model.BidataSimilarSkn;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
import com.yoho.search.service.index.ProductIndexBaseService;
import com.yoho.search.service.helper.ProductListHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
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 BigdataSimilarSknService {
private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknService.class);
@Autowired
private SearchLikeHelper searchLikeHelper;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private ProductListHelper productListHelper;
@Autowired
private BigdataSimilarSknIndexBaseService bigdataSimilarSknIndexBaseService;
/**
* 获取非重复skn
*
* @param productSknStr
* @return
*/
private List<String> getDistinctSknList(String productSknStr) {
List<String> results = new ArrayList<>();
String[] productSkns = productSknStr.split(",");
for (String productSkn : productSkns) {
if (!results.contains(productSkn) && StringUtils.isNumeric(productSkn)) {
results.add(productSkn);
}
}
return results;
}
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_NOT_IN_SHOP_SIMILAR_NEW", includeParams = {"product_skn"})
public SearchApiResult searchLikeSimilarSknNotInShop(Map<String, String> paramMap) {
try {
// 1、获取参数
String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
if (StringUtils.isBlank(productSkn)) {
return new SearchApiResult().setCode(400).setMessage("请输入SKN");
}
// 2、检测分页参数【默认30条,最多60条】
int pageSize = 30;
// 3、获取当前查询的SKN的基本信息
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
if (productInfoInEs == null) {
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
}
//4、获取similarskn
BidataSimilarSkn bidataSimilarSkn = bigdataSimilarSknIndexBaseService.querySimilarSkn(productSkn);
//5、构造searchParams
List<SearchParam> searchParams = new ArrayList<>();
//5.1)图片[性别+不同店铺]
List<String> diffShopImgSimilarSkns = new ArrayList<>();
if (bidataSimilarSkn != null && StringUtils.isNotEmpty(bidataSimilarSkn.getDiffShopImgSimilarSkns())) {
diffShopImgSimilarSkns = this.getDistinctSknList(bidataSimilarSkn.getDiffShopImgSimilarSkns());
}
searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopImgSimilarSkns, true));
//5.2)行为[性别+不同店铺+同品类]
List<String> diffShopActionSimilarSkns = new ArrayList<>();
if (bidataSimilarSkn != null && StringUtils.isNotEmpty(bidataSimilarSkn.getDiffShopActionSimilarSkns())) {
diffShopActionSimilarSkns = this.getDistinctSknList(bidataSimilarSkn.getDiffShopActionSimilarSkns());
}
searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopActionSimilarSkns, true));
//5.3)文字兜底[性别+不同店铺+文字相似性]
searchParams.add(searchLikeHelper.builderSearchLikeNotInShopCharactersSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize, true));
// 6、获取搜索结果
List<List<Map<String, Object>>> queryResults = searchLikeHelper.queryProductLists(searchParams);
// 7、处理图片和行为的顺序
List<Map<String, Object>> diffShopImgSimilarProducts = this.sortProductList(queryResults.get(0), diffShopImgSimilarSkns, pageSize / 3);
List<Map<String, Object>> diffShopActionSimilarProducts = this.sortProductList(queryResults.get(1), diffShopActionSimilarSkns, pageSize / 3);
// 8、获取临时结果
List<Map<String, Object>> tempProductList = new ArrayList<>();
tempProductList.addAll(diffShopImgSimilarProducts);
tempProductList.addAll(diffShopActionSimilarProducts);
tempProductList.addAll(queryResults.get(2));
if (tempProductList.size() > pageSize) {
tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, pageSize);
}
// 8.1 保留偶数
if (tempProductList.size() % 2 > 0) {
tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
}
// 9、构造真实返回结果
List<Map<String, Object>> productListResults = new ArrayList<>();
if (!tempProductList.isEmpty()) {
productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
}
JSONObject result = new JSONObject();
result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
result.put("product_list", productListResults);
return new SearchApiResult().setData(result);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
}
}
/**
* 按productSkn顺序截取商品条数-注意循环引用的问题
*
* @param esProductList
* @param sortedProductSkns
* @param size
* @return
*/
private List<Map<String, Object>> sortProductList(List<Map<String, Object>> esProductList, List<String> sortedProductSkns, int size) {
List<Map<String, Object>> results = new ArrayList<>();
if (esProductList == null || esProductList.isEmpty()) {
return results;
}
if (sortedProductSkns == null || sortedProductSkns.isEmpty() || size <= 0) {
return results;
}
Map<String, Map<String, Object>> productMap = SearchCollectionUtils.toMap(esProductList, (product) -> MapUtils.getString(product, ProductIndexEsField.productSkn, ""));
for (String productSkn : sortedProductSkns) {
if (productMap.containsKey(productSkn)) {
results.add(new HashMap<>(productMap.get(productSkn)));//注意循环引用
}
if (results.size() >= size) {
break;
}
}
return results;
}
/**
* 构建SimilarSknSearchParam[考虑productSkns为空的情况]
*
* @param productInfoInEs
* @param productSkns
* @param filterSameSort
* @return
*/
private SearchParam builderSimilarSknSearchParam(JSONObject productInfoInEs, List<String> productSkns, boolean filterSameSort) {
// 1、设置SearchParam
SearchParam searchParam = new SearchParam();
// 2)设置query和filter
searchParam.setFiter(this.builderSimilarSknFilter(productInfoInEs, productSkns, filterSameSort));
// 3、设置分页参数
searchParam.setOffset(0);
searchParam.setSize(productSkns.size());
// 4)设置返回的参数【节省带宽】
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
searchParam.setIncludeFields(includeFields);
return searchParam;
}
private QueryBuilder builderSimilarSknFilter(JSONObject productInfoInEs, List<String> inProductSkns, boolean filterSameSort) {
String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
// 1)设置此SKN相关的性别过滤条件
String gender = productInfoInEs.getString(ProductIndexEsField.gender);
List<String> genderList = searchLikeHelper.getGenderInfo(gender);
if (genderList != null && !genderList.isEmpty()) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
}
// 2)设置productskn
if (inProductSkns != null && !inProductSkns.isEmpty()) {
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, inProductSkns));
}
// 3)设置品牌或店铺信息
Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
if (searchLikeHelper.isLegalInteger(shopId)) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
} else if (searchLikeHelper.isLegalInteger(brandId)) {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
}
// 4)是否需要同品类
Integer middleSortId = productInfoInEs.getInteger(ProductIndexEsField.middleSortId);
if (filterSameSort && searchLikeHelper.isLegalInteger(middleSortId)) {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
}
return boolFilter;
}
}
... ...