Authored by Gino Zhang

增加【颜色+品类】、【风格+品类】的建议词

@@ -20,7 +20,7 @@ public enum KeywordType { @@ -20,7 +20,7 @@ public enum KeywordType {
20 ProductKeywordToken(5, 6, SuggestionConstants.PRODUCTKEYWORD_TOKEN_ENABLED_KEY), 20 ProductKeywordToken(5, 6, SuggestionConstants.PRODUCTKEYWORD_TOKEN_ENABLED_KEY),
21 BrandKeywordToken(6, 6, SuggestionConstants.BRANDKEYWORD_TOKEN_ENABLED_KEY), 21 BrandKeywordToken(6, 6, SuggestionConstants.BRANDKEYWORD_TOKEN_ENABLED_KEY),
22 22
23 - SortNameWithGender(14, 3, SuggestionConstants.SORTNAME_ENABLED_KEY), 23 + SortNameWithOtherDetail(14, 3, SuggestionConstants.SORTNAME_ENABLED_KEY),
24 24
25 ProductName(8, 1, SuggestionConstants.PRODUCTNAME_ENABLED_KEY), 25 ProductName(8, 1, SuggestionConstants.PRODUCTNAME_ENABLED_KEY),
26 Customized(0, 1), 26 Customized(0, 1),
  1 +package com.yoho.search.consumer.suggests.discover;
  2 +
  3 +import com.yoho.search.base.utils.ISearchConstants;
  4 +import com.yoho.search.consumer.index.common.IYohoIndexService;
  5 +import com.yoho.search.consumer.service.base.ProductSortService;
  6 +import com.yoho.search.consumer.suggests.common.KeywordType;
  7 +import com.yoho.search.core.es.model.SearchParam;
  8 +import com.yoho.search.core.es.model.SearchResult;
  9 +import com.yoho.search.dal.model.ProductSort;
  10 +import org.apache.commons.collections.CollectionUtils;
  11 +import org.apache.commons.lang.StringUtils;
  12 +import org.elasticsearch.index.query.QueryBuilders;
  13 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  14 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  15 +import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.stereotype.Component;
  17 +
  18 +import java.util.*;
  19 +import java.util.stream.Collectors;
  20 +
  21 +/**
  22 + * 【颜色+品类】的搜索建议词,如【白色 卫衣】
  23 + * Created by ginozhang on 2017/3/20.
  24 + */
  25 +@Component
  26 +public class SortNameWithColorSuggestionDiscoverer extends AbstractSuggestionDiscoverer {
  27 +
  28 + @Autowired
  29 + private ProductSortService productSortService;
  30 +
  31 + @Autowired
  32 + private IYohoIndexService yohoIndexService;
  33 +
  34 + @Override
  35 + public int count() {
  36 + // 品类不多 一把捞了
  37 + return 1;
  38 + }
  39 +
  40 + @Override
  41 + public Set<String> getSuggestWordSet(int pageNo, int batchSize) {
  42 + Set<String> keywordSet = new HashSet<>(batchSize);
  43 + List<ProductSort> productSortList = productSortService.getPageLists(0, 5000);
  44 + if (CollectionUtils.isEmpty(productSortList)) {
  45 + return keywordSet;
  46 + }
  47 +
  48 + SearchParam searchParam = new SearchParam();
  49 + searchParam.setSize(0);
  50 + searchParam.setFiter(QueryBuilders.termsQuery("status", 1));
  51 + searchParam.addAbstractAggregationBuilder(AggregationBuilders.terms("smallSortAgg").field("smallSortId").size(100)
  52 + .subAggregation(AggregationBuilders.terms("colorNameAgg").field("colorNames").size(10))
  53 + .subAggregation(AggregationBuilders.terms("styleAgg").field("style").size(10)));
  54 + SearchResult searchResult = yohoIndexService.search(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  55 + if (searchResult != null && searchResult.getAggMaps() != null && searchResult.getAggMaps().get("smallSortAgg") != null) {
  56 + Map<Integer, String> sortNameMap = productSortList.stream().filter(sort -> StringUtils.isNotEmpty(sort.getSortName()))
  57 + .collect(Collectors.toMap(ProductSort::getId, ProductSort::getSortName));
  58 +
  59 + MultiBucketsAggregation aggregation = (MultiBucketsAggregation) searchResult.getAggMaps().get("smallSortAgg");
  60 + Iterator<? extends MultiBucketsAggregation.Bucket> bucketIterator = aggregation.getBuckets().iterator();
  61 + while (bucketIterator.hasNext()) {
  62 + MultiBucketsAggregation.Bucket brandBucket = bucketIterator.next();
  63 + Integer smallSortId = Integer.valueOf(brandBucket.getKeyAsString());
  64 + if (!sortNameMap.containsKey(smallSortId)) {
  65 + continue;
  66 + }
  67 +
  68 + String sortName = sortNameMap.get(smallSortId);
  69 + if (brandBucket.getAggregations() != null && brandBucket.getAggregations().get("colorNameAgg") != null) {
  70 + MultiBucketsAggregation colorNameAgg = brandBucket.getAggregations().get("colorNameAgg");
  71 + Iterator<? extends MultiBucketsAggregation.Bucket> colorNameBucketIterator = colorNameAgg.getBuckets().iterator();
  72 + while (colorNameBucketIterator.hasNext()) {
  73 + MultiBucketsAggregation.Bucket colorNameBucket = colorNameBucketIterator.next();
  74 + String colorName = colorNameBucket.getKeyAsString();
  75 + if (StringUtils.isNotEmpty(colorName) && colorName.length() > 1) {
  76 + for (String itemName : sortName.split("\\/")) {
  77 + keywordSet.add(colorName + " " + itemName);
  78 + }
  79 + }
  80 + }
  81 + }
  82 +
  83 + if (brandBucket.getAggregations() != null && brandBucket.getAggregations().get("styleAgg") != null) {
  84 + MultiBucketsAggregation styleAgg = brandBucket.getAggregations().get("styleAgg");
  85 + Iterator<? extends MultiBucketsAggregation.Bucket> styleBucketIterator = styleAgg.getBuckets().iterator();
  86 + while (styleBucketIterator.hasNext()) {
  87 + MultiBucketsAggregation.Bucket styleBucket = styleBucketIterator.next();
  88 + String styleName = styleBucket.getKeyAsString();
  89 + if (StringUtils.isNotEmpty(styleName) && styleName.length() > 1) {
  90 + for (String itemName : sortName.split("\\/")) {
  91 + keywordSet.add(styleName + " " + itemName);
  92 + }
  93 + }
  94 + }
  95 + }
  96 + }
  97 + }
  98 +
  99 + return keywordSet;
  100 + }
  101 +
  102 + @Override
  103 + public KeywordType getKeywordType() {
  104 + return KeywordType.SortNameWithOtherDetail;
  105 + }
  106 +}
@@ -7,14 +7,14 @@ import org.apache.commons.collections.CollectionUtils; @@ -7,14 +7,14 @@ import org.apache.commons.collections.CollectionUtils;
7 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Component; 8 import org.springframework.stereotype.Component;
9 9
10 -import java.util.Arrays; 10 +import java.util.ArrayList;
11 import java.util.HashSet; 11 import java.util.HashSet;
12 import java.util.List; 12 import java.util.List;
13 import java.util.Set; 13 import java.util.Set;
14 14
15 /** 15 /**
16 * 【品类+性别】的搜索建议词,如【卫衣 女】 16 * 【品类+性别】的搜索建议词,如【卫衣 女】
17 - * Created by ginozhang on 2017/3/2. 17 + * Created by ginozhang on 2017/3/20.
18 */ 18 */
19 @Component 19 @Component
20 public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDiscoverer { 20 public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDiscoverer {
@@ -37,7 +37,7 @@ public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDi @@ -37,7 +37,7 @@ public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDi
37 // 有一些sortName是用斜杠连接多个词(如[凉鞋/凉拖]),需要split处理一下 37 // 有一些sortName是用斜杠连接多个词(如[凉鞋/凉拖]),需要split处理一下
38 String sortName = productSort.getSortName(); 38 String sortName = productSort.getSortName();
39 for (String itemName : sortName.split("\\/")) { 39 for (String itemName : sortName.split("\\/")) {
40 - sortNameSet.addAll(buildSortNameWithGenderKeywords(itemName)); 40 + sortNameSet.addAll(buildSortNameWithGenderKeywords(itemName, productSort.getGender()));
41 } 41 }
42 } 42 }
43 } 43 }
@@ -45,12 +45,30 @@ public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDi @@ -45,12 +45,30 @@ public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDi
45 return sortNameSet; 45 return sortNameSet;
46 } 46 }
47 47
48 - private List<String> buildSortNameWithGenderKeywords(String itemName) {  
49 - return Arrays.asList(itemName + " 男", itemName + " 女"); 48 + private List<String> buildSortNameWithGenderKeywords(String itemName, String gender) {
  49 + List<String> resultList = new ArrayList<>();
  50 + if (gender != null) {
  51 + switch (gender) {
  52 + case "1":
  53 + // 男性
  54 + resultList.add(itemName + " 男");
  55 + break;
  56 + case "2":
  57 + // 女性
  58 + resultList.add(itemName + " 女");
  59 + break;
  60 + default:
  61 + // 通用
  62 + resultList.add(itemName + " 男");
  63 + resultList.add(itemName + " 女");
  64 + }
  65 + }
  66 +
  67 + return resultList;
50 } 68 }
51 69
52 @Override 70 @Override
53 public KeywordType getKeywordType() { 71 public KeywordType getKeywordType() {
54 - return KeywordType.SortNameWithGender; 72 + return KeywordType.SortNameWithOtherDetail;
55 } 73 }
56 } 74 }