...
|
...
|
@@ -11,6 +11,7 @@ import javax.annotation.PostConstruct; |
|
|
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.common.lucene.search.function.CombineFunction;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
...
|
...
|
@@ -19,7 +20,7 @@ import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; |
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.sort.SortBuilder;
|
|
|
import org.elasticsearch.search.sort.SortBuilders;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
...
|
...
|
@@ -36,6 +37,7 @@ import com.yoho.search.common.cache.model.SearchCache; |
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.models.YohoFilterFunctionBuilders;
|
|
|
import com.yoho.search.service.aggregations.common.SimpleFieldAgg;
|
|
|
import com.yoho.search.service.base.SearchCacheService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
...
|
...
|
@@ -64,13 +66,13 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCacheService searchCacheService;
|
|
|
@Autowired
|
|
|
private SearchCacheFactory searchCacheFactory;
|
|
|
private SearchCache searchLikeSearchCache;
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
searchLikeSearchCache = searchCacheFactory.getSearchLikeSearchCache();
|
...
|
...
|
@@ -119,9 +121,9 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSknList));
|
|
|
searchParam.setFiter(boolFilter);
|
|
|
// 2、设置聚合条件,获取所有的品类和品牌
|
|
|
TermsBuilder smallSortIdAgg = AggregationBuilders.terms(SMALL_SORT_AGG_NAME).field(ProductIndexEsField.smallSortId).size(100);
|
|
|
TermsBuilder brandIdAgg = AggregationBuilders.terms(BRAND_AGG_NAME).field(ProductIndexEsField.brandId).size(100);
|
|
|
List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>();
|
|
|
TermsAggregationBuilder smallSortIdAgg = AggregationBuilders.terms(SMALL_SORT_AGG_NAME).field(ProductIndexEsField.smallSortId).size(100);
|
|
|
TermsAggregationBuilder brandIdAgg = AggregationBuilders.terms(BRAND_AGG_NAME).field(ProductIndexEsField.brandId).size(100);
|
|
|
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
|
|
|
aggregationBuilders.add(smallSortIdAgg);
|
|
|
aggregationBuilders.add(brandIdAgg);
|
|
|
searchParam.setAggregationBuilders(aggregationBuilders);
|
...
|
...
|
@@ -129,7 +131,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(0);
|
|
|
// 4、先从缓存中获取,如果能取到,则直接返回
|
|
|
JSONObject sortAndBrandJSONObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache,ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
JSONObject sortAndBrandJSONObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (sortAndBrandJSONObject != null) {
|
|
|
return sortAndBrandJSONObject;
|
|
|
}
|
...
|
...
|
@@ -146,7 +148,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
sortAndBrandJSONObject = new JSONObject();
|
|
|
sortAndBrandJSONObject.put(BRAND_IDS, brandIds);
|
|
|
sortAndBrandJSONObject.put(SMALL_SORT_IDS, smallSortIds);
|
|
|
searchCacheService.addJSONObjectToCache(searchLikeSearchCache,ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sortAndBrandJSONObject);
|
|
|
searchCacheService.addJSONObjectToCache(searchLikeSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sortAndBrandJSONObject);
|
|
|
return sortAndBrandJSONObject;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -185,7 +187,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
List<SimpleFieldAgg> simpleFieldAggs = new ArrayList<SimpleFieldAgg>();
|
|
|
simpleFieldAggs.add(new SimpleFieldAgg(SMALL_SORT_AGG_NAME, ProductIndexEsField.smallSortId, 100));
|
|
|
simpleFieldAggs.add(new SimpleFieldAgg(BRAND_AGG_NAME, ProductIndexEsField.brandId, 100));
|
|
|
AbstractAggregationBuilder smallSortAndBrandAgg = AggCommonHelper.getTopHitAggregation(simpleFieldAggs, similarProductOrder, 2);
|
|
|
AbstractAggregationBuilder<?> smallSortAndBrandAgg = AggCommonHelper.getTopHitAggregation(simpleFieldAggs, similarProductOrder, 2);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(smallSortAndBrandAgg));
|
|
|
|
|
|
// 5、设置分页
|
...
|
...
|
@@ -193,7 +195,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 6、先从缓存中获取,如果能取到,则直接返回
|
|
|
JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(searchLikeSearchCache,ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(searchLikeSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (recommendedSknJSONArray != null) {
|
|
|
return this.jsonArrayToList(recommendedSknJSONArray, String.class);
|
|
|
}
|
...
|
...
|
@@ -228,7 +230,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
searchParam.setAggregationBuilders(null);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
List<SortBuilder> sortBuilders = new ArrayList<>();
|
|
|
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
|
|
|
sortBuilders.add(SortBuilders.fieldSort(ES_SCORE_FIELD).order(SortOrder.DESC));
|
|
|
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.salesNum).order(SortOrder.DESC));
|
|
|
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.firstShelveTime).order(SortOrder.DESC));
|
...
|
...
|
@@ -236,7 +238,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
// 2.从缓存中获取数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache,indexName, searchParam);
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchLikeSearchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
return new SearchApiResult().setData(cacheObject);
|
|
|
}
|
...
|
...
|
@@ -253,7 +255,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
dataMap.put("product_list", productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()));
|
|
|
// 5.将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(searchLikeSearchCache,indexName, searchParam, dataMap);
|
|
|
searchCacheService.addJSONObjectToCache(searchLikeSearchCache, indexName, searchParam, dataMap);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -265,7 +267,7 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.isSeckill, "Y"));
|
|
|
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.isFobbiden, 1));
|
|
|
boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.attribute, 2));
|
|
|
|
|
|
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isOutlets, 2));
|
|
|
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
|
...
|
...
|
@@ -293,11 +295,10 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
}
|
|
|
|
|
|
private QueryBuilder builderProductQueryBuilder(Map<String, String> paramMap, List<String> recommendedSknList) {
|
|
|
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
|
|
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
|
|
|
YohoFilterFunctionBuilders filterFunctionBuilders = new YohoFilterFunctionBuilders();
|
|
|
// 如果参数中包含firstProductSkn,则放在第一个
|
|
|
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN)) {
|
|
|
functionScoreQueryBuilder.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "")),
|
|
|
filterFunctionBuilders.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "")),
|
|
|
ScoreFunctionBuilders.weightFactorFunction(100000));
|
|
|
}
|
|
|
// 针对推荐出来的SKN做加分
|
...
|
...
|
@@ -305,11 +306,13 @@ public class SimilarProductServiceImpl extends BaseService implements ISimilarPr |
|
|
Map<Integer, List<String>> recommendSknMap = this.splitProductSknList(recommendedSknList, 2);
|
|
|
float currentGroupScore = 1000;
|
|
|
for (Map.Entry<Integer, List<String>> entry : recommendSknMap.entrySet()) {
|
|
|
functionScoreQueryBuilder.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, entry.getValue()),
|
|
|
filterFunctionBuilders.add(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, entry.getValue()),
|
|
|
ScoreFunctionBuilders.weightFactorFunction(currentGroupScore));
|
|
|
currentGroupScore = currentGroupScore - 10;
|
|
|
}
|
|
|
}
|
|
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery(), filterFunctionBuilders.getFilterFunctionBuilders());
|
|
|
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
|
|
|
return functionScoreQueryBuilder;
|
|
|
}
|
|
|
|
...
|
...
|
|