Authored by hugufei

Merge branch 'group_shops' into recall_config

... ... @@ -85,6 +85,5 @@ public class GroupBrandAggregation extends AbstractAggregation {
dataMap.put(brandId, tempResult);
}
return dataMap;
}
}
... ...
... ... @@ -54,8 +54,6 @@ public class GroupShopAggregation extends AbstractAggregation {
public AbstractAggregationBuilder<?> getBuilder() {
return AggCommonHelper.getTopHitAggregation(Arrays.asList(simpleFieldAgg), this.topHitOrder, this.topHitCount);
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
... ... @@ -85,6 +83,5 @@ public class GroupShopAggregation extends AbstractAggregation {
dataMap.put(shopId, tempResult);
}
return dataMap;
}
}
... ...
... ... @@ -12,6 +12,7 @@ import java.util.Map;
import java.util.Map.Entry;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.service.helper.ProductListHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
... ... @@ -269,45 +270,34 @@ public class BrandServiceImpl extends AbstractCacheAbleService implements IBrand
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "GROUP_BRANDS", excludeParams = {"page", "order", "uid", "udid"})
public SearchApiResult groupBrands(Map<String, String> paramMap) {
try {
logger.info("[func=groupBrands][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、参数检验
if (!paramMap.containsKey("brand")) {
// 0、参数检验
if (!paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_BRAND)) {
return new SearchApiResult().setCode(400).setMessage("没有 brand参数 ");
}
// 2、获取聚合条件
// 1、获取topHitCount
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
// 3、是否需要个性化
// 2、获取聚合的排序规则
String topHitOrder = "heatValue:desc";
if (topHitCount > 0 && searchCommonHelper.isNeedPersonalSearch(paramMap)) {
topHitOrder = "_score:desc";
}
// 4、构建searchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, topHitCount > 0 ? true : false);
// 3、构建searchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
IAggregation groupBrandAgg = aggregationFactoryService.getGroupBrandAggregation(topHitOrder, topHitCount);
searchParam.setAggregationBuilders(Arrays.asList(groupBrandAgg.getBuilder()));
searchParam.setOffset(0);
searchParam.setSize(0);
searchParam.setOffset(topHitCount * 2);// justForCache
// 5、从缓存中获取
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject groupBrandResult = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
if (groupBrandResult != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/groupBrands.json", paramMap);
return new SearchApiResult().setData(groupBrandResult);
}
// 6、从ES中获取
groupBrandResult = aggregationService.getAggNameAndResponseWithTotal(groupBrandAgg, searchParam);
// 4、从ES中获取
JSONObject groupBrandResult = aggregationService.getAggNameAndResponseWithTotal(groupBrandAgg, searchParam);
if (groupBrandResult == null) {
return new SearchApiResult().setData(500).setMessage("exception");
}
// 7、生成结果并且加入缓存
// 5、返回生成结果
JSONObject realResult = new JSONObject();
realResult.put("total", groupBrandResult.getOrDefault("total", 0));
realResult.put("brands", groupBrandResult.getOrDefault(groupBrandAgg.aggName(), new JSONObject()));
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, realResult);
return new SearchApiResult().setData(realResult).setMessage("groupBrands new List.");
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getFunctionName(),
... ...
... ... @@ -46,230 +46,228 @@ import java.util.*;
@Service
public class ShopsServiceImpl extends BaseService implements IShopsService, ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(ShopsServiceImpl.class);
private static final Logger logger = LoggerFactory.getLogger(ShopsServiceImpl.class);
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private IShopListService shopListService;
@Autowired
private AggregationService aggregationService;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private AggregationFactoryService aggregationFactoryService;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private IShopListService shopListService;
@Autowired
private AggregationService aggregationService;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private AggregationFactoryService aggregationFactoryService;
@Autowired
private SearchParamHelper searchParamHelper;
private ApplicationEventPublisher publisher;
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "GROUP_SHOPS", excludeParams = { "page", "order" })
public SearchApiResult group_shops(Map<String, String> paramMap) {
try {
// 1、获取topHitCount
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
// 2、获取聚合的排序规则
String topHitOrder = "heatValue:desc";
if (topHitCount > 0 && searchCommonHelper.isNeedPersonalSearch(paramMap)) {
topHitOrder = "_score:desc";
}
// 3、获取聚合条件
IAggregation groupShopAgg = aggregationFactoryService.getGroupShopAggregation(topHitOrder, topHitCount);
// 4、构建searchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, topHitCount > 0 ? true : false);
searchParam.setAggregationBuilders(Arrays.asList(groupShopAgg.getBuilder()));
searchParam.setOffset(0);
searchParam.setSize(0);
// 5、从ES中获取
JSONObject aggNameAndResponse = aggregationService.getAggNameAndResponseWithTotal(groupShopAgg, searchParam);
if (aggNameAndResponse == null) {
return new SearchApiResult().setData(500).setMessage("exception");
}
// 6、返回生成结果
JSONObject realResult = new JSONObject();
realResult.put("total", aggNameAndResponse.getOrDefault("total", 0));
realResult.put("shops", aggNameAndResponse.getOrDefault(groupShopAgg.aggName(), new JSONObject()));
return new SearchApiResult().setData(realResult).setMessage("groupShops new List.");
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult("group_shops", paramMap, e);
}
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "GROUP_SHOPS", excludeParams = {"page", "order", "uid", "udid"})
public SearchApiResult group_shops(Map<String, String> paramMap) {
try {
// 1、获取topHitCount
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
// 2、获取聚合的排序规则
String topHitOrder = "heatValue:desc";
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_YOHO_BRAND", includeParams = { "keyword", "is_encode"})
public SearchApiResult searchYohoBrand(Map<String, String> paramMap) {
try {
// 1、参数校验
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
return new SearchApiResult().setCode(400).setMessage("keyword不能为空!");
}
// 2、配置keyword的查询字段以及权重设置
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.operator(Operator.OR);
queryBuilder.field("brandName.brandName_ansj", 100).field("brandName.brandName_pinyin").field("brandNameCn.brandNameCn_ansj").field("brandNameCn.brandNameCn_pinyin")
.field("brandNameEn").field("brandDomain", 50);
queryBuilder.minimumShouldMatch("100%");
// 3、构建searchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, topHitCount > 0 ? true : false);
IAggregation groupShopAgg = aggregationFactoryService.getGroupShopAggregation(topHitOrder, topHitCount);
searchParam.setAggregationBuilders(Arrays.asList(groupShopAgg.getBuilder()));
searchParam.setOffset(0);
searchParam.setSize(0);
// 3、构建SearchParam
SearchParam searchParam = new SearchParam();
searchParam.setQuery(queryBuilder);
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termQuery("status", 1));
boolFilter.mustNot(QueryBuilders.termQuery("isGlobal", "Y"));
searchParam.setFiter(boolFilter);
searchParam.setSize(5);
// 4、从ES中获取
JSONObject aggNameAndResponse = aggregationService.getAggNameAndResponseWithTotal(groupShopAgg, searchParam);
if (aggNameAndResponse == null) {
return new SearchApiResult().setData(500).setMessage("exception");
}
// 5、返回生成结果
JSONObject realResult = new JSONObject();
realResult.put("total", aggNameAndResponse.getOrDefault("total", 0));
realResult.put("shops", aggNameAndResponse.getOrDefault(groupShopAgg.aggName(), new JSONObject()));
return new SearchApiResult().setData(realResult).setMessage("groupShops new List.");
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult("group_shops", paramMap, e);
}
}
// 4、根据searchParam查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_BRAND, searchParam);
List<String> brandIds = new ArrayList<String>();
Map<String, Map<String, Object>> brandInfoMap = new HashMap<String, Map<String, Object>>();
if (searchResult != null && searchResult.getResultList() != null && !searchResult.getResultList().isEmpty()) {
List<Map<String, Object>> result = searchResult.getResultList();
for (Map<String, Object> brandInfo : result) {
String brandId = brandInfo.get("id").toString();
brandInfoMap.put(brandId, brandInfo);
brandIds.add(brandId);
}
}
// 6、判断能否查出符合条件的品牌
String brandId = this.getMaxScoreIdFromPi(brandIds, "brandId");
JSONObject dataMap = new JSONObject();
if (StringUtils.isNotBlank(brandId)) {
Map<String, Object> brandInfo = brandInfoMap.get(brandId);
dataMap.put("id", brandInfo.getOrDefault("id", 0));
dataMap.put("brand_name", brandInfo.getOrDefault("brandName", ""));
dataMap.put("brand_ico", brandInfo.getOrDefault("brandIco", ""));
dataMap.put("brand_domain", brandInfo.getOrDefault("brandDomain", ""));
}
return new SearchApiResult().setMessage("shops info").setData(dataMap);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("searchShops", paramMap, e);
}
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_YOHO_BRAND", includeParams = {"keyword", "is_encode"})
public SearchApiResult searchYohoBrand(Map<String, String> paramMap) {
try {
// 1、参数校验
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
return new SearchApiResult().setCode(400).setMessage("keyword不能为空!");
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_SHOP_NEW", includeParams = { "keyword", "is_encode"})
public SearchApiResult searchShopsNew(Map<String, String> paramMap) {
try {
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、参数校验
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
return new SearchApiResult().setCode(400).setMessage("keyword不能为空!");
}
// 2、构造返回对象
SearchApiResult searchApiResult = new SearchApiResult();
JSONObject returnMap = new JSONObject();
returnMap.put("shop", null);
returnMap.put("brand", null);
returnMap.put("tblBrand", null);
// 2、配置keyword的查询字段以及权重设置
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.operator(Operator.OR);
queryBuilder.field("brandName.brandName_ansj", 100).field("brandName.brandName_pinyin").field("brandNameCn.brandNameCn_ansj").field("brandNameCn.brandNameCn_pinyin")
.field("brandNameEn").field("brandDomain", 50);
queryBuilder.minimumShouldMatch("100%");
// 3、查询店铺和全球购品牌
SearchApiResult shopListApiResult = shopListService.searchShopList(paramMap);
if (shopListApiResult != null) {
JSONObject jsonObject = (JSONObject) shopListApiResult.getData();
JSONArray shop_list = jsonObject.getJSONArray("shop_list");
if (shop_list != null && !shop_list.isEmpty()) {
for (int i = 0; i < shop_list.size(); i++) {
JSONObject shop_info = shop_list.getJSONObject(0);
if (shop_info.getJSONObject("yoho_shop") != null) {
JSONObject yohoShop = this.getShopDataMapFromMap(shop_info.getJSONObject("yoho_shop"));
returnMap.put("shop", yohoShop);
return searchApiResult.setData(returnMap);
}
if (shop_info.getJSONObject("tbl_brand") != null) {
JSONObject tblBrand = this.getTblBrandFromMap(shop_info.getJSONObject("tbl_brand"));
returnMap.put("tblBrand", tblBrand);
return searchApiResult.setData(returnMap);
}
}
}
}
// 4、否則获取有货品牌信息
returnMap.put("brand", this.getYohoBrand(paramMap));
// 3、构建SearchParam
SearchParam searchParam = new SearchParam();
searchParam.setQuery(queryBuilder);
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termQuery("status", 1));
boolFilter.mustNot(QueryBuilders.termQuery("isGlobal", "Y"));
searchParam.setFiter(boolFilter);
searchParam.setSize(5);
// 5、返回结果
return searchApiResult.setData(returnMap);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("searchShopsNew", paramMap, e);
}
}
// 4、根据searchParam查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_BRAND, searchParam);
List<String> brandIds = new ArrayList<String>();
Map<String, Map<String, Object>> brandInfoMap = new HashMap<String, Map<String, Object>>();
if (searchResult != null && searchResult.getResultList() != null && !searchResult.getResultList().isEmpty()) {
List<Map<String, Object>> result = searchResult.getResultList();
for (Map<String, Object> brandInfo : result) {
String brandId = brandInfo.get("id").toString();
brandInfoMap.put(brandId, brandInfo);
brandIds.add(brandId);
}
}
// 6、判断能否查出符合条件的品牌
String brandId = this.getMaxScoreIdFromPi(brandIds, "brandId");
JSONObject dataMap = new JSONObject();
if (StringUtils.isNotBlank(brandId)) {
Map<String, Object> brandInfo = brandInfoMap.get(brandId);
dataMap.put("id", brandInfo.getOrDefault("id", 0));
dataMap.put("brand_name", brandInfo.getOrDefault("brandName", ""));
dataMap.put("brand_ico", brandInfo.getOrDefault("brandIco", ""));
dataMap.put("brand_domain", brandInfo.getOrDefault("brandDomain", ""));
}
return new SearchApiResult().setMessage("shops info").setData(dataMap);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("searchShops", paramMap, e);
}
}
private JSONObject getShopDataMapFromMap(Map<String, Object> shopInfo) {
JSONObject shopDataMap = new JSONObject();
shopDataMap.put("shop_id", shopInfo.getOrDefault("shop_id", 0));
shopDataMap.put("shop_name", shopInfo.getOrDefault("shop_name", ""));
shopDataMap.put("shop_logo", shopInfo.getOrDefault("shop_logo", ""));
shopDataMap.put("shop_domain", shopInfo.getOrDefault("shop_domain", ""));
shopDataMap.put("shop_type", shopInfo.getOrDefault("shop_type", 0));
shopDataMap.put("decorator_template_type", shopInfo.getOrDefault("decorator_template_type", 0));
return shopDataMap;
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_SHOP_NEW", includeParams = {"keyword", "is_encode"})
public SearchApiResult searchShopsNew(Map<String, String> paramMap) {
try {
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、参数校验
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
return new SearchApiResult().setCode(400).setMessage("keyword不能为空!");
}
// 2、构造返回对象
SearchApiResult searchApiResult = new SearchApiResult();
JSONObject returnMap = new JSONObject();
returnMap.put("shop", null);
returnMap.put("brand", null);
returnMap.put("tblBrand", null);
private JSONObject getTblBrandFromMap(Map<String, Object> tblBrandInfo) {
JSONObject tblBrand = new JSONObject();
tblBrand.put("brand_id", tblBrandInfo.getOrDefault("id", 0));
tblBrand.put("brand_name", tblBrandInfo.getOrDefault("brand_name", ""));
tblBrand.put("brand_logo", tblBrandInfo.getOrDefault("brand_ico", ""));
return tblBrand;
}
// 3、查询店铺和全球购品牌
SearchApiResult shopListApiResult = shopListService.searchShopList(paramMap);
if (shopListApiResult != null) {
JSONObject jsonObject = (JSONObject) shopListApiResult.getData();
JSONArray shop_list = jsonObject.getJSONArray("shop_list");
if (shop_list != null && !shop_list.isEmpty()) {
for (int i = 0; i < shop_list.size(); i++) {
JSONObject shop_info = shop_list.getJSONObject(0);
if (shop_info.getJSONObject("yoho_shop") != null) {
JSONObject yohoShop = this.getShopDataMapFromMap(shop_info.getJSONObject("yoho_shop"));
returnMap.put("shop", yohoShop);
return searchApiResult.setData(returnMap);
}
if (shop_info.getJSONObject("tbl_brand") != null) {
JSONObject tblBrand = this.getTblBrandFromMap(shop_info.getJSONObject("tbl_brand"));
returnMap.put("tblBrand", tblBrand);
return searchApiResult.setData(returnMap);
}
}
}
}
// 4、否則获取有货品牌信息
returnMap.put("brand", this.getYohoBrand(paramMap));
private JSONObject getYohoBrand(Map<String, String> paramMap) {
SearchApiResult searchApiResult = this.searchYohoBrand(paramMap);
if (searchApiResult == null || searchApiResult.getData() == null) {
return null;
}
JSONObject yohoBrandInfo = (JSONObject) searchApiResult.getData();
return yohoBrandInfo;
}
// 5、返回结果
return searchApiResult.setData(returnMap);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("searchShopsNew", paramMap, e);
}
}
private JSONObject getShopDataMapFromMap(Map<String, Object> shopInfo) {
JSONObject shopDataMap = new JSONObject();
shopDataMap.put("shop_id", shopInfo.getOrDefault("shop_id", 0));
shopDataMap.put("shop_name", shopInfo.getOrDefault("shop_name", ""));
shopDataMap.put("shop_logo", shopInfo.getOrDefault("shop_logo", ""));
shopDataMap.put("shop_domain", shopInfo.getOrDefault("shop_domain", ""));
shopDataMap.put("shop_type", shopInfo.getOrDefault("shop_type", 0));
shopDataMap.put("decorator_template_type", shopInfo.getOrDefault("decorator_template_type", 0));
return shopDataMap;
}
private JSONObject getTblBrandFromMap(Map<String, Object> tblBrandInfo) {
JSONObject tblBrand = new JSONObject();
tblBrand.put("brand_id", tblBrandInfo.getOrDefault("id", 0));
tblBrand.put("brand_name", tblBrandInfo.getOrDefault("brand_name", ""));
tblBrand.put("brand_logo", tblBrandInfo.getOrDefault("brand_ico", ""));
return tblBrand;
}
private JSONObject getYohoBrand(Map<String, String> paramMap) {
SearchApiResult searchApiResult = this.searchYohoBrand(paramMap);
if (searchApiResult == null || searchApiResult.getData() == null) {
return null;
}
JSONObject yohoBrandInfo = (JSONObject) searchApiResult.getData();
return yohoBrandInfo;
}
/**
* 过滤出有库存且有商品上架的店铺id,并且按店铺id排序
*
* @return
*/
private String getMaxScoreIdFromPi(List<String> ids, String esFieldName) {
// 查询ProductIndex
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termsQuery(esFieldName, ids));
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, "1"));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.attribute, "1"));
searchParam.setFiter(boolFilter);
searchParam.setOffset(0);
searchParam.setSize(1);
// 按shopId打分
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchAllQuery());
double boost = Math.pow(10d, 5d);
for (String id : ids) {
boolQueryBuilder.should(QueryBuilders.termQuery(esFieldName, id).boost((float) boost));
boost = boost / 10;
}
searchParam.setQuery(boolQueryBuilder);
// 按得分排序
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 结果只返回shopId
searchParam.setIncludeFields(Arrays.asList(esFieldName));
// 查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (searchResult == null || CollectionUtils.isEmpty(searchResult.getResultList())) {
return null;
}
return searchResult.getResultList().get(0).get(esFieldName).toString();
}
/**
* 过滤出有库存且有商品上架的店铺id,并且按店铺id排序
*
* @return
*/
private String getMaxScoreIdFromPi(List<String> ids, String esFieldName) {
// 查询ProductIndex
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termsQuery(esFieldName, ids));
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.storageNum).gte(1));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, "1"));
boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.attribute, "1"));
searchParam.setFiter(boolFilter);
searchParam.setOffset(0);
searchParam.setSize(1);
// 按shopId打分
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchAllQuery());
double boost = Math.pow(10d, 5d);
for (String id : ids) {
boolQueryBuilder.should(QueryBuilders.termQuery(esFieldName, id).boost((float) boost));
boost = boost / 10;
}
searchParam.setQuery(boolQueryBuilder);
// 按得分排序
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
searchParam.setSortBuilders(sortBuilders);
// 结果只返回shopId
searchParam.setIncludeFields(Arrays.asList(esFieldName));
// 查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (searchResult == null || CollectionUtils.isEmpty(searchResult.getResultList())) {
return null;
}
return searchResult.getResultList().get(0).get(esFieldName).toString();
}
}
... ...