|
|
package com.yoho.search.recall.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.config.CacheTimeConstants;
|
|
|
import com.yoho.search.recall.models.common.ParamQueryFilter;
|
|
|
import com.yoho.search.recall.models.personal.PageProductIdBitSet;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
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 PageProductIdBitSetComponent extends AbstractPageComponent<PageProductIdBitSet>{
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
/**
|
|
|
* 获取页面上的skn的bitset
|
|
|
*
|
|
|
* @param paramQueryFilter
|
|
|
* @return
|
|
|
*/
|
|
|
public PageProductIdBitSet queryPageProductIdBitSet(ParamQueryFilter paramQueryFilter) {
|
|
|
return this.doRealQuery(paramQueryFilter);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:PAGE_PRODUCT_BITSET:").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.PAGE_SKN_BITSET;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected PageProductIdBitSet doRealQuery(ParamQueryFilter paramQueryFilter) {
|
|
|
//1、构造请求参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(paramQueryFilter.getParamQuery());
|
|
|
|
|
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
|
|
filter.must(paramQueryFilter.getParamFilter());
|
|
|
searchParam.setFiter(filter);
|
|
|
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
//2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
aggregationBuilders.add(AggregationBuilders.terms("productIdAgg").field(ProductIndexEsField.productId).size(100000).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();
|
|
|
PageProductIdBitSet pageProductIdBitSet = this.getPageSknBitSetFromAggregationMap(aggregationMap, "productIdAgg");
|
|
|
return pageProductIdBitSet;
|
|
|
}
|
|
|
|
|
|
private PageProductIdBitSet 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);
|
|
|
}
|
|
|
PageProductIdBitSet pageProductIdBitSet = new PageProductIdBitSet();
|
|
|
for (Integer productId : productIdList) {
|
|
|
pageProductIdBitSet.add(productId);
|
|
|
}
|
|
|
return pageProductIdBitSet;
|
|
|
}
|
|
|
|
|
|
} |