|
|
package com.yoho.search.recall.scene.beans.persional;
|
|
|
|
|
|
import com.google.common.hash.BloomFilter;
|
|
|
import com.google.common.hash.Funnels;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.search.common.cache.impls.EhCache;
|
|
|
import com.yoho.search.common.cache.model.CacheObject;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.recall.scene.constants.CacheTimeConstants;
|
|
|
import com.yoho.search.recall.scene.models.common.ParamQueryFilter;
|
|
|
import com.yoho.search.recall.scene.models.personal.PageSknBitSet;
|
|
|
import org.apache.lucene.util.RamUsageEstimator;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
|
public class PageSknBitSetComponent {
|
|
|
public class PageSknBitSetComponent extends AbstractPageComponent {
|
|
|
|
|
|
@Autowired
|
|
|
private EhCache ehCache;
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
/**
|
|
|
* 获取页面上的skn列表
|
|
|
* 获取页面上的skn的bitset
|
|
|
*
|
|
|
* @param paramQueryFilter
|
|
|
* @return
|
|
|
*/
|
|
|
public PageSknBitSet queryPageSknBitSet(ParamQueryFilter paramQueryFilter) {
|
|
|
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
|
|
|
redisKeyBuilder.appendFixed("YOHOSEARCH:").appendFixed("PAGESKN").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
CacheObject cacheObject = ehCache.get(redisKeyBuilder);
|
|
|
if(cacheObject!=null){
|
|
|
return (PageSknBitSet)cacheObject.toObject();
|
|
|
Object value = super.queryWithCache(paramQueryFilter);
|
|
|
return value==null?null:(PageSknBitSet)value;
|
|
|
}
|
|
|
PageSknBitSet pageSknBitSet = new PageSknBitSet();
|
|
|
cacheObject = new CacheObject(pageSknBitSet);
|
|
|
ehCache.addOrUpdate(redisKeyBuilder,cacheObject,30);
|
|
|
|
|
|
@Override
|
|
|
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:PAGE_SKN_BITSET:").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected int cacheTimeInSecond() {
|
|
|
return CacheTimeConstants.PAGE_SKN_BITSET;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected Object doRealQuery(ParamQueryFilter paramQueryFilter) {
|
|
|
//1、构造请求参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(paramQueryFilter.getParamQuery());
|
|
|
searchParam.setFiter(paramQueryFilter.getParamFilter());
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
//2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
aggregationBuilders.add(AggregationBuilders.terms("productIdAgg").field(ProductIndexEsField.productId).size(10000).order(Terms.Order.term(false)));//品类-品牌聚合
|
|
|
searchParam.setAggregationBuilders(aggregationBuilders);
|
|
|
|
|
|
//3、执行查询
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
|
|
|
//4、构造结果
|
|
|
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
|
|
|
PageSknBitSet pageSknBitSet = this.getPageSknBitSetFromAggregationMap(aggregationMap,"productIdAgg");
|
|
|
return pageSknBitSet;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
Charset charset = Charset.forName("utf-8");
|
|
|
BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(charset),2<<21);//指定bloomFilter的容量
|
|
|
for(int i =0;i<100000;i++){
|
|
|
bloomFilter.put(i+"");
|
|
|
private PageSknBitSet getPageSknBitSetFromAggregationMap(Map<String, Aggregation> aggregationMap,String firstAggName){
|
|
|
if(!aggregationMap.containsKey(firstAggName)){
|
|
|
return null;
|
|
|
}
|
|
|
System.out.println(bloomFilter.mightContain("1000000"));
|
|
|
List<Integer> productIdList = new ArrayList<Integer>();
|
|
|
MultiBucketsAggregation firstAggregation = (MultiBucketsAggregation) aggregationMap.get(firstAggName);
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> firstAggregationIterator = firstAggregation.getBuckets().iterator();
|
|
|
while (firstAggregationIterator.hasNext()) {
|
|
|
MultiBucketsAggregation.Bucket bucket = firstAggregationIterator.next();
|
|
|
Integer value = Integer.valueOf(bucket.getKeyAsString());
|
|
|
productIdList.add(value);
|
|
|
}
|
|
|
PageSknBitSet pageSknBitSet = new PageSknBitSet();
|
|
|
for (Integer productId: productIdList) {
|
|
|
pageSknBitSet.add(productId);
|
|
|
}
|
|
|
return pageSknBitSet;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|