...
|
...
|
@@ -57,42 +57,7 @@ public class ShopListService { |
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "SHOP_LIST", includeParams = {"keyword", "is_encode", "contain_global", "contain_ufo"})
|
|
|
public SearchApiResult searchShopList(Map<String, String> paramMap) {
|
|
|
// 1、获取搜索店铺的关键词
|
|
|
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
|
|
|
if (StringUtils.isBlank(keyword)) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传keyword");
|
|
|
}
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 2、构建query
|
|
|
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
|
|
|
queryBuilder.operator(Operator.OR);
|
|
|
List<SearchField> shopSearchFields = SearchFieldUtils.getShopNameSearchFields();
|
|
|
for (SearchField searchField : shopSearchFields) {
|
|
|
queryBuilder.field(searchField.getEsField(), searchField.getBoost());
|
|
|
}
|
|
|
queryBuilder.minimumShouldMatch("100%");
|
|
|
// 2.1 全球购得分减半
|
|
|
FilterFunctionBuilder[] filterFunctionBuilders = new FilterFunctionBuilder[3];
|
|
|
filterFunctionBuilders[0] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.isGlobal, "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
|
|
|
filterFunctionBuilders[1] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.isUfo, "Y"), ScoreFunctionBuilders.weightFactorFunction(0.6f));
|
|
|
filterFunctionBuilders[2] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.csBrandKeyword, keyword), ScoreFunctionBuilders.weightFactorFunction(10f));
|
|
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, filterFunctionBuilders);
|
|
|
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
|
|
|
searchParam.setQuery(functionScoreQueryBuilder);
|
|
|
|
|
|
// 3、构建filter
|
|
|
// 3.1默认条件
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));
|
|
|
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.isOutlets, 2));
|
|
|
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.attribute, 1));
|
|
|
boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.isSeckill, "Y"));
|
|
|
|
|
|
private BoolQueryBuilder buildShopFilter(Map<String, String> paramMap) {
|
|
|
// 3.2店铺过滤器
|
|
|
BoolQueryBuilder shopFilter = QueryBuilders.boolQuery();
|
|
|
// 3.2.1 全球购店铺过滤器
|
...
|
...
|
@@ -127,21 +92,60 @@ public class ShopListService { |
|
|
if (!containGlobal && !containUfo) {
|
|
|
shopFilter.must(yohoShopFilter);
|
|
|
}
|
|
|
searchParam.setFiter(shopFilter);
|
|
|
return shopFilter;
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "SHOP_LIST", includeParams = {"keyword", "is_encode", "contain_global", "contain_ufo"})
|
|
|
public SearchApiResult searchShopList(Map<String, String> paramMap) {
|
|
|
// 1、获取搜索店铺的关键词
|
|
|
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
|
|
|
if (StringUtils.isBlank(keyword)) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传keyword");
|
|
|
}
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 2、构建query
|
|
|
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
|
|
|
queryBuilder.operator(Operator.OR);
|
|
|
List<SearchField> shopSearchFields = SearchFieldUtils.getShopNameSearchFields();
|
|
|
for (SearchField searchField : shopSearchFields) {
|
|
|
queryBuilder.field(searchField.getEsField(), searchField.getBoost());
|
|
|
}
|
|
|
queryBuilder.minimumShouldMatch("100%");
|
|
|
// 2.1 全球购得分减半
|
|
|
FilterFunctionBuilder[] filterFunctionBuilders = new FilterFunctionBuilder[3];
|
|
|
filterFunctionBuilders[0] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.isGlobal, "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
|
|
|
filterFunctionBuilders[1] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.isUfo, "Y"), ScoreFunctionBuilders.weightFactorFunction(0.6f));
|
|
|
filterFunctionBuilders[2] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.csBrandKeyword, keyword), ScoreFunctionBuilders.weightFactorFunction(10f));
|
|
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, filterFunctionBuilders);
|
|
|
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
|
|
|
searchParam.setQuery(functionScoreQueryBuilder);
|
|
|
|
|
|
// 3、构建filter
|
|
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
|
|
filter.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));
|
|
|
filter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
|
|
|
filter.must(QueryBuilders.termQuery(ProductIndexEsField.isOutlets, 2));
|
|
|
filter.must(QueryBuilders.termQuery(ProductIndexEsField.attribute, 1));
|
|
|
filter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.isSeckill, "Y"));
|
|
|
filter.must(this.buildShopFilter(paramMap));
|
|
|
searchParam.setFiter(filter);
|
|
|
|
|
|
// 4、构建聚合条件
|
|
|
final String firstAggName = "firstAgg";
|
|
|
SearchSort aggSort = new SearchSort("_score", SortOrder.DESC);
|
|
|
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
|
|
|
// 2.1)构造父聚合:品牌或品类聚合【同时按子聚合的sort字段排序】
|
|
|
// 4.1)构造父聚合:品牌或品类聚合【同时按子聚合的sort字段排序】
|
|
|
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms(firstAggName).field(ProductIndexEsField.shopId).order(Terms.Order.aggregation("sort", aggSort.asc())).size(50);
|
|
|
// 2.2)添加子聚合:取得分最大的值
|
|
|
// 4.2)添加子聚合:取得分最大的值
|
|
|
firstAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(aggSort.getSortField()));
|
|
|
// 2.3)添加孙聚合:取打分最高的一个product
|
|
|
// 4.3)添加孙聚合:取打分最高的一个product
|
|
|
firstAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").sort(SortBuilders.scoreSort().order(aggSort.getSortOrder())).size(1));
|
|
|
list.add(firstAggregationBuilder);
|
|
|
searchParam.setAggregationBuilders(list);
|
|
|
|
|
|
// 4、执行搜索,并构造返回结果
|
|
|
// 5、执行搜索,并构造返回结果
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
...
|
...
|
@@ -151,7 +155,7 @@ public class ShopListService { |
|
|
if (!aggMaps.containsKey(firstAggName)) {
|
|
|
return null;
|
|
|
}
|
|
|
// 5、构造返回结果
|
|
|
// 6、构造返回结果
|
|
|
List<Map<String, Object>> shop_list = this.getShopList(((MultiBucketsAggregation) aggMaps.get(firstAggName)));
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put("shop_list", shop_list);
|
...
|
...
|
|