Authored by 胡古飞

fix INDEX_NAME_PRODUCT_COLOR with ColorIndexBaseService

package com.yoho.search.service.aggregations.impls;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yoho.search.core.es.agg.IAggregation;
import com.yoho.search.service.aggregations.AggInterface;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.base.BrandIndexBaseService;
import com.yoho.search.service.service.base.ColorIndexBaseService;
import com.yoho.search.service.servicenew.IShopsService;
import com.yoho.search.service.servicenew.impl.SearchAfterCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class AggregationFactoryService {
... ... @@ -21,6 +22,8 @@ public class AggregationFactoryService {
private SearchAfterCacheService searchAfterCacheService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ColorIndexBaseService colorIndexBaseService;
@Autowired
private IShopsService shopsService;
... ... @@ -41,7 +44,7 @@ public class AggregationFactoryService {
iAggregation = new PriceAggregation(searchAfterCacheService);
break;
case AggInterface.COLOR:
iAggregation = new ColorAggregation(searchCommonService, paramMap);
iAggregation = new ColorAggregation(colorIndexBaseService, paramMap);
break;
case AggInterface.STYLE:
iAggregation = new StyleAggregation(searchCommonService, paramMap);
... ... @@ -92,7 +95,7 @@ public class AggregationFactoryService {
}
public IAggregation getColorAggregation(Map<String, String> paramMap) {
return new ColorAggregation(searchCommonService, paramMap);
return new ColorAggregation(colorIndexBaseService, paramMap);
}
public IAggregation getStyleAggregation(Map<String, String> paramMap) {
... ...
package com.yoho.search.service.aggregations.impls;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.service.SearchCommonService;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
... ... @@ -10,18 +12,17 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.service.base.ColorIndexBaseService;
public class ColorAggregation extends AbstractAggregation {
private SearchCommonService searchCommonService;
private ColorIndexBaseService colorIndexBaseService;
private Map<String, String> paramMap;
ColorAggregation(SearchCommonService searchCommonService, Map<String, String> paramMap) {
this.searchCommonService = searchCommonService;
ColorAggregation(ColorIndexBaseService colorIndexBaseService, Map<String, String> paramMap) {
this.colorIndexBaseService = colorIndexBaseService;
this.paramMap = paramMap;
}
... ... @@ -56,7 +57,7 @@ public class ColorAggregation extends AbstractAggregation {
}
}
try {
return searchCommonService.doMultiGet(ISearchConstants.INDEX_NAME_PRODUCT_COLOR, sizeIdsSet, null);
return colorIndexBaseService.getColorListByIds(sizeIdsSet);
} catch (Exception e) {
e.printStackTrace();
return null;
... ...
... ... @@ -2,6 +2,7 @@ package com.yoho.search.service.service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
... ... @@ -107,6 +108,30 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
}
return results;
}
/**
* 通过id获取内容
*
* @param indexName
* @param fields
* @return
*/
public Map<String, Object> doGetCommon(final String indexName, final String id) {
if (StringUtils.isBlank(id)) {
return null;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return null;
}
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
return null;
}
return response.getSourceAsMap();
}
/**
* 通过id获取内容
... ... @@ -115,7 +140,7 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @param fields
* @return
*/
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final List<?> idList) throws Exception {
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
... ... @@ -162,9 +187,7 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
return map;
}
// 构造返回结果
if (indexName.equals(ISearchConstants.INDEX_NAME_PRODUCT_COLOR)) {
map = getColorMap(response.getSource());
} else if (indexName.equals(ISearchConstants.INDEX_NAME_SIZE)) {
if (indexName.equals(ISearchConstants.INDEX_NAME_SIZE)) {
map = getSizeMap(response.getSource());
} else if (indexName.equals(ISearchConstants.INDEX_NAME_STYLE)) {
map = getStyleMap(response.getSource());
... ... @@ -232,13 +255,6 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
list.add(getShopMap(tmpMap));
}
}
} else if (indexName.equals(ISearchConstants.INDEX_NAME_PRODUCT_COLOR)) {
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
tmpMap = item.getResponse().getSource();
list.add(getColorMap(tmpMap));
}
}
} else if (indexName.equals(ISearchConstants.INDEX_NAME_SIZE)) {
List<Map<String, Object>> sizeResults = new ArrayList<Map<String, Object>>();
for (MultiGetItemResponse item : response.getResponses()) {
... ... @@ -311,15 +327,6 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
return map;
}
private Map<String, Object> getColorMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("color_id", esMap.get("id"));
map.put("color_value", esMap.get("colorValue"));
map.put("color_name", esMap.get("colorName"));
map.put("color_code", esMap.get("colorCode"));
return map;
}
private Map<String, Object> getShopMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("shop_id", esMap.get("shopsId"));
... ...
... ... @@ -21,6 +21,7 @@ import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.service.base.BrandIndexBaseService;
import com.yoho.search.service.service.base.ColorIndexBaseService;
import com.yoho.search.service.service.helper.SearchCommonHelper;
import com.yoho.search.service.service.helper.SearchServiceHelper;
import com.yoho.search.service.service.helper.SearchSortHelper;
... ... @@ -46,6 +47,8 @@ public class SearchProductsServiceNew {
private SearchKeyWordService searchKeyWordService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ColorIndexBaseService colorIndexBaseService;
private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) {
if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) {
... ... @@ -262,7 +265,7 @@ public class SearchProductsServiceNew {
if (colorResponse != null) {
filter.put("color", colorResponse);
} else {
filter.put("color", searchCommonService.doGet(ISearchConstants.INDEX_NAME_PRODUCT_COLOR, paramMap.get("color")));
filter.put("color", colorIndexBaseService.getColorById(paramMap.get("color")));
}
// 5)获取风格层面的聚合结果
... ...
package com.yoho.search.service.service.base;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
... ... @@ -29,43 +30,27 @@ public class BrandIndexBaseService {
private SearchServiceHelper searchServiceHelper;
private static final String BRAND_INDEX_NAME = ISearchConstants.INDEX_NAME_BRAND;
private Map<String, Object> getBrandMapOld(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", esMap.get("id"));
map.put("brand_alif", esMap.get("brandAlif"));
map.put("brand_name_en", esMap.get("brandNameEn"));
map.put("brand_domain", esMap.get("brandDomain"));
map.put("is_hot", esMap.get("isHot"));
map.put("hot_keyword", esMap.get("hotKeyword"));
map.put("brand_name_cn", esMap.get("brandNameCn"));
map.put("brand_ico", esMap.get("brandIco"));
map.put("brand_name", esMap.get("brandName"));
map.put("brand_keyword", esMap.get("brandKeyword"));
map.put("status", esMap.get("status"));
return map;
}
public Map<String, Object> getBrandMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", esMap.get("id"));
map.put("brand_name", esMap.getOrDefault("brandName",""));
map.put("brand_name_en", esMap.getOrDefault("brandNameEn",""));
map.put("brand_name_cn", esMap.getOrDefault("brandNameCn",""));
map.put("brand_ico", esMap.getOrDefault("brandIco",""));
map.put("shelves_brand_time", esMap.getOrDefault("shelvesBrandTime",0));
map.put("is_hot", esMap.getOrDefault("isHot","N"));
map.put("status", esMap.getOrDefault("status",0));
map.put("brand_alif", esMap.getOrDefault("brandAlif",""));
map.put("brand_domain", esMap.getOrDefault("brandDomain",""));
map.put("hot_keyword", esMap.getOrDefault("hotKeyword",""));
map.put("brand_keyword", esMap.getOrDefault("brandKeyword",""));
map.put("yoho_brand_id", esMap.getOrDefault("yohoBrandId",0));
map.put("is_global", esMap.getOrDefault("isGlobal","N"));
map.put("brand_name", esMap.getOrDefault("brandName", ""));
map.put("brand_name_en", esMap.getOrDefault("brandNameEn", ""));
map.put("brand_name_cn", esMap.getOrDefault("brandNameCn", ""));
map.put("brand_ico", esMap.getOrDefault("brandIco", ""));
map.put("shelves_brand_time", esMap.getOrDefault("shelvesBrandTime", 0));
map.put("is_hot", esMap.getOrDefault("isHot", "N"));
map.put("status", esMap.getOrDefault("status", 0));
map.put("brand_alif", esMap.getOrDefault("brandAlif", ""));
map.put("brand_domain", esMap.getOrDefault("brandDomain", ""));
map.put("hot_keyword", esMap.getOrDefault("hotKeyword", ""));
map.put("brand_keyword", esMap.getOrDefault("brandKeyword", ""));
map.put("yoho_brand_id", esMap.getOrDefault("yohoBrandId", 0));
map.put("is_global", esMap.getOrDefault("isGlobal", "N"));
return map;
}
public List<Map<String, Object>> getBrandListByIds(List<?> brandIds) {
public List<Map<String, Object>> getBrandListByIds(Collection<?> brandIds) {
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
try {
List<Map<String, Object>> multiGetResults = searchCommonService.doMultiGetCommon(BRAND_INDEX_NAME, brandIds);
... ... @@ -78,7 +63,7 @@ public class BrandIndexBaseService {
return resultList;
}
public Map<String, Map<String, Object>> getBrandMapByIds(List<?> brandIds) {
public Map<String, Map<String, Object>> getBrandMapByIds(Collection<?> brandIds) {
List<Map<String, Object>> resultList = this.getBrandListByIds(brandIds);
Map<String, Map<String, Object>> resultMap = new HashMap<String, Map<String, Object>>();
for (Map<String, Object> result : resultList) {
... ... @@ -93,7 +78,7 @@ public class BrandIndexBaseService {
* @param globalBrandIds
* @return
*/
private List<Map<String, Object>> getGlobalBrandListByIds(List<?> brandIds) {
private List<Map<String, Object>> getGlobalBrandListByIds(Collection<?> brandIds) {
if (brandIds == null || brandIds.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
... ... @@ -116,7 +101,7 @@ public class BrandIndexBaseService {
return list;
}
public Map<String, Map<String, Object>> getGlobalBrandMapByIds(List<?> brandIds) {
public Map<String, Map<String, Object>> getGlobalBrandMapByIds(Collection<?> brandIds) {
List<Map<String, Object>> resultList = this.getGlobalBrandListByIds(brandIds);
Map<String, Map<String, Object>> resultMap = new HashMap<String, Map<String, Object>>();
for (Map<String, Object> result : resultList) {
... ...
package com.yoho.search.service.service.base;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.helper.SearchServiceHelper;
@Service
public class ColorIndexBaseService {
private static final Logger logger = LoggerFactory.getLogger(ColorIndexBaseService.class);
private Map<String, Object> getColorMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("color_id", esMap.get("id"));
map.put("color_value", esMap.get("colorValue"));
map.put("color_name", esMap.get("colorName"));
map.put("color_code", esMap.get("colorCode"));
return map;
}
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchServiceHelper searchServiceHelper;
private static final String COLOR_INDEX_NAME = ISearchConstants.INDEX_NAME_PRODUCT_COLOR;
public Map<String, Object> getColorById(final String id) {
Map<String, Object> colorEsMap = searchCommonService.doGetCommon(COLOR_INDEX_NAME, id);
if(colorEsMap==null){
return null;
}
return this.getColorMap(colorEsMap);
}
public List<Map<String, Object>> getColorListByIds(final Collection<?> colorIds) {
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
try {
List<Map<String, Object>> multiGetResults = searchCommonService.doMultiGetCommon(COLOR_INDEX_NAME, colorIds);
for (Map<String, Object> esMap : multiGetResults) {
resultList.add(this.getColorMap(esMap));
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return resultList;
}
}
... ...
... ... @@ -30,6 +30,7 @@ import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
import com.yoho.search.service.service.SearchCacheService;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.base.BrandIndexBaseService;
import com.yoho.search.service.service.base.ColorIndexBaseService;
import com.yoho.search.service.service.helper.SearchServiceHelper;
import com.yoho.search.service.servicenew.IBreakSizeService;
import com.yoho.search.service.servicenew.ISelectionsWithAdvanceService;
... ... @@ -54,6 +55,8 @@ public class SelectionWithAdvanceImpl implements ISelectionsWithAdvanceService {
private IBreakSizeService breakSizeService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ColorIndexBaseService colorIndexBaseService;
private static Logger logger = LoggerFactory.getLogger(SelectionWithAdvanceImpl.class);
private static Logger CACHE_MATCH_REQUEST = LoggerFactory.getLogger("CACHE_MATCH_REQUEST");
... ... @@ -316,7 +319,7 @@ public class SelectionWithAdvanceImpl implements ISelectionsWithAdvanceService {
if (colorResponse != null) {
filter.put("color", colorResponse);
} else {
filter.put("color", searchCommonService.doGet(ISearchConstants.INDEX_NAME_PRODUCT_COLOR, paramMap.get("color")));
filter.put("color", colorIndexBaseService.getColorById(paramMap.get("color")));
}
// 5)获取风格层面的聚合结果
IAggregation styleAggregation = aggregationFactoryService.getStyleAggregation(paramMap);
... ...
... ... @@ -9,6 +9,7 @@ import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
import com.yoho.search.service.service.SearchCacheService;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.base.BrandIndexBaseService;
import com.yoho.search.service.service.base.ColorIndexBaseService;
import com.yoho.search.service.service.helper.SearchServiceHelper;
import com.yoho.search.service.servicenew.ISelectionsWithOutAdvanceService;
import com.yoho.search.service.utils.HttpServletRequestUtils;
... ... @@ -45,6 +46,8 @@ public class SelectionWithOutAdvanceImpl implements ISelectionsWithOutAdvanceSer
private SearchAfterCacheService searchAfterCacheService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ColorIndexBaseService colorIndexBaseService;
@Override
public SearchApiResult getSelectionsForApp(Map<String, String> paramMap) {
... ... @@ -173,7 +176,7 @@ public class SelectionWithOutAdvanceImpl implements ISelectionsWithOutAdvanceSer
if (colorResponse != null) {
filter.put("color", colorResponse);
} else {
filter.put("color", searchCommonService.doMultiGet(ISearchConstants.INDEX_NAME_PRODUCT_COLOR, paramMap.get("color").split(","), null));
filter.put("color", colorIndexBaseService.getColorListByIds(Arrays.asList(paramMap.get("color").split(","))));
}
// 5)获取风格层面的聚合结果
... ... @@ -193,7 +196,7 @@ public class SelectionWithOutAdvanceImpl implements ISelectionsWithOutAdvanceSer
} else {
filter.put("brand", brandIndexBaseService.getBrandListByIds(Arrays.asList(paramMap.get("brand").split(","))));
}
// 7)获取尺码聚合结果[尺码不会提前聚合]
IAggregation sizeAggregation = aggregationFactoryService.getSizeAggregation();
Object sizeResponse = sizeAggregation.getAggregationResponseMap(aggMaps);
... ...