Authored by hugufei

优化product_skn_code

1 -package com.yoho.search.recall.beans.persional;  
2 -  
3 -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;  
4 -import com.yoho.search.base.utils.ISearchConstants;  
5 -import com.yoho.search.base.utils.ProductIndexEsField;  
6 -import com.yoho.search.core.es.model.SearchParam;  
7 -import com.yoho.search.core.es.model.SearchResult;  
8 -import com.yoho.search.recall.config.CacheTimeConstants;  
9 -import com.yoho.search.recall.models.common.ParamQueryFilter;  
10 -import com.yoho.search.recall.models.personal.PageProductIdBitSet;  
11 -import com.yoho.search.service.base.SearchCommonService;  
12 -import org.elasticsearch.index.query.BoolQueryBuilder;  
13 -import org.elasticsearch.index.query.QueryBuilders;  
14 -import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;  
15 -import org.elasticsearch.search.aggregations.Aggregation;  
16 -import org.elasticsearch.search.aggregations.AggregationBuilders;  
17 -import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;  
18 -import org.elasticsearch.search.aggregations.bucket.terms.Terms;  
19 -import org.springframework.beans.factory.annotation.Autowired;  
20 -import org.springframework.stereotype.Component;  
21 -  
22 -import java.util.ArrayList;  
23 -import java.util.Iterator;  
24 -import java.util.List;  
25 -import java.util.Map;  
26 -  
27 -@Component  
28 -public class PageProductIdBitSetComponent extends AbstractPageComponent<PageProductIdBitSet>{  
29 -  
30 - @Autowired  
31 - private SearchCommonService searchCommonService;  
32 -  
33 - /**  
34 - * 获取页面上的skn的bitset  
35 - *  
36 - * @param paramQueryFilter  
37 - * @return  
38 - */  
39 - public PageProductIdBitSet queryPageProductIdBitSet(ParamQueryFilter paramQueryFilter) {  
40 - return this.doRealQuery(paramQueryFilter);  
41 - }  
42 -  
43 - @Override  
44 - public RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {  
45 - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:PAGE_PRODUCT_BITSET:").appendVar(paramQueryFilter.getParamMd5Key());  
46 - }  
47 -  
48 - @Override  
49 - public int cacheTimeInMinute() {  
50 - return CacheTimeConstants.PAGE_SKN_BITSET;  
51 - }  
52 -  
53 - @Override  
54 - protected PageProductIdBitSet doRealQuery(ParamQueryFilter paramQueryFilter) {  
55 - //1、构造请求参数  
56 - SearchParam searchParam = new SearchParam();  
57 - searchParam.setQuery(paramQueryFilter.getParamQuery());  
58 -  
59 - BoolQueryBuilder filter = QueryBuilders.boolQuery();  
60 - filter.must(paramQueryFilter.getParamFilter());  
61 - searchParam.setFiter(filter);  
62 -  
63 - searchParam.setSize(0);  
64 -  
65 - //2、构造聚合参数  
66 - List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();  
67 - aggregationBuilders.add(AggregationBuilders.terms("productIdAgg").field(ProductIndexEsField.productId).size(100000).order(Terms.Order.term(false)));  
68 - searchParam.setAggregationBuilders(aggregationBuilders);  
69 -  
70 - //3、执行查询  
71 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);  
72 -  
73 - //4、构造结果  
74 - Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();  
75 - PageProductIdBitSet pageProductIdBitSet = this.getPageSknBitSetFromAggregationMap(aggregationMap, "productIdAgg");  
76 - return pageProductIdBitSet;  
77 - }  
78 -  
79 - private PageProductIdBitSet getPageSknBitSetFromAggregationMap(Map<String, Aggregation> aggregationMap, String firstAggName) {  
80 - if (!aggregationMap.containsKey(firstAggName)) {  
81 - return null;  
82 - }  
83 - List<Integer> productIdList = new ArrayList<Integer>();  
84 - MultiBucketsAggregation firstAggregation = (MultiBucketsAggregation) aggregationMap.get(firstAggName);  
85 - Iterator<? extends MultiBucketsAggregation.Bucket> firstAggregationIterator = firstAggregation.getBuckets().iterator();  
86 - while (firstAggregationIterator.hasNext()) {  
87 - MultiBucketsAggregation.Bucket bucket = firstAggregationIterator.next();  
88 - Integer value = Integer.valueOf(bucket.getKeyAsString());  
89 - productIdList.add(value);  
90 - }  
91 - PageProductIdBitSet pageProductIdBitSet = new PageProductIdBitSet();  
92 - for (Integer productId : productIdList) {  
93 - pageProductIdBitSet.add(productId);  
94 - }  
95 - return pageProductIdBitSet;  
96 - }  
97 -  
98 -}  
1 -package com.yoho.search.recall.models.personal;  
2 -  
3 -import java.io.Serializable;  
4 -import java.util.BitSet;  
5 -  
6 -public class PageProductIdBitSet implements Serializable{  
7 -  
8 - public static final int maxValue = 2000000;  
9 -  
10 - private static final long serialVersionUID = 7185024266096124078L;  
11 - private BitSet productIdBitSet;  
12 -  
13 - public PageProductIdBitSet(){  
14 - this.productIdBitSet = new BitSet(maxValue);  
15 - }  
16 -  
17 - public void add(int bitSetIndex) {  
18 - if(bitSetIndex>maxValue){  
19 - return;  
20 - }  
21 - this.productIdBitSet.set(bitSetIndex);  
22 - }  
23 -  
24 - public BitSet getProductIdBitSet() {  
25 - return productIdBitSet;  
26 - }  
27 -  
28 -}