Authored by wangnan9279

店铺内找相似修改

package com.yoho.search.service.index;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.dal.model.SimilarSkn;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
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 org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author wangnan
* @version 2018/5/23
*/
@Service
public class BigDataSimilarSknImgAliIndexBaseService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SearchCommonService searchCommonService;
public SimilarSkn querySimilarSkn(String productSkn) {
try {
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkn));
searchParam.setFiter(boolFilter);
searchParam.setAggregationBuilders(null);
searchParam.setSize(1);
String productIndexName = ISearchConstants.INDEX_NAME_BIGDATA_SIMILAR_SKN_IMG_ALI_INDEX;
SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
if (searchResult == null) {
return null;
}
List<Map<String, Object>> similarSknResults = searchResult.getResultList();
if (CollectionUtils.isEmpty(similarSknResults) || similarSknResults.get(0) == null) {
return null;
}
Map<String, Object> result = similarSknResults.get(0);
SimilarSkn similarSkn = new SimilarSkn();
similarSkn.setDataId(MapUtils.getInteger(result, "dateId", 0));
similarSkn.setSameShopImgSimilarSkns(MapUtils.getString(result, "sameShopImgSimilarSkns", ""));
similarSkn.setDiffShopImgSimilarSkns(MapUtils.getString(result, "diffShopImgSimilarSkns", ""));
return similarSkn;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
public List<String> getSimilarProductSknListInShop(String productSkn) {
SimilarSkn similarSkn = this.querySimilarSkn(productSkn);
List<String> similarProductSknList = new ArrayList<>();
if (similarSkn != null && StringUtils.isNotBlank(similarSkn.getSameShopImgSimilarSkns())) {
String[] productSknArray = similarSkn.getSameShopImgSimilarSkns().split(",");
if (productSknArray.length > 0) {
similarProductSknList = Arrays.asList(productSknArray);
}
}
return similarProductSknList;
}
}
... ...
... ... @@ -4,14 +4,15 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.models.SearchFieldBoost;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.service.index.ProductIndexBaseService;
import com.yoho.search.service.helper.FunctionScoreSearchHelper;
import com.yoho.search.service.helper.ProductListHelper;
import com.yoho.search.service.index.ProductIndexBaseService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -26,6 +27,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class SearchLikeHelper {
... ... @@ -329,6 +331,39 @@ public class SearchLikeHelper {
}
/**
* 处理图片相似+文字相似skn列表,把图片相似skn排序
*/
public List<Map<String, Object>> queryProductListWithSimilarImg(List<SearchParam> searchParams, List<String> similarProductSknList) {
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
List<Map<String, Object>> results = new ArrayList<>();
Set<String> existProductSkns = new HashSet<>();
if (CollectionUtils.isEmpty(searchResults) || searchResults.size() != 2) {
return results;
}
SearchResult searchResultFromImg = searchResults.get(0);
List<Map<String, Object>> searchResultFromImgList = searchResultFromImg.getResultList();
if (CollectionUtils.isNotEmpty(searchResultFromImgList)) {
Map<String, Map<String, Object>> searchResultFromImgMap = searchResultFromImgList.stream().collect(Collectors.toMap(p -> MapUtils.getString(p, ProductIndexEsField.productSkn), p -> p));
for (String skn : similarProductSknList) {
if (searchResultFromImgMap.containsKey(skn)) {
results.add(searchResultFromImgMap.get(skn));
existProductSkns.add(skn);
}
}
}
SearchResult searchResultFromWord = searchResults.get(1);
if (CollectionUtils.isNotEmpty(searchResultFromWord.getResultList())) {
for (Map<String, Object> product : searchResultFromWord.getResultList()) {
if (this.isProductSknExists(existProductSkns, product)) {
continue;
}
results.add(product);
}
}
return results;
}
/**
* 店铺外按文字找相似
*
* @param productInfoInEs
... ... @@ -387,7 +422,8 @@ public class SearchLikeHelper {
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
} else {
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.smallSort_smallSort_keyword, productInfoInEs.getString(ProductIndexEsField.smallSort)));// 有些小分类同名,要排除这种情况。
// 有些小分类同名,要排除这种情况。
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.smallSort_smallSort_keyword, productInfoInEs.getString(ProductIndexEsField.smallSort)));
}
return boolFilter;
}
... ...
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;
}
}
... ...