|
|
package com.yoho.search.service.scene.searchlike;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.aop.cache.SearchCacheAble;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.base.utils.SearchCollectionUtils;
|
|
|
import com.yoho.search.bo.BigdataSimilarSknIndexBO;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.dal.model.BigdataSimilarSkn;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
|
|
|
import com.yoho.search.service.index.ProductIndexBaseService;
|
|
|
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.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class SearchLikeNotInShopService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SearchLikeNotInShopService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchLikeHelper searchLikeHelper;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
@Autowired
|
|
|
private BigdataSimilarSknIndexBaseService bigdataSimilarSknIndexBaseService;
|
|
|
|
|
|
@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
|
|
|
BigdataSimilarSknIndexBO bigdataSimilarSkn = bigdataSimilarSknIndexBaseService.querySimilarSkn(productSkn);
|
|
|
|
|
|
//5、构造searchParams
|
|
|
List<SearchParam> searchParams = new ArrayList<>();
|
|
|
//5.1)图片[性别+不同店铺]
|
|
|
List<String> diffShopImgSimilarSkns = new ArrayList<>();
|
|
|
if (bigdataSimilarSkn != null && StringUtils.isNotEmpty(bigdataSimilarSkn.getDiffShopImgSimilarSkns())) {
|
|
|
diffShopImgSimilarSkns = searchLikeHelper.getDistinctSknList(bigdataSimilarSkn.getDiffShopImgSimilarSkns());
|
|
|
}
|
|
|
searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopImgSimilarSkns));
|
|
|
|
|
|
//5.2)行为[性别+不同店铺+同品类]
|
|
|
List<String> diffShopActionSimilarSkns = new ArrayList<>();
|
|
|
if (bigdataSimilarSkn != null && StringUtils.isNotEmpty(bigdataSimilarSkn.getDiffShopActionSimilarSkns())) {
|
|
|
diffShopActionSimilarSkns = searchLikeHelper.getDistinctSknList(bigdataSimilarSkn.getDiffShopActionSimilarSkns());
|
|
|
}
|
|
|
searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopActionSimilarSkns));
|
|
|
|
|
|
//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 = searchLikeHelper.sortProductList(queryResults.get(0), diffShopImgSimilarSkns, pageSize / 3);
|
|
|
List<Map<String, Object>> diffShopActionSimilarProducts = searchLikeHelper.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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建SimilarSknSearchParam[考虑productSkns为空的情况]
|
|
|
*
|
|
|
* @param productInfoInEs
|
|
|
* @param productSkns
|
|
|
* @return
|
|
|
*/
|
|
|
private SearchParam builderSimilarSknSearchParam(JSONObject productInfoInEs, List<String> productSkns) {
|
|
|
// 1、设置SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
// 2)设置query和filter
|
|
|
searchParam.setFiter(this.builderSimilarSknFilter(productInfoInEs, productSkns));
|
|
|
// 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) {
|
|
|
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 (searchLikeHelper.isLegalInteger(middleSortId)) {
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
|
|
|
}
|
|
|
return boolFilter;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|