...
|
...
|
@@ -6,8 +6,6 @@ import java.util.HashMap; |
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
...
|
...
|
@@ -28,11 +26,8 @@ import com.alibaba.fastjson.JSONArray; |
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.HttpServletRequestUtils;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.cache.SearchCacheFactory;
|
|
|
import com.yoho.search.common.cache.SearchCacheMatchLogger;
|
|
|
import com.yoho.search.common.cache.model.SearchCache;
|
|
|
import com.yoho.search.common.cache.aop.SearchCacheAble;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.common.utils.SearchKeyWordUtils;
|
|
|
import com.yoho.search.core.es.agg.IAggregation;
|
...
|
...
|
@@ -41,7 +36,6 @@ import com.yoho.search.core.es.model.SearchResult; |
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
|
|
|
import com.yoho.search.service.base.SearchCacheService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
...
|
...
|
@@ -69,13 +63,8 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl |
|
|
@Autowired
|
|
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
@Autowired
|
|
|
private SearchCacheService searchCacheService;
|
|
|
@Autowired
|
|
|
private SearchCacheFactory searchCacheFactory;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
|
|
|
private SearchCache brandAndShopSearchCache;
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -83,52 +72,33 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl |
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
brandAndShopSearchCache = searchCacheFactory.getBrandAndShopSearchCache();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "GROUP_SHOPS", excludeParams = { "page", "order" })
|
|
|
public SearchApiResult group_shops(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
String key=HttpServletRequestUtils.genParamString(paramMap);
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
String cacheKey="YOHOSEARCH:"+productIndexName+":"+key;
|
|
|
JSONObject groupShopsResult = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, cacheKey);
|
|
|
if (groupShopsResult != null) {
|
|
|
return new SearchApiResult().setData(groupShopsResult).setMessage("groupShops new List.");
|
|
|
}
|
|
|
// 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";
|
|
|
}
|
|
|
// 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(topHitCount * 2);// justForCache
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
// final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
// JSONObject groupShopsResult = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, productIndexName, searchParam);
|
|
|
// if (groupShopsResult != null) {
|
|
|
// SearchCacheMatchLogger.doSearchCacheMatchLog("/group_shops.json", paramMap);
|
|
|
// return new SearchApiResult().setData(groupShopsResult).setMessage("groupShops new List.");
|
|
|
// }
|
|
|
// 6、从ES中获取
|
|
|
// 5、从ES中获取
|
|
|
JSONObject aggNameAndResponse = aggregationService.getAggNameAndResponseWithTotal(groupShopAgg, searchParam);
|
|
|
if (aggNameAndResponse == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
// 6、返回生成结果
|
|
|
JSONObject realResult = new JSONObject();
|
|
|
realResult.put("total", aggNameAndResponse.getOrDefault("total", 0));
|
|
|
realResult.put("shops", aggNameAndResponse.getOrDefault(groupShopAgg.aggName(), new JSONObject()));
|
|
|
searchCacheService.addJSONObjectToCache(brandAndShopSearchCache, cacheKey, realResult);
|
|
|
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(),
|
...
|
...
|
@@ -138,51 +108,38 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_YOHO_BRAND", includeParams = { "keyword", "is_encode"})
|
|
|
public SearchApiResult searchYohoBrand(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不能为空!");
|
|
|
}
|
|
|
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
|
|
|
|
|
|
// 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%");
|
|
|
searchParam.setQuery(queryBuilder);
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("shops info");
|
|
|
|
|
|
// 4、先从缓存里取数据
|
|
|
JSONObject dataMap = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, indexName, searchParam);
|
|
|
if (dataMap != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/shops.json", paramMap);
|
|
|
return searchApiResult.setData(dataMap);
|
|
|
}
|
|
|
|
|
|
// 5、根据searchParam查询ES
|
|
|
// 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) {
|
|
|
for (Map<String, Object> brandInfo : result) {
|
|
|
String brandId = brandInfo.get("id").toString();
|
|
|
brandInfoMap.put(brandId, brandInfo);
|
|
|
brandIds.add(brandId);
|
...
|
...
|
@@ -190,7 +147,7 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl |
|
|
}
|
|
|
// 6、判断能否查出符合条件的品牌
|
|
|
String brandId = this.getMaxScoreIdFromPi(brandIds, "brandId");
|
|
|
dataMap = new JSONObject();
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
if (StringUtils.isNotBlank(brandId)) {
|
|
|
Map<String, Object> brandInfo = brandInfoMap.get(brandId);
|
|
|
dataMap.put("id", brandInfo.getOrDefault("id", 0));
|
...
|
...
|
@@ -198,14 +155,14 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl |
|
|
dataMap.put("brand_ico", brandInfo.getOrDefault("brandIco", ""));
|
|
|
dataMap.put("brand_domain", brandInfo.getOrDefault("brandDomain", ""));
|
|
|
}
|
|
|
searchCacheService.addJSONObjectToCache(brandAndShopSearchCache, indexName, searchParam, dataMap);
|
|
|
return searchApiResult.setData(dataMap);
|
|
|
return new SearchApiResult().setMessage("shops info").setData(dataMap);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("searchShops", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "SHOP_NEW", includeParams = { "keyword", "is_encode"})
|
|
|
public SearchApiResult searchShopsNew(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
...
|
...
|
@@ -318,4 +275,5 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl |
|
|
}
|
|
|
return searchResult.getResultList().get(0).get(esFieldName).toString();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|