|
|
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;
|
|
|
}
|
|
|
} |
...
|
...
|
|