Authored by 胡古飞

利用SearchKeyWordUtils重构keyword判断

... ... @@ -15,8 +15,10 @@ import com.yoho.search.service.servicenew.ISearchWithCacheService;
import com.yoho.search.service.servicenew.IShopsService;
import com.yoho.search.service.utils.HttpServletRequestUtils;
import com.yoho.search.service.utils.SearchApiResultUtils;
import com.yoho.search.service.utils.SearchKeyWordUtils;
import com.yoho.search.service.utils.SearchRequestParams;
import com.yoho.search.service.vo.SearchApiResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.*;
... ... @@ -214,34 +216,19 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、参数校验
String keyword = paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
keyword = keyword.toLowerCase();
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap);
if (StringUtils.isBlank(keyword)) {
return new SearchApiResult().setCode(400).setMessage("keyword不能为空!");
}
if (keyword.contains("%")) {
keyword.replace("%", "percent");// 特殊处理
}
if (keyword.equals("耐克")) {
keyword = "nike";
}
// 编码转换
String is_encode = paramMap.get("is_encode");
if (StringUtils.isNotBlank(is_encode) && is_encode.equals("1")) {
try {
keyword = URLDecoder.decode(keyword, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn(e.getMessage(), e);
}
}
// 2、构造返回对象
SearchApiResult searchApiResult = new SearchApiResult();
JSONObject returnMap = new JSONObject();
returnMap.put("shop", null);
returnMap.put("brand", null);
returnMap.put("tblBrand", null);
//tbl判断
//if (paramMap.containsKey("contain_global") && "Y".equals(paramMap.get("contain_global"))) {
//3、如果包含了全球购,则直接返回
searchApiResult = searchTbl(paramMap, keyword);
if (searchApiResult.getData() != null) {
Map<String, Object> shopInfoMap = (Map<String, Object>) searchApiResult.getData();
... ... @@ -253,16 +240,15 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
returnMap.put("tblBrand", shopDataMap);
return searchApiResult.setData(returnMap);
}
//}
// 2、配置keyword的查询字段以及权重设置
// 4、配置keyword的查询字段以及权重设置
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.operator(MatchQueryBuilder.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).field("shopName", 200);
queryBuilder.minimumShouldMatch("100%");
// 3、构建SearchParam
// 5、构建SearchParam
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilterForShops = this.constructShopsFilterBuilder(paramMap);
searchParam.setFiter(boolFilterForShops);
... ... @@ -270,14 +256,14 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
searchParam.setSize(5);
final String indexName = ISearchConstants.INDEX_NAME_SHOPS;
// 4、先从缓存里取数据
// 6、先从缓存里取数据
returnMap = searchCacheService.getJSONObjectFromCache(indexName, searchParam);
if (returnMap != null) {
CACHE_MATCH_REQUEST.info("match cache , url is :/shops.json?" + HttpServletRequestUtils.genParamString(paramMap));
return searchApiResult.setData(returnMap);
}
// 5、根据searchParam查询ES
// 7、根据searchParam查询ES
returnMap = new JSONObject();
returnMap.put("shop", null);
returnMap.put("brand", null);
... ... @@ -294,7 +280,7 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
shopIds.add(shopId);
}
}
// 6、判断能否查出符合条件的店铺
// 8、判断能否查出符合条件的店铺
String shopId = this.getMaxScoreIdFromPi(shopIds, "shopId");
if (!StringUtils.isBlank(shopId)) {
JSONObject shopDataMap = new JSONObject();
... ...
package com.yoho.search.service.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SearchKeyWordUtils {
private static final Logger logger = LoggerFactory.getLogger(SearchKeyWordUtils.class);
public static String getParamKeyword(Map<String, String> paramMap) {
String keyword = paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
return null;
}
// 编码转换
String is_encode = paramMap.get("is_encode");
if (StringUtils.isNotBlank(is_encode) && is_encode.equals("1")) {
try {
keyword = URLDecoder.decode(keyword, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn(e.getMessage(), e);
}
}
if (keyword.contains("%")) {
keyword.replace("%", "percent");// 特殊处理
}
if (keyword.equals("耐克")) {
keyword = "nike";
}
keyword = keyword.toLowerCase();
return keyword;
}
}
... ...
... ... @@ -21,8 +21,8 @@ redis.search.proxy.port =6379
redis.search.proxy.auth =
#search
search.es.cluster.name=yohosearch_test
search.es.servers=192.168.102.209:9300 192.168.102.216:9300
search.es.cluster.name=yohosearch
search.es.servers=192.168.102.12:9300 192.168.102.13:9300
search.index.number_of_replicas=1
search.index.refresh_interval=1
search.index.translog.flush_threshold_ops=5000
... ...