|
|
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
|
+} |