Authored by Gino Zhang

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

... ... @@ -20,7 +20,7 @@ public enum KeywordType {
ProductKeywordToken(5, 6, SuggestionConstants.PRODUCTKEYWORD_TOKEN_ENABLED_KEY),
BrandKeywordToken(6, 6, SuggestionConstants.BRANDKEYWORD_TOKEN_ENABLED_KEY),
SortNameWithGender(14, 3, SuggestionConstants.SORTNAME_ENABLED_KEY),
SortNameWithOtherDetail(14, 3, SuggestionConstants.SORTNAME_ENABLED_KEY),
ProductName(8, 1, SuggestionConstants.PRODUCTNAME_ENABLED_KEY),
Customized(0, 1),
... ...
package com.yoho.search.consumer.suggests.discover;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.service.base.ProductSortService;
import com.yoho.search.consumer.suggests.common.KeywordType;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.dal.model.ProductSort;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
/**
* 【颜色+品类】的搜索建议词,如【白色 卫衣】
* Created by ginozhang on 2017/3/20.
*/
@Component
public class SortNameWithColorSuggestionDiscoverer extends AbstractSuggestionDiscoverer {
@Autowired
private ProductSortService productSortService;
@Autowired
private IYohoIndexService yohoIndexService;
@Override
public int count() {
// 品类不多 一把捞了
return 1;
}
@Override
public Set<String> getSuggestWordSet(int pageNo, int batchSize) {
Set<String> keywordSet = new HashSet<>(batchSize);
List<ProductSort> productSortList = productSortService.getPageLists(0, 5000);
if (CollectionUtils.isEmpty(productSortList)) {
return keywordSet;
}
SearchParam searchParam = new SearchParam();
searchParam.setSize(0);
searchParam.setFiter(QueryBuilders.termsQuery("status", 1));
searchParam.addAbstractAggregationBuilder(AggregationBuilders.terms("smallSortAgg").field("smallSortId").size(100)
.subAggregation(AggregationBuilders.terms("colorNameAgg").field("colorNames").size(10))
.subAggregation(AggregationBuilders.terms("styleAgg").field("style").size(10)));
SearchResult searchResult = yohoIndexService.search(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (searchResult != null && searchResult.getAggMaps() != null && searchResult.getAggMaps().get("smallSortAgg") != null) {
Map<Integer, String> sortNameMap = productSortList.stream().filter(sort -> StringUtils.isNotEmpty(sort.getSortName()))
.collect(Collectors.toMap(ProductSort::getId, ProductSort::getSortName));
MultiBucketsAggregation aggregation = (MultiBucketsAggregation) searchResult.getAggMaps().get("smallSortAgg");
Iterator<? extends MultiBucketsAggregation.Bucket> bucketIterator = aggregation.getBuckets().iterator();
while (bucketIterator.hasNext()) {
MultiBucketsAggregation.Bucket brandBucket = bucketIterator.next();
Integer smallSortId = Integer.valueOf(brandBucket.getKeyAsString());
if (!sortNameMap.containsKey(smallSortId)) {
continue;
}
String sortName = sortNameMap.get(smallSortId);
if (brandBucket.getAggregations() != null && brandBucket.getAggregations().get("colorNameAgg") != null) {
MultiBucketsAggregation colorNameAgg = brandBucket.getAggregations().get("colorNameAgg");
Iterator<? extends MultiBucketsAggregation.Bucket> colorNameBucketIterator = colorNameAgg.getBuckets().iterator();
while (colorNameBucketIterator.hasNext()) {
MultiBucketsAggregation.Bucket colorNameBucket = colorNameBucketIterator.next();
String colorName = colorNameBucket.getKeyAsString();
if (StringUtils.isNotEmpty(colorName) && colorName.length() > 1) {
for (String itemName : sortName.split("\\/")) {
keywordSet.add(colorName + " " + itemName);
}
}
}
}
if (brandBucket.getAggregations() != null && brandBucket.getAggregations().get("styleAgg") != null) {
MultiBucketsAggregation styleAgg = brandBucket.getAggregations().get("styleAgg");
Iterator<? extends MultiBucketsAggregation.Bucket> styleBucketIterator = styleAgg.getBuckets().iterator();
while (styleBucketIterator.hasNext()) {
MultiBucketsAggregation.Bucket styleBucket = styleBucketIterator.next();
String styleName = styleBucket.getKeyAsString();
if (StringUtils.isNotEmpty(styleName) && styleName.length() > 1) {
for (String itemName : sortName.split("\\/")) {
keywordSet.add(styleName + " " + itemName);
}
}
}
}
}
}
return keywordSet;
}
@Override
public KeywordType getKeywordType() {
return KeywordType.SortNameWithOtherDetail;
}
}
... ...
... ... @@ -7,14 +7,14 @@ import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 【品类+性别】的搜索建议词,如【卫衣 女】
* Created by ginozhang on 2017/3/2.
* Created by ginozhang on 2017/3/20.
*/
@Component
public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDiscoverer {
... ... @@ -37,7 +37,7 @@ public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDi
// 有一些sortName是用斜杠连接多个词(如[凉鞋/凉拖]),需要split处理一下
String sortName = productSort.getSortName();
for (String itemName : sortName.split("\\/")) {
sortNameSet.addAll(buildSortNameWithGenderKeywords(itemName));
sortNameSet.addAll(buildSortNameWithGenderKeywords(itemName, productSort.getGender()));
}
}
}
... ... @@ -45,12 +45,30 @@ public class SortNameWithGenderSuggestionDiscoverer extends AbstractSuggestionDi
return sortNameSet;
}
private List<String> buildSortNameWithGenderKeywords(String itemName) {
return Arrays.asList(itemName + " 男", itemName + " 女");
private List<String> buildSortNameWithGenderKeywords(String itemName, String gender) {
List<String> resultList = new ArrayList<>();
if (gender != null) {
switch (gender) {
case "1":
// 男性
resultList.add(itemName + " 男");
break;
case "2":
// 女性
resultList.add(itemName + " 女");
break;
default:
// 通用
resultList.add(itemName + " 男");
resultList.add(itemName + " 女");
}
}
return resultList;
}
@Override
public KeywordType getKeywordType() {
return KeywordType.SortNameWithGender;
return KeywordType.SortNameWithOtherDetail;
}
}
... ...