Authored by 胡古飞

update

package com.yoho.search.service.service;
import com.alibaba.fastjson.JSONArray;
import com.yoho.search.base.utils.EventReportEnum;
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.SearchResult;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.core.es.model.SearchParam;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
... ... @@ -17,8 +17,14 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
import com.alibaba.fastjson.JSONArray;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
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;
@Service
public class SearchCommonService implements ApplicationEventPublisherAware {
... ... @@ -111,11 +117,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
return map;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
String realIndexName = yohoIndexHelper.getRealIndexName(indexName, client);
if (StringUtils.isBlank(realIndexName)) {
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames==null || realIndexNames.isEmpty()) {
return null;
}
GetResponse response = client.get(realIndexName, indexName, id);
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
... ... @@ -175,11 +181,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
return result;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
String realIndexName = yohoIndexHelper.getRealIndexName(indexName, client);
if (StringUtils.isBlank(realIndexName)) {
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames==null || realIndexNames.isEmpty()) {
return null;
}
MultiGetResponse response = client.multiGet(realIndexName, indexName, idSet, fields);
MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, fields);
return makeResponse(response, indexName);
}
... ...
package com.yoho.search.service.service;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.service.common.RedisKeys;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.es.impl.YohoIndexHelper;
import com.yoho.core.redis.YHZSetOperations;
import com.yoho.search.service.vo.KeyWordWithCount;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Resource;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -15,14 +18,12 @@ import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.yoho.core.redis.YHZSetOperations;
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.service.common.RedisKeys;
import com.yoho.search.service.vo.KeyWordWithCount;
/**
* 将关键字结果存进redis中
... ... @@ -35,12 +36,12 @@ import java.util.concurrent.Executors;
public class SearchKeyWordService {
private static final Logger logger = LoggerFactory.getLogger(SearchKeyWordService.class);
private static final Logger EMPTY_RESULT = LoggerFactory.getLogger("EMPTY_RESULT");
@Resource(name = "yhNoSyncZSetOperations")
private YHZSetOperations<String, String> yhNoSyncZSetOperations;
@Autowired
private ESClientMgr esClientMgr;
... ... @@ -51,15 +52,13 @@ public class SearchKeyWordService {
public List<AnalyzeToken> getAnalyzeTokens(String text, String analyzer) {
List<AnalyzeToken> analyzeTokens = new ArrayList<AnalyzeToken>();
final String yohoIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
IElasticsearchClient client = esClientMgr.getClient(yohoIndexName);
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
if (!StringUtils.isBlank(realIndexName)) {
analyzeTokens = client.getAnalyzeResponse(yohoIndexName, text, analyzer).getTokens();
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
if (realIndexNames == null || realIndexNames.isEmpty() || realIndexNames.size() > 1) {
return analyzeTokens;
}
return analyzeTokens;
return client.getAnalyzeResponse(yohoIndexName, text, analyzer).getTokens();
}
/**
... ... @@ -91,7 +90,7 @@ public class SearchKeyWordService {
// 1、记录整词
String keyWord = queryWord;
yhNoSyncZSetOperations.incrementScore(redisKey, keyWord, 1);
// 2、记录分词结果
String redisKeyWithToken = redisKey + ".TOEKNS";
List<String> tokens = getAnalyzeTokens(keyWord);
... ... @@ -173,7 +172,7 @@ public class SearchKeyWordService {
}
return results;
}
public void handleEmptyRecords(Map<String, String> paramMap) {
StringBuilder paramStringBuilder = new StringBuilder();
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
... ... @@ -181,10 +180,15 @@ public class SearchKeyWordService {
}
String paramString = paramStringBuilder.toString().replaceFirst("&", "");
EMPTY_RESULT.info("empty records for search.json: time[{}], param [{}]", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()), paramString);
//商品池或品类列表 为空,则告警
if(paramMap.get("filter_poolId")!=null ){//|| paramMap.get("msort")!=null || paramMap.get("misort")!=null || paramMap.get("sort")!=null
///TODO
// 商品池或品类列表 为空,则告警
if (paramMap.get("filter_poolId") != null) {// ||
// paramMap.get("msort")!=null
// ||
// paramMap.get("misort")!=null
// ||
// paramMap.get("sort")!=null
// /TODO
}
}
... ...
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);
}
}
... ...