Authored by unknown

similarskn

@@ -4,6 +4,7 @@ import java.util.Map; @@ -4,6 +4,7 @@ import java.util.Map;
4 4
5 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
6 6
  7 +import com.yoho.search.service.scene.searchlike.BigdataSimilarSknService;
7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Controller; 9 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RequestMapping;
@@ -37,6 +38,8 @@ public class SearchLikeSecneController { @@ -37,6 +38,8 @@ public class SearchLikeSecneController {
37 private IProductListWithSupplyService productListWithSupplyService; 38 private IProductListWithSupplyService productListWithSupplyService;
38 @Autowired 39 @Autowired
39 private ISimilarProductService similarProductService; 40 private ISimilarProductService similarProductService;
  41 + @Autowired
  42 + private BigdataSimilarSknService bigdataSimilarSknService;
40 43
41 @RequestMapping(method = RequestMethod.GET, value = "/searchLike") 44 @RequestMapping(method = RequestMethod.GET, value = "/searchLike")
42 @ResponseBody 45 @ResponseBody
@@ -56,7 +59,8 @@ public class SearchLikeSecneController { @@ -56,7 +59,8 @@ public class SearchLikeSecneController {
56 @ResponseBody 59 @ResponseBody
57 public SearchApiResult searchLikeNotInShop(HttpServletRequest request) { 60 public SearchApiResult searchLikeNotInShop(HttpServletRequest request) {
58 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); 61 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
59 - return searchLikeNotInShopService.searchLikeNotInShop(paramMap); 62 +// return searchLikeNotInShopService.searchLikeNotInShop(paramMap);
  63 + return bigdataSimilarSknService.searchLikeSimilarSknNotInShop(paramMap);
60 } 64 }
61 65
62 /** 66 /**
  1 +package com.yoho.search.service.scene.searchlike;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.base.utils.CollectionUtils;
  5 +import com.yoho.search.base.utils.ProductIndexEsField;
  6 +import com.yoho.search.core.es.model.SearchParam;
  7 +import com.yoho.search.dal.model.SimilarSkn;
  8 +import com.yoho.search.models.SearchApiResult;
  9 +import com.yoho.search.service.base.SearchDynamicConfigService;
  10 +import com.yoho.search.service.base.SearchRequestParams;
  11 +import com.yoho.search.service.base.index.ProductIndexBaseService;
  12 +import org.apache.commons.beanutils.BeanUtils;
  13 +import org.apache.commons.lang.StringUtils;
  14 +import org.elasticsearch.index.query.BoolQueryBuilder;
  15 +import org.elasticsearch.index.query.QueryBuilder;
  16 +import org.elasticsearch.index.query.QueryBuilders;
  17 +import org.elasticsearch.search.sort.SortBuilder;
  18 +import org.elasticsearch.search.sort.SortBuilders;
  19 +import org.elasticsearch.search.sort.SortOrder;
  20 +import org.slf4j.Logger;
  21 +import org.slf4j.LoggerFactory;
  22 +import org.springframework.beans.factory.annotation.Autowired;
  23 +import org.springframework.stereotype.Service;
  24 +
  25 +import java.util.ArrayList;
  26 +import java.util.Arrays;
  27 +import java.util.List;
  28 +import java.util.Map;
  29 +
  30 +@Service
  31 +public class BigdataSimilarSknService {
  32 + private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknService.class);
  33 +
  34 + @Autowired
  35 + private SearchLikeHelper searchLikeHelper;
  36 + @Autowired
  37 + private ProductIndexBaseService productIndexBaseService;
  38 + @Autowired
  39 + private SearchDynamicConfigService searchDynamicConfigService;
  40 +
  41 + public SearchApiResult searchLikeSimilarSknNotInShop(Map<String, String> paramMap) {
  42 + try {
  43 + // 1、获取参数
  44 + String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
  45 + if (StringUtils.isBlank(productSkn)) {
  46 + return new SearchApiResult().setCode(400).setMessage("请输入SKN");
  47 + }
  48 + // 2、检测分页参数【默认30条,最多60条】
  49 + int pageSize = searchLikeHelper.getPageSize(paramMap);
  50 + // 2、获取当前查询的SKN的基本信息
  51 + JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
  52 + if (productInfoInEs == null) {
  53 + return new SearchApiResult().setCode(400).setMessage("SKN不存在");
  54 + }
  55 + //查similarskn
  56 + SimilarSkn similarSkn = new SimilarSkn();
  57 + BeanUtils.populate(similarSkn, searchLikeHelper.searchSimilarSkn(productSkn));
  58 +
  59 + List<SearchParam> searchParams = new ArrayList<>();
  60 + if (StringUtils.isNotEmpty(similarSkn.getDiffShopActionSimilarSkns())) {
  61 + List<String> diffShopActionSimilarSkns = CollectionUtils.arrayToList(similarSkn.getDiffShopActionSimilarSkns().split(","));
  62 + searchParams.add(builderSimilarSknSearchParam(productInfoInEs, diffShopActionSimilarSkns));
  63 + }
  64 + if (StringUtils.isNotEmpty(similarSkn.getDiffShopActionSimilarSkns())) {
  65 + List<String> diffShopImgSimilarSkns = CollectionUtils.arrayToList(similarSkn.getDiffShopImgSimilarSkns().split(","));
  66 + searchParams.add(builderSimilarSknSearchParam(productInfoInEs, diffShopImgSimilarSkns));
  67 + }
  68 +
  69 + int sameSortCount = searchDynamicConfigService.getSearchLikeNotInShopSameSortPercent() * pageSize / 100;
  70 + searchParams.add(builderSearchParam(productInfoInEs, Arrays.asList(productSkn), sameSortCount, true));
  71 + searchParams.add(builderSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize - sameSortCount, false));
  72 +
  73 + // 4、获取搜索结果[截取条数]
  74 + List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
  75 + if (tempProductList.size() > pageSize) {
  76 + tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
  77 + }
  78 + // 5、构造真实返回结果
  79 + List<Map<String, Object>> productListResults = new ArrayList<>();
  80 + if (!tempProductList.isEmpty()) {
  81 + productListResults = productIndexBaseService.getProductListWithPricePlan(tempProductList);
  82 + }
  83 + JSONObject result = new JSONObject();
  84 + result.put("page", 1);
  85 + result.put("page_total", 1);
  86 + result.put("page_size", pageSize);
  87 + result.put("total", productListResults.size());
  88 + result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
  89 + result.put("product_list", productListResults);
  90 + // 8、结果加入缓存
  91 + return new SearchApiResult().setData(result);
  92 + } catch (Exception e) {
  93 + logger.error(e.getMessage(), e);
  94 + return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
  95 + }
  96 + }
  97 +
  98 + private SearchParam builderSimilarSknSearchParam(JSONObject productInfoInEs, List<String> productSkns) {
  99 + // 1、设置SearchParam
  100 + SearchParam searchParam = new SearchParam();
  101 + // 2)设置query和filter
  102 + searchParam.setQuery(builderSimilarSknQueryBuilder(productInfoInEs));
  103 + searchParam.setFiter(builderSimilarSknFilterBuilder(productInfoInEs, productSkns));
  104 + // 3、设置排序规则[按打分排序]
  105 + List<SortBuilder<?>> sortBuilders = new ArrayList<>();
  106 + sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
  107 + searchParam.setSortBuilders(sortBuilders);
  108 + // 4、设置分页参数
  109 + searchParam.setOffset(0);
  110 + searchParam.setSize(productSkns.size());
  111 + // 5)设置返回的参数【节省带宽】
  112 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  113 + searchParam.setIncludeFields(includeFields);
  114 + return searchParam;
  115 + }
  116 +
  117 + private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> productSkns, int pageSize, boolean inSameSort) {
  118 + // 1、设置SearchParam
  119 + SearchParam searchParam = new SearchParam();
  120 + // 2)设置query和filter
  121 + searchParam.setQuery(this.builderQueryBuilder(productInfoInEs, inSameSort ? "30%" : "20%"));
  122 + searchParam.setFiter(this.builderFilterBuilder(productInfoInEs, productSkns, inSameSort));
  123 + // 3、设置排序规则[按打分排序]
  124 + List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
  125 + sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
  126 + searchParam.setSortBuilders(sortBuilders);
  127 + // 4、设置分页参数
  128 + searchParam.setOffset(0);
  129 + searchParam.setSize(pageSize);
  130 + // 5)设置返回的参数【节省带宽】
  131 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  132 + searchParam.setIncludeFields(includeFields);
  133 + return searchParam;
  134 + }
  135 +
  136 + private QueryBuilder builderFilterBuilder(JSONObject productInfoInEs, List<String> notProductSkns, boolean inSameSort) {
  137 + String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
  138 + boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
  139 + BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(notProductSkns, isGlobal);
  140 + // 1)设置此SKN相关的性别过滤条件
  141 + String gender = productInfoInEs.getString(ProductIndexEsField.gender);
  142 + List<String> genderList = searchLikeHelper.getGenderInfo(gender);
  143 + if (genderList != null && !genderList.isEmpty()) {
  144 + boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
  145 + }
  146 + // 2)设置品牌或店铺信息
  147 + Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
  148 + Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
  149 + if (searchLikeHelper.isLegalInteger(shopId)) {
  150 + boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
  151 + } else if (searchLikeHelper.isLegalInteger(brandId)) {
  152 + boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
  153 + }
  154 + // 3)设置品类信息
  155 + Integer middleSortId = productInfoInEs.getInteger(ProductIndexEsField.middleSortId);
  156 + if (inSameSort) {
  157 + boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
  158 + } else {
  159 + boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
  160 + boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.smallSort_smallSort_keyword, productInfoInEs.getString(ProductIndexEsField.smallSort)));// 有些小分类同名,要排除这种情况。
  161 + }
  162 + return boolFilter;
  163 + }
  164 +
  165 + private QueryBuilder builderSimilarSknFilterBuilder(JSONObject productInfoInEs, List<String> inProductSkns) {
  166 + String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
  167 + boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
  168 + BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
  169 + // 1)设置此SKN相关的性别过滤条件
  170 + String gender = productInfoInEs.getString(ProductIndexEsField.gender);
  171 + List<String> genderList = searchLikeHelper.getGenderInfo(gender);
  172 + if (genderList != null && !genderList.isEmpty()) {
  173 + boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
  174 + }
  175 + //設置productskn
  176 + if (inProductSkns != null && !inProductSkns.isEmpty()) {
  177 + boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, inProductSkns));
  178 + }
  179 + // 2)设置品牌或店铺信息
  180 + Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
  181 + Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
  182 + if (searchLikeHelper.isLegalInteger(shopId)) {
  183 + boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
  184 + } else if (searchLikeHelper.isLegalInteger(brandId)) {
  185 + boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
  186 + }
  187 + return boolFilter;
  188 + }
  189 +
  190 + private QueryBuilder builderQueryBuilder(JSONObject productInfoInEs, String minimumShouldMatch) {
  191 + String queryString = searchLikeHelper.genYohoQueryStringWithOutBrandName(productInfoInEs);
  192 + String productFeatureFactor = productInfoInEs.getString(ProductIndexEsField.productFeatureFactor);
  193 + QueryBuilder queryBuilder = searchLikeHelper.genSearchLikeQueryBuilder(queryString, minimumShouldMatch, productFeatureFactor);
  194 + return queryBuilder;
  195 + }
  196 +
  197 + private QueryBuilder builderSimilarSknQueryBuilder(JSONObject productInfoInEs) {
  198 + return searchLikeHelper.genProductFeatureQueryBuilder( productInfoInEs.getString(ProductIndexEsField.productFeatureFactor));
  199 + }
  200 +
  201 +}
@@ -18,6 +18,7 @@ import org.elasticsearch.index.query.QueryBuilder; @@ -18,6 +18,7 @@ import org.elasticsearch.index.query.QueryBuilder;
18 import org.elasticsearch.index.query.QueryBuilders; 18 import org.elasticsearch.index.query.QueryBuilders;
19 import org.springframework.beans.factory.annotation.Autowired; 19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Component; 20 import org.springframework.stereotype.Component;
  21 +import org.springframework.util.CollectionUtils;
21 22
22 import javax.annotation.PostConstruct; 23 import javax.annotation.PostConstruct;
23 import java.util.*; 24 import java.util.*;
@@ -264,4 +265,33 @@ public class SearchLikeHelper { @@ -264,4 +265,33 @@ public class SearchLikeHelper {
264 return jsonArray; 265 return jsonArray;
265 } 266 }
266 267
  268 + public Map<String, Object> searchSimilarSkn(String productSkn) {
  269 + SearchParam searchParam = new SearchParam();
  270 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  271 + boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkn));
  272 + searchParam.setFiter(boolFilter);
  273 + searchParam.setAggregationBuilders(null);
  274 + searchParam.setSize(1);
  275 +
  276 + String productIndexName = ISearchConstants.INDEX_NAME_BIGDATESIMILARSKNINDEX;
  277 + SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
  278 + if (searchResult == null) {
  279 + return null;
  280 + }
  281 + List<Map<String, Object>> similarSknResults = searchResult.getResultList();
  282 + if (CollectionUtils.isEmpty(similarSknResults) || similarSknResults.get(0) == null) {
  283 + return null;
  284 + }
  285 + return similarSknResults.get(0);
  286 + }
  287 +
  288 + /**
  289 + * 找相似相关的匹配打分策略
  290 + * @param productFeatureFactor
  291 + * @return
  292 + */
  293 + public QueryBuilder genProductFeatureQueryBuilder(String productFeatureFactor) {
  294 + return functionScoreSearchHelper.buildFunctionScoreQueryBuildWithProductFeature(QueryBuilders.matchAllQuery(), productFeatureFactor);
  295 + }
  296 +
267 } 297 }