|
|
package com.yoho.search.service.service;
|
|
|
|
|
|
import com.yoho.search.core.es.IElasticsearchClient;
|
|
|
import com.yoho.search.core.es.impl.YohoIndexHelper;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.service.utils.LocalCacheKeyUtils;
|
|
|
import com.yoho.search.service.cache.LocalCacheService;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
...
|
...
|
@@ -21,153 +21,136 @@ import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.core.es.IElasticsearchClient;
|
|
|
import com.yoho.search.core.es.impl.YohoIndexHelper;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.service.cache.LocalCacheService;
|
|
|
import com.yoho.search.service.utils.LocalCacheKeyUtils;
|
|
|
|
|
|
/**
|
|
|
* Created by zhuzhu on 15-11-19.
|
|
|
* 全球购搜索接口
|
|
|
* 只需要返回productSkn
|
|
|
* Created by zhuzhu on 15-11-19. 全球购搜索接口 只需要返回productSkn
|
|
|
*/
|
|
|
@Service
|
|
|
public class TblProductService {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(TblProductService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private ESClientMgr esClientMgr;
|
|
|
@Autowired
|
|
|
private YohoIndexHelper yohoIndexHelper;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private TblProductServiceHepler tblProductServiceHepler;
|
|
|
@Autowired
|
|
|
private LocalCacheService localCacheService;
|
|
|
|
|
|
public Map<String, Object> search(Map<String, String> paramMap) throws ExecutionException {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[model=TblProductService][func=search][param={}][begin={}]", paramMap.toString(), begin);
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(QueryBuilders.matchAllQuery());
|
|
|
BoolQueryBuilder boolFilterBuilder = QueryBuilders.boolQuery();
|
|
|
boolFilterBuilder = tblProductServiceHepler.buildFilter(boolFilterBuilder, paramMap);
|
|
|
if (boolFilterBuilder.hasClauses()) {
|
|
|
searchParam.setFiter(boolFilterBuilder);
|
|
|
}
|
|
|
// 设置查询结果返回字段
|
|
|
if (StringUtils.isNotBlank(paramMap.get("resultFields"))) {
|
|
|
String resultFields = paramMap.get("resultFields");
|
|
|
searchParam.setResultFields(Arrays.asList(resultFields.split(",")));
|
|
|
}
|
|
|
|
|
|
int offset = StringUtils.isBlank(paramMap.get("offset")) ? 0 : Integer.parseInt(paramMap.get("offset"));
|
|
|
int limit = StringUtils.isBlank(paramMap.get("limit")) ? 10 : Integer.parseInt(paramMap.get("limit"));
|
|
|
searchParam.setOffset(offset);
|
|
|
searchParam.setSize(limit);
|
|
|
|
|
|
// 设置排序字段
|
|
|
// LinkedHashMap<String, String> sortFields = new LinkedHashMap<String,
|
|
|
// String>();
|
|
|
// if (StringUtils.isNotBlank(paramMap.get("order"))) {
|
|
|
// String[] sortTypes = paramMap.get("order").split(",");
|
|
|
// for (String sortType : sortTypes) {
|
|
|
// if (sortType.split(ISearchConstants.SPLIT_CHAR_COLON).length != 2) {
|
|
|
// throw new IllegalArgumentException("order参数格式不正确,请检查");
|
|
|
// }
|
|
|
// String field = sortType.split(ISearchConstants.SPLIT_CHAR_COLON)[0];
|
|
|
// String type = sortType.split(ISearchConstants.SPLIT_CHAR_COLON)[1];
|
|
|
// sortFields.put(field, type);
|
|
|
// }
|
|
|
// searchParam.setSortFields(sortFields);
|
|
|
// }
|
|
|
|
|
|
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
|
|
|
if (StringUtils.isNotBlank(paramMap.get("order"))) {
|
|
|
String[] sortTypes = paramMap.get("order").split(",");
|
|
|
for (String sortType : sortTypes) {
|
|
|
if (sortType.split(ISearchConstants.SPLIT_CHAR_COLON).length != 2) {
|
|
|
throw new IllegalArgumentException("order参数格式不正确,请检查");
|
|
|
}
|
|
|
String field = sortType.split(ISearchConstants.SPLIT_CHAR_COLON)[0];
|
|
|
String type = sortType.split(ISearchConstants.SPLIT_CHAR_COLON)[1];
|
|
|
SortOrder sortOrder = SortOrder.ASC.toString().equals(type) ? SortOrder.ASC : SortOrder.DESC;
|
|
|
sortBuilders.add(SortBuilders.fieldSort(field).order(sortOrder));
|
|
|
}
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
}
|
|
|
|
|
|
//增加aggregation
|
|
|
searchParam.setAggregationBuilders(tblProductServiceHepler.buildAggregations(paramMap));
|
|
|
|
|
|
SearchResult searchResult = localCacheService.getOrAddToLocalCache(LocalCacheKeyUtils.guavaCacheKey(ISearchConstants.INDEX_NAME_TBLPRODUCT, searchParam), new Callable<SearchResult>() {
|
|
|
@Override
|
|
|
public SearchResult call() throws Exception {
|
|
|
return searchCommonService.doSearch(ISearchConstants.INDEX_NAME_TBLPRODUCT, searchParam);
|
|
|
}
|
|
|
});
|
|
|
// 将searchResult转化为map返回
|
|
|
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
|
|
jsonMap.put("message", "tblproduct info");
|
|
|
jsonMap.put("code", 200);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
jsonMap.put("data", "");
|
|
|
} else {
|
|
|
Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
dataMap.put("total", searchResult.getTotal());
|
|
|
dataMap.put("page", searchResult.getPage());
|
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
dataMap.put("product_list", searchResult.getResultList());
|
|
|
//增加aggregation结果处理
|
|
|
if (searchResult.getAggMaps() != null) {
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
dataMap.put("filter", buildFilterResult(aggMaps));
|
|
|
}
|
|
|
jsonMap.put("data", dataMap);
|
|
|
}
|
|
|
logger.info("[model=TblProductService][func=search][param={}][cost={}ms]", paramMap.toString(), System.currentTimeMillis()
|
|
|
- begin);
|
|
|
return jsonMap;
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> buildFilterResult(Map<String, Aggregation> aggregationMap) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
String key;
|
|
|
String field;
|
|
|
for (Map.Entry<String, Aggregation> entry : aggregationMap.entrySet()) {
|
|
|
key = entry.getKey();
|
|
|
if (key.length() <= 3) break;
|
|
|
field = entry.getKey().substring(0, entry.getKey().length() -3);
|
|
|
if ("goods_price".equals(field)) {
|
|
|
map.put(field, tblProductServiceHepler.getPriceResponseMap((MultiBucketsAggregation) entry.getValue()));
|
|
|
} else if ("gender".equals(field)) {
|
|
|
map.put(field, tblProductServiceHepler.getGenderResponseMap((MultiBucketsAggregation) entry.getValue()));
|
|
|
} else {
|
|
|
map.put(field, tblProductServiceHepler.buildFilterResponseMap((MultiBucketsAggregation) entry.getValue(), field));
|
|
|
}
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
public void update(String product_skn, String json) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[model=TblProductService][func=update][param={}][begin={}]", json, begin);
|
|
|
// TODO: need be tested
|
|
|
// indexService.updateIndexDataStr(ISearchConstants.INDEX_NAME_TBLPRODUCT, product_skn, json);
|
|
|
|
|
|
final String yohoIndexName = ISearchConstants.INDEX_NAME_TBLPRODUCT;
|
|
|
IElasticsearchClient client = esClientMgr.getClient(yohoIndexName);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRelatedIndexs(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.updateIndexDataStr(realIndexName, yohoIndexName, product_skn, json);
|
|
|
}
|
|
|
|
|
|
|
|
|
logger.info("[model=TblProductService][func=update][param={}][cost={}ms]", json, System.currentTimeMillis()
|
|
|
- begin);
|
|
|
}
|
|
|
private static Logger logger = LoggerFactory.getLogger(TblProductService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private ESClientMgr esClientMgr;
|
|
|
@Autowired
|
|
|
private YohoIndexHelper yohoIndexHelper;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private TblProductServiceHepler tblProductServiceHepler;
|
|
|
@Autowired
|
|
|
private LocalCacheService localCacheService;
|
|
|
|
|
|
public Map<String, Object> search(Map<String, String> paramMap) throws ExecutionException {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[model=TblProductService][func=search][param={}][begin={}]", paramMap.toString(), begin);
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setQuery(QueryBuilders.matchAllQuery());
|
|
|
BoolQueryBuilder boolFilterBuilder = QueryBuilders.boolQuery();
|
|
|
boolFilterBuilder = tblProductServiceHepler.buildFilter(boolFilterBuilder, paramMap);
|
|
|
if (boolFilterBuilder.hasClauses()) {
|
|
|
searchParam.setFiter(boolFilterBuilder);
|
|
|
}
|
|
|
// 设置查询结果返回字段
|
|
|
if (StringUtils.isNotBlank(paramMap.get("resultFields"))) {
|
|
|
String resultFields = paramMap.get("resultFields");
|
|
|
searchParam.setResultFields(Arrays.asList(resultFields.split(",")));
|
|
|
}
|
|
|
|
|
|
int offset = StringUtils.isBlank(paramMap.get("offset")) ? 0 : Integer.parseInt(paramMap.get("offset"));
|
|
|
int limit = StringUtils.isBlank(paramMap.get("limit")) ? 10 : Integer.parseInt(paramMap.get("limit"));
|
|
|
searchParam.setOffset(offset);
|
|
|
searchParam.setSize(limit);
|
|
|
|
|
|
// 设置排序字段
|
|
|
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
|
|
|
if (StringUtils.isNotBlank(paramMap.get("order"))) {
|
|
|
String[] sortTypes = paramMap.get("order").split(",");
|
|
|
for (String sortType : sortTypes) {
|
|
|
if (sortType.split(ISearchConstants.SPLIT_CHAR_COLON).length != 2) {
|
|
|
throw new IllegalArgumentException("order参数格式不正确,请检查");
|
|
|
}
|
|
|
String field = sortType.split(ISearchConstants.SPLIT_CHAR_COLON)[0];
|
|
|
String type = sortType.split(ISearchConstants.SPLIT_CHAR_COLON)[1];
|
|
|
SortOrder sortOrder = SortOrder.ASC.toString().equals(type) ? SortOrder.ASC : SortOrder.DESC;
|
|
|
sortBuilders.add(SortBuilders.fieldSort(field).order(sortOrder));
|
|
|
}
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
}
|
|
|
|
|
|
// 增加aggregation
|
|
|
searchParam.setAggregationBuilders(tblProductServiceHepler.buildAggregations(paramMap));
|
|
|
|
|
|
SearchResult searchResult = localCacheService.getOrAddToLocalCache(LocalCacheKeyUtils.guavaCacheKey(ISearchConstants.INDEX_NAME_TBLPRODUCT, searchParam),
|
|
|
new Callable<SearchResult>() {
|
|
|
@Override
|
|
|
public SearchResult call() throws Exception {
|
|
|
return searchCommonService.doSearch(ISearchConstants.INDEX_NAME_TBLPRODUCT, searchParam);
|
|
|
}
|
|
|
});
|
|
|
// 将searchResult转化为map返回
|
|
|
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
|
|
jsonMap.put("message", "tblproduct info");
|
|
|
jsonMap.put("code", 200);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
jsonMap.put("data", "");
|
|
|
} else {
|
|
|
Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
dataMap.put("total", searchResult.getTotal());
|
|
|
dataMap.put("page", searchResult.getPage());
|
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
dataMap.put("product_list", searchResult.getResultList());
|
|
|
// 增加aggregation结果处理
|
|
|
if (searchResult.getAggMaps() != null) {
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
dataMap.put("filter", buildFilterResult(aggMaps));
|
|
|
}
|
|
|
jsonMap.put("data", dataMap);
|
|
|
}
|
|
|
logger.info("[model=TblProductService][func=search][param={}][cost={}ms]", paramMap.toString(), System.currentTimeMillis() - begin);
|
|
|
return jsonMap;
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> buildFilterResult(Map<String, Aggregation> aggregationMap) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
String key;
|
|
|
String field;
|
|
|
for (Map.Entry<String, Aggregation> entry : aggregationMap.entrySet()) {
|
|
|
key = entry.getKey();
|
|
|
if (key.length() <= 3)
|
|
|
break;
|
|
|
field = entry.getKey().substring(0, entry.getKey().length() - 3);
|
|
|
if ("goods_price".equals(field)) {
|
|
|
map.put(field, tblProductServiceHepler.getPriceResponseMap((MultiBucketsAggregation) entry.getValue()));
|
|
|
} else if ("gender".equals(field)) {
|
|
|
map.put(field, tblProductServiceHepler.getGenderResponseMap((MultiBucketsAggregation) entry.getValue()));
|
|
|
} else {
|
|
|
map.put(field, tblProductServiceHepler.buildFilterResponseMap((MultiBucketsAggregation) entry.getValue(), field));
|
|
|
}
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
public void update(String product_skn, String json) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[model=TblProductService][func=update][param={}][begin={}]", json, begin);
|
|
|
|
|
|
final String yohoIndexName = ISearchConstants.INDEX_NAME_TBLPRODUCT;
|
|
|
IElasticsearchClient client = esClientMgr.getClient(yohoIndexName);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.updateIndexDataStr(realIndexName, yohoIndexName, product_skn, json);
|
|
|
}
|
|
|
logger.info("[model=TblProductService][func=update][param={}][cost={}ms]", json, System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
} |
...
|
...
|
|