|
|
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.common.SearchRequestParams;
|
|
|
import com.yoho.search.service.index.ProductIndexBaseService;
|
|
|
|
|
|
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;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
...
|
...
|
@@ -43,14 +43,13 @@ public class SearchLikeInShopService { |
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
@Autowired
|
|
|
private BigDataSimilarSknImgAliIndexBaseService bigDataSimilarSknImgAliIndexBaseService;
|
|
|
|
|
|
/**
|
|
|
* 店铺内推荐
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP_NEW", includeParams = { "product_skn"})
|
|
|
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_IN_SHOP_NEW", includeParams = {"product_skn"})
|
|
|
public SearchApiResult searchLikeInShop(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 1、获取参数
|
...
|
...
|
@@ -66,19 +65,32 @@ public class SearchLikeInShopService { |
|
|
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
}
|
|
|
// 4、设置SearchParams
|
|
|
List<SearchParam> searchParams = new ArrayList<SearchParam>();
|
|
|
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 = searchLikeHelper.queryProductList(searchParams);
|
|
|
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);
|
|
|
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>>();
|
|
|
List<Map<String, Object>> productListResults = new ArrayList<>();
|
|
|
if (!tempProductList.isEmpty()) {
|
|
|
productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
|
|
|
}
|
...
|
...
|
@@ -139,4 +151,30 @@ public class SearchLikeInShopService { |
|
|
return queryBuilder;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据阿里相似图片的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;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|