|
|
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.CollectionUtils;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.aop.cache.SearchCacheAble;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
|
|
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
import com.yoho.search.service.index.BigDataSimilarSknImgAliIndexBaseService;
|
|
|
import com.yoho.search.service.index.ProductIndexBaseService;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
...
|
...
|
@@ -29,152 +29,114 @@ import java.util.Map; |
|
|
|
|
|
/**
|
|
|
* 店铺内找相似
|
|
|
*
|
|
|
*
|
|
|
* @author gufei.hu
|
|
|
*/
|
|
|
@Service
|
|
|
public class SearchLikeInShopService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SearchLikeInShopService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchLikeHelper searchLikeHelper;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
@Autowired
|
|
|
private BigDataSimilarSknImgAliIndexBaseService bigDataSimilarSknImgAliIndexBaseService;
|
|
|
|
|
|
/**
|
|
|
* 店铺内推荐
|
|
|
*/
|
|
|
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP", includeParams = {"product_skn"})
|
|
|
public SearchApiResult searchLikeInShop(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条】
|
|
|
int viewNum = 30;
|
|
|
// 3、获取当前查询的SKN的基本信息
|
|
|
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
if (productInfoInEs == null) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
}
|
|
|
// 4、设置SearchParams
|
|
|
List<SearchParam> searchParams = new ArrayList<>();
|
|
|
//4.1图片相似skn
|
|
|
List<String> similarProductSknList = bigDataSimilarSknImgAliIndexBaseService.getSimilarProductSknListInShop(productSkn);
|
|
|
if (similarProductSknList != null && !similarProductSknList.isEmpty()) {
|
|
|
searchParams.add(this.builderSearchParamForSimilarImg(productInfoInEs, similarProductSknList, viewNum));
|
|
|
}
|
|
|
//4.2文字相似skn
|
|
|
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), viewNum));
|
|
|
// 5、获取搜索结果[截取条数]
|
|
|
List<Map<String, Object>> tempProductList = new ArrayList<>();
|
|
|
//只包含文字相似
|
|
|
if (searchParams.size() == 1) {
|
|
|
tempProductList = searchLikeHelper.queryProductList(searchParams);
|
|
|
} else {
|
|
|
//包含图片+文字相似
|
|
|
tempProductList = searchLikeHelper.queryProductListWithSimilarImg(searchParams, similarProductSknList);
|
|
|
}
|
|
|
if (tempProductList.size() > viewNum) {
|
|
|
tempProductList = CollectionUtils.safeSubList(tempProductList, 0, viewNum);
|
|
|
}
|
|
|
//6、保留偶数
|
|
|
if (tempProductList.size() % 2 > 0) {
|
|
|
tempProductList = CollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
|
|
|
}
|
|
|
// 7、构造返回结果
|
|
|
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("searchLikeInShop Exception").setCode(500);
|
|
|
}
|
|
|
}
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SearchLikeInShopService.class);
|
|
|
|
|
|
private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> productSkns, int pageSize) {
|
|
|
// 1、设置SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
// 2)设置query和filter
|
|
|
searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, "20%"));
|
|
|
searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns));
|
|
|
// 3、设置排序规则[按打分排序]
|
|
|
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
|
|
|
sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
// 4、设置分页参数
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(pageSize);
|
|
|
// 5)设置返回的参数【节省带宽】
|
|
|
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
searchParam.setIncludeFields(includeFields);
|
|
|
return searchParam;
|
|
|
}
|
|
|
@Autowired
|
|
|
private SearchLikeHelper searchLikeHelper;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
/**
|
|
|
* 店铺内推荐
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP_NEW", includeParams = { "product_skn"})
|
|
|
public SearchApiResult searchLikeInShop(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条】
|
|
|
int viewNum = 30;
|
|
|
// 3、获取当前查询的SKN的基本信息
|
|
|
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
if (productInfoInEs == null) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
}
|
|
|
// 4、设置SearchParams
|
|
|
List<SearchParam> searchParams = new ArrayList<SearchParam>();
|
|
|
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), viewNum));
|
|
|
// 5、获取搜索结果[截取条数]
|
|
|
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
|
|
|
if (tempProductList.size() > viewNum) {
|
|
|
tempProductList = CollectionUtils.safeSubList(tempProductList,0, viewNum);
|
|
|
}
|
|
|
//6、保留偶数
|
|
|
if (tempProductList.size() % 2 > 0) {
|
|
|
tempProductList = CollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
|
|
|
}
|
|
|
// 7、构造返回结果
|
|
|
List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
|
|
|
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("searchLikeInShop Exception").setCode(500);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private QueryBuilder builderFilterBuilder(JSONObject productInfoInEs, List<String> notProductSkns) {
|
|
|
String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(notProductSkns, 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)设置品牌或店铺信息
|
|
|
Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
|
|
|
Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
|
|
|
if (searchLikeHelper.isLegalInteger(shopId)) {
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
|
|
|
} else if (searchLikeHelper.isLegalInteger(brandId)) {
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
|
|
|
}
|
|
|
return boolFilter;
|
|
|
}
|
|
|
private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> productSkns, int pageSize) {
|
|
|
// 1、设置SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
// 2)设置query和filter
|
|
|
searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, "20%"));
|
|
|
searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns));
|
|
|
// 3、设置排序规则[按打分排序]
|
|
|
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
|
|
|
sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
// 4、设置分页参数
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(pageSize);
|
|
|
// 5)设置返回的参数【节省带宽】
|
|
|
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
searchParam.setIncludeFields(includeFields);
|
|
|
return searchParam;
|
|
|
}
|
|
|
|
|
|
private QueryBuilder builderQueryBuilder(JSONObject productInfoInEs, String minimumShouldMatch) {
|
|
|
String queryString = searchLikeHelper.genYohoQueryStringWithBrandName(productInfoInEs);
|
|
|
String productFeatureFactor = productInfoInEs.getString(ProductIndexEsField.productFeatureFactor);
|
|
|
QueryBuilder queryBuilder = searchLikeHelper.genSearchLikeQueryBuilder(queryString, minimumShouldMatch, productFeatureFactor);
|
|
|
return queryBuilder;
|
|
|
}
|
|
|
private QueryBuilder builderFilterBuilder(JSONObject productInfoInEs, List<String> notProductSkns) {
|
|
|
String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(notProductSkns, 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)设置品牌或店铺信息
|
|
|
Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
|
|
|
Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
|
|
|
if (searchLikeHelper.isLegalInteger(shopId)) {
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
|
|
|
} else if (searchLikeHelper.isLegalInteger(brandId)) {
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
|
|
|
}
|
|
|
return boolFilter;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据阿里相似图片的skn构建SearchParam去查productindex
|
|
|
*/
|
|
|
private SearchParam builderSearchParamForSimilarImg(JSONObject productInfoInEs, List<String> productSkns, int pageSize) {
|
|
|
// 1、设置SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
// 2)设置filter
|
|
|
String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
|
|
|
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
|
|
|
String gender = productInfoInEs.getString(ProductIndexEsField.gender);
|
|
|
List<String> genderList = searchLikeHelper.getGenderInfo(gender);
|
|
|
if (genderList != null && !genderList.isEmpty()) {
|
|
|
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
|
|
|
}
|
|
|
searchParam.setFiter(boolFilter);
|
|
|
// 4、设置分页参数
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(pageSize);
|
|
|
// 5)设置返回的参数【节省带宽】
|
|
|
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
searchParam.setIncludeFields(includeFields);
|
|
|
return searchParam;
|
|
|
}
|
|
|
private QueryBuilder builderQueryBuilder(JSONObject productInfoInEs, String minimumShouldMatch) {
|
|
|
String queryString = searchLikeHelper.genYohoQueryStringWithBrandName(productInfoInEs);
|
|
|
String productFeatureFactor = productInfoInEs.getString(ProductIndexEsField.productFeatureFactor);
|
|
|
QueryBuilder queryBuilder = searchLikeHelper.genSearchLikeQueryBuilder(queryString, minimumShouldMatch, productFeatureFactor);
|
|
|
return queryBuilder;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|