Authored by hugufei

添加按productId实时召回的逻辑

package com.yoho.search.recall.scene.beans.persional;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
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 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.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Component
public class PageSknBitSetComponent extends AbstractPageComponent {
@Autowired
private SearchCommonService searchCommonService;
/**
* 获取页面上的skn的bitset
*
* @param paramQueryFilter
* @return
*/
public PageSknBitSet queryPageSknBitSet(ParamQueryFilter paramQueryFilter) {
Object value = super.queryWithCache(paramQueryFilter);
return value==null?null:(PageSknBitSet)value;
}
@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;
}
private PageSknBitSet getPageSknBitSetFromAggregationMap(Map<String, Aggregation> aggregationMap,String firstAggName){
if(!aggregationMap.containsKey(firstAggName)){
return null;
}
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;
}
}
package com.yoho.search.recall.scene.models.personal;
import java.io.Serializable;
import java.util.BitSet;
public class PageSknBitSet implements Serializable{
public static final int maxValue = 2000000;
private static final long serialVersionUID = 7185024266096124078L;
private BitSet sknBitSet;
public PageSknBitSet(){
this.sknBitSet = new BitSet(maxValue);
}
public void add(int bitSetIndex) {
if(bitSetIndex>maxValue){
return;
}
this.sknBitSet.set(bitSetIndex);
}
public boolean exist(int bitSetIndex) {
return sknBitSet.get(bitSetIndex);
}
}