|
|
package com.yoho.search.service.index;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import com.yoho.search.base.utils.ConvertUtils;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.BaseService;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
...
|
...
|
@@ -15,128 +16,124 @@ import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.yoho.search.base.utils.ConvertUtils;
|
|
|
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.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.common.BaseService;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
public class ShopsIndexBaseService extends BaseService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ShopsIndexBaseService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
private static final String SHOP_INDEX_NAME = ISearchConstants.INDEX_NAME_SHOPS;
|
|
|
|
|
|
public Map<String, Object> getShopMap(Map<String, Object> esMap) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("shop_id", MapUtils.getIntValue(esMap, "shopsId", 0));
|
|
|
map.put("shop_name", MapUtils.getString(esMap, "shopName", ""));
|
|
|
map.put("shop_logo", MapUtils.getString(esMap, "shopLogo", ""));
|
|
|
map.put("shop_domain", MapUtils.getString(esMap, "shopDomain", ""));
|
|
|
map.put("shop_type", MapUtils.getIntValue(esMap, "shopsType", 0));
|
|
|
map.put("decorator_template_type", MapUtils.getIntValue(esMap, "decoratorTemplateType", 0));
|
|
|
map.put("status", MapUtils.getIntValue(esMap, "status", 0));
|
|
|
map.put("blk_status", MapUtils.getIntValue(esMap, "blkStatus", 0));
|
|
|
map.put("check_status", MapUtils.getIntValue(esMap, "checkStatus", 0));
|
|
|
map.put("search_show_image", MapUtils.getString(esMap, "searchShowImage", ""));
|
|
|
map.put("shop_intro", MapUtils.getString(esMap, "shopIntro", "品牌官方授权"));
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getShopsListByIds(Collection<?> shopIds) {
|
|
|
List<Map<String, Object>> shopResults = new ArrayList<Map<String, Object>>();
|
|
|
try {
|
|
|
List<Map<String, Object>> multiGetResults = searchCommonService.doMultiGetCommon(SHOP_INDEX_NAME, shopIds);
|
|
|
for (Map<String, Object> esMap : multiGetResults) {
|
|
|
shopResults.add(this.getShopMap(esMap));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
return shopResults;
|
|
|
}
|
|
|
|
|
|
public Map<String, Map<String, Object>> getShopsMapByIds(Collection<?> shopIds) {
|
|
|
List<Map<String, Object>> shopList = this.getShopsListByIds(shopIds);
|
|
|
Map<String, Map<String, Object>> results = new HashMap<String, Map<String, Object>>();
|
|
|
for (Map<String, Object> shop : shopList) {
|
|
|
results.put(shop.getOrDefault("shop_id", 0).toString(), shop);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getShopListByIdsWithSortAndStatus(Collection<?> shopIds) {
|
|
|
Map<String, Map<String, Object>> shopMap = this.getShopsMapByIds(shopIds);
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
for (Object shopId : shopIds) {
|
|
|
Map<String, Object> shop = shopMap.get(shopId.toString());
|
|
|
if (shop != null && MapUtils.getIntValue(shop, "status", 0)==1) {
|
|
|
results.add(shop);
|
|
|
}
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<Map<String, Object>> queryShopListByParam(SearchParam searchParam) {
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
SearchResult searchResult = searchCommonService.doSearch(SHOP_INDEX_NAME, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) {
|
|
|
return results;
|
|
|
}
|
|
|
for (Map<String, Object> esMap : searchResult.getResultList()) {
|
|
|
results.add(this.getShopMap(esMap));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
// 根据参数构造filter
|
|
|
private BoolQueryBuilder constructShopsFilterBuilder(Map<String, String> paramMap) {
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SHOP) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP))) {
|
|
|
List<Integer> shopsIds = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ",");
|
|
|
boolFilter.must(QueryBuilders.termsQuery("shopsId", shopsIds));
|
|
|
}
|
|
|
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_BLKSTATUS);
|
|
|
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_CHECKSTATUS);
|
|
|
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_EXAMINESTATUS);
|
|
|
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_OPERATIONSTATUS);
|
|
|
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_SHOPSTYPE);
|
|
|
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_STATUS);
|
|
|
boolFilter = generalHandler(paramMap, boolFilter);
|
|
|
return boolFilter;
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> queryShopListByParam(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 1、构建SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
BoolQueryBuilder boolFilterForShops = this.constructShopsFilterBuilder(paramMap);
|
|
|
searchParam.setFiter(boolFilterForShops);
|
|
|
searchParam.setSize(10000);
|
|
|
searchParam.setQuery(QueryBuilders.matchAllQuery());
|
|
|
|
|
|
// 2、调搜索,并将结果加入缓存
|
|
|
List<Map<String, Object>> shopsList = this.queryShopListByParam(searchParam);
|
|
|
return shopsList;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Map<String, Map<String, Object>> queryShopMapByParam(Map<String, String> paramMap) {
|
|
|
List<Map<String, Object>> shopList = this.queryShopListByParam(paramMap);
|
|
|
Map<String, Map<String, Object>> shopInfoMap = new HashMap<String, Map<String, Object>>();
|
|
|
for (Map<String, Object> shopInfo : shopList) {
|
|
|
shopInfoMap.put(shopInfo.getOrDefault("shop_id", 0).toString(), shopInfo);
|
|
|
}
|
|
|
return shopInfoMap;
|
|
|
}
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ShopsIndexBaseService.class);
|
|
|
|
|
|
private static final List<String> paramsList = Arrays.asList(SearchRequestParams.SHOPS_PARAM_BLKSTATUS,
|
|
|
SearchRequestParams.SHOPS_PARAM_CHECKSTATUS,
|
|
|
SearchRequestParams.SHOPS_PARAM_EXAMINESTATUS,
|
|
|
SearchRequestParams.SHOPS_PARAM_OPERATIONSTATUS,
|
|
|
SearchRequestParams.SHOPS_PARAM_SHOPSTYPE,
|
|
|
SearchRequestParams.SHOPS_PARAM_STATUS);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
private static final String SHOP_INDEX_NAME = ISearchConstants.INDEX_NAME_SHOPS;
|
|
|
|
|
|
public Map<String, Object> getShopMap(Map<String, Object> esMap) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("shop_id", MapUtils.getIntValue(esMap, "shopsId", 0));
|
|
|
map.put("shop_name", MapUtils.getString(esMap, "shopName", ""));
|
|
|
map.put("shop_logo", MapUtils.getString(esMap, "shopLogo", ""));
|
|
|
map.put("shop_domain", MapUtils.getString(esMap, "shopDomain", ""));
|
|
|
map.put("shop_type", MapUtils.getIntValue(esMap, "shopsType", 0));
|
|
|
map.put("decorator_template_type", MapUtils.getIntValue(esMap, "decoratorTemplateType", 0));
|
|
|
map.put("status", MapUtils.getIntValue(esMap, "status", 0));
|
|
|
map.put("blk_status", MapUtils.getIntValue(esMap, "blkStatus", 0));
|
|
|
map.put("check_status", MapUtils.getIntValue(esMap, "checkStatus", 0));
|
|
|
map.put("search_show_image", MapUtils.getString(esMap, "searchShowImage", ""));
|
|
|
map.put("shop_intro", MapUtils.getString(esMap, "shopIntro", "品牌官方授权"));
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getShopsListByIds(Collection<?> shopIds) {
|
|
|
List<Map<String, Object>> shopResults = new ArrayList<Map<String, Object>>();
|
|
|
try {
|
|
|
List<Map<String, Object>> multiGetResults = searchCommonService.doMultiGetCommon(SHOP_INDEX_NAME, shopIds);
|
|
|
for (Map<String, Object> esMap : multiGetResults) {
|
|
|
shopResults.add(this.getShopMap(esMap));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
return shopResults;
|
|
|
}
|
|
|
|
|
|
public Map<String, Map<String, Object>> getShopsMapByIds(Collection<?> shopIds) {
|
|
|
List<Map<String, Object>> shopList = this.getShopsListByIds(shopIds);
|
|
|
Map<String, Map<String, Object>> results = new HashMap<String, Map<String, Object>>();
|
|
|
for (Map<String, Object> shop : shopList) {
|
|
|
results.put(shop.getOrDefault("shop_id", 0).toString(), shop);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getShopListByIdsWithSortAndStatus(Collection<?> shopIds) {
|
|
|
Map<String, Map<String, Object>> shopMap = this.getShopsMapByIds(shopIds);
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
for (Object shopId : shopIds) {
|
|
|
Map<String, Object> shop = shopMap.get(shopId.toString());
|
|
|
if (shop != null && MapUtils.getIntValue(shop, "status", 0) == 1) {
|
|
|
results.add(shop);
|
|
|
}
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<Map<String, Object>> queryShopListByParam(SearchParam searchParam) {
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
SearchResult searchResult = searchCommonService.doSearch(SHOP_INDEX_NAME, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) {
|
|
|
return results;
|
|
|
}
|
|
|
for (Map<String, Object> esMap : searchResult.getResultList()) {
|
|
|
results.add(this.getShopMap(esMap));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
// 根据参数构造filter
|
|
|
private BoolQueryBuilder constructShopsFilterBuilder(Map<String, String> paramMap) {
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SHOP) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP))) {
|
|
|
List<Integer> shopsIds = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ",");
|
|
|
boolFilter.must(QueryBuilders.termsQuery("shopsId", shopsIds));
|
|
|
}
|
|
|
addTermQueryBatch(paramMap, boolFilter, paramsList);
|
|
|
boolFilter = generalHandler(paramMap, boolFilter);
|
|
|
return boolFilter;
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> queryShopListByParam(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 1、构建SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
BoolQueryBuilder boolFilterForShops = this.constructShopsFilterBuilder(paramMap);
|
|
|
searchParam.setFiter(boolFilterForShops);
|
|
|
searchParam.setSize(10000);
|
|
|
searchParam.setQuery(QueryBuilders.matchAllQuery());
|
|
|
|
|
|
// 2、调搜索,并将结果加入缓存
|
|
|
List<Map<String, Object>> shopsList = this.queryShopListByParam(searchParam);
|
|
|
return shopsList;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Map<String, Map<String, Object>> queryShopMapByParam(Map<String, String> paramMap) {
|
|
|
List<Map<String, Object>> shopList = this.queryShopListByParam(paramMap);
|
|
|
Map<String, Map<String, Object>> shopInfoMap = new HashMap<String, Map<String, Object>>();
|
|
|
for (Map<String, Object> shopInfo : shopList) {
|
|
|
shopInfoMap.put(shopInfo.getOrDefault("shop_id", 0).toString(), shopInfo);
|
|
|
}
|
|
|
return shopInfoMap;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|