Authored by 胡古飞

模糊搜索资源位直通车支持细选

@@ -18,6 +18,8 @@ public class SearchRequestParams { @@ -18,6 +18,8 @@ public class SearchRequestParams {
18 public static final String PARAM_SEARCH_FILTER_QUERY = "filterQuery";// 过滤关键词,不打分 18 public static final String PARAM_SEARCH_FILTER_QUERY = "filterQuery";// 过滤关键词,不打分
19 public static final String PARAM_SEARCH_IS_ENCODE = "is_encode";// 检索关键词 19 public static final String PARAM_SEARCH_IS_ENCODE = "is_encode";// 检索关键词
20 public static final String PARAM_SEARCH_SHOPS_KEYWORD = "keyword";// 店铺检索词 20 public static final String PARAM_SEARCH_SHOPS_KEYWORD = "keyword";// 店铺检索词
  21 + public static final String PARAM_SEARCH_CONTIAN_XIXUAN = "contain_xixuan";// 模糊搜索资源位是否包含细选
  22 +
21 23
22 //排序参数 24 //排序参数
23 public static final String PARAM_SEARCH_ORDER = "order";// 排序值 25 public static final String PARAM_SEARCH_ORDER = "order";// 排序值
@@ -42,9 +42,9 @@ public class CsSearchResourceFuzzyIndexBaseService { @@ -42,9 +42,9 @@ public class CsSearchResourceFuzzyIndexBaseService {
42 42
43 //Guava Cache 43 //Guava Cache
44 private LoadingCache<String, List<Map<String, Object>>> fuzzySearchResourceCache = CacheBuilder.newBuilder() 44 private LoadingCache<String, List<Map<String, Object>>> fuzzySearchResourceCache = CacheBuilder.newBuilder()
45 - .maximumSize(100).expireAfterWrite(3, TimeUnit.MINUTES).build(new CacheLoader<String, List<Map<String, Object>>>() { 45 + .maximumSize(100).expireAfterWrite(2, TimeUnit.MINUTES).build(new CacheLoader<String, List<Map<String, Object>>>() {
46 public List<Map<String, Object>> load(String key) { 46 public List<Map<String, Object>> load(String key) {
47 - return queryValidFuzzySearchResourceFromEs(1000); 47 + return queryValidFuzzySearchResourceFromEs(500);
48 } 48 }
49 }); 49 });
50 50
@@ -96,44 +96,45 @@ public class CsSearchResourceFuzzyIndexBaseService { @@ -96,44 +96,45 @@ public class CsSearchResourceFuzzyIndexBaseService {
96 String image = MapUtils.getString(esMap, "image", ""); 96 String image = MapUtils.getString(esMap, "image", "");
97 map.put("image", ImageUrlAssist.getAllProductPicUrl(image, "goodsimg", "center", "d2hpdGU=")); 97 map.put("image", ImageUrlAssist.getAllProductPicUrl(image, "goodsimg", "center", "d2hpdGU="));
98 map.put("resource_type", MapUtils.getIntValue(esMap, "resourceType", 0)); 98 map.put("resource_type", MapUtils.getIntValue(esMap, "resourceType", 0));
99 - Object esDetails = MapUtils.getObject(esMap,"details",new ArrayList<>());  
100 - List<JSONObject> esDetailList = JSON.parseObject(JSON.toJSONString(esDetails), new TypeReference<List<JSONObject>>(){});  
101 - List<JSONObject> detailResults = esDetailList.stream().map(details->transferDetails(details)).collect(Collectors.toList()); 99 + Object esDetails = MapUtils.getObject(esMap, "details", new ArrayList<>());
  100 + List<JSONObject> esDetailList = JSON.parseObject(JSON.toJSONString(esDetails), new TypeReference<List<JSONObject>>() {
  101 + });
  102 + List<JSONObject> detailResults = esDetailList.stream().map(details -> transferDetails(details)).collect(Collectors.toList());
102 map.put("details", detailResults); 103 map.put("details", detailResults);
103 return map; 104 return map;
104 } 105 }
105 106
106 - private JSONObject transferDetails(JSONObject detail){ 107 + private JSONObject transferDetails(JSONObject detail) {
107 JSONObject result = new JSONObject(); 108 JSONObject result = new JSONObject();
108 - result.put("id",detail.getIntValue("id"));  
109 - result.put("show_name",detail.getString("showName")); 109 + result.put("id", detail.getIntValue("id"));
  110 + result.put("show_name", detail.getString("showName"));
110 int link_type = detail.getIntValue("linkType"); 111 int link_type = detail.getIntValue("linkType");
111 String param = detail.getString("param"); 112 String param = detail.getString("param");
112 - result.put("link_type",link_type);  
113 - if(link_type==1){  
114 - result.put("keyword",param);  
115 - }else{  
116 - result.put("params",transfer(param)); 113 + result.put("link_type", link_type);
  114 + if (link_type == 1) {
  115 + result.put("keyword", param);
  116 + } else {
  117 + result.put("params", transfer(param));
117 } 118 }
118 return result; 119 return result;
119 } 120 }
120 121
121 @Data 122 @Data
122 - public static class ParamKeyValue{ 123 + public static class ParamKeyValue {
123 String key; 124 String key;
124 String value; 125 String value;
125 } 126 }
126 127
127 // "param": "brand=248&sort=1" 128 // "param": "brand=248&sort=1"
128 - private List<ParamKeyValue> transfer(String param){ 129 + private List<ParamKeyValue> transfer(String param) {
129 List<ParamKeyValue> results = new ArrayList<>(); 130 List<ParamKeyValue> results = new ArrayList<>();
130 - String [] paramParts = param.split("&");  
131 - for(String paramPart : paramParts){  
132 - String [] keyValueArr = paramPart.split("=");  
133 - if(keyValueArr.length!=2){ 131 + String[] paramParts = param.split("&");
  132 + for (String paramPart : paramParts) {
  133 + String[] keyValueArr = paramPart.split("=");
  134 + if (keyValueArr.length != 2) {
134 continue; 135 continue;
135 } 136 }
136 - ParamKeyValue keyValue = new ParamKeyValue(); 137 + ParamKeyValue keyValue = new ParamKeyValue();
137 keyValue.key = keyValueArr[0]; 138 keyValue.key = keyValueArr[0];
138 keyValue.value = keyValueArr[1]; 139 keyValue.value = keyValueArr[1];
139 results.add(keyValue); 140 results.add(keyValue);
@@ -141,17 +142,20 @@ public class CsSearchResourceFuzzyIndexBaseService { @@ -141,17 +142,20 @@ public class CsSearchResourceFuzzyIndexBaseService {
141 return results; 142 return results;
142 } 143 }
143 144
144 -  
145 - public List<Map<String, Object>> queryFuzzySearchResourcesByKeyWord(Map<String, String> paramMap) { 145 + public List<Map<String, Object>> queryFuzzySearchResourcesByKeyWord(String keyword, boolean contain_xixuan) {
146 try { 146 try {
147 - String keyword = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY, "");  
148 List<Map<String, Object>> cacheResult = fuzzySearchResourceCache.get("DEFAULT"); 147 List<Map<String, Object>> cacheResult = fuzzySearchResourceCache.get("DEFAULT");
149 return cacheResult.stream().filter(resource -> { 148 return cacheResult.stream().filter(resource -> {
150 - if (MapUtils.getString(resource, "keyword").equalsIgnoreCase(keyword)) {  
151 - return true;  
152 - } else { 149 + // 判断是否包含细选
  150 + int resource_type = MapUtils.getIntValue(resource, "resource_type", 0);
  151 + if (!contain_xixuan && resource_type == 1) {
153 return false; 152 return false;
154 } 153 }
  154 + // 判断关键字能否匹配
  155 + if (MapUtils.getString(resource, "keyword", "").equalsIgnoreCase(keyword)) {
  156 + return true;
  157 + }
  158 + return false;
155 }).collect(Collectors.toList()); 159 }).collect(Collectors.toList());
156 } catch (Exception e) { 160 } catch (Exception e) {
157 logger.error(e.getMessage(), e); 161 logger.error(e.getMessage(), e);
@@ -43,12 +43,12 @@ public class CsSearchResourceService { @@ -43,12 +43,12 @@ public class CsSearchResourceService {
43 public List<Map<String, Object>> querySortSearchResource(JSONObject productListData, Map<String, String> paramMap) { 43 public List<Map<String, Object>> querySortSearchResource(JSONObject productListData, Map<String, String> paramMap) {
44 try { 44 try {
45 //0、判断是否开启 45 //0、判断是否开启
46 - if(abUserPartitionUtils.isAUserComplete(paramMap)){  
47 - if(!searchDynamicConfigService.sortSearchResourceAUserOpen()){ 46 + if (abUserPartitionUtils.isAUserComplete(paramMap)) {
  47 + if (!searchDynamicConfigService.sortSearchResourceAUserOpen()) {
48 return new ArrayList<>(); 48 return new ArrayList<>();
49 } 49 }
50 - }else{  
51 - if(!searchDynamicConfigService.sortSearchResourceBUserOpen()){ 50 + } else {
  51 + if (!searchDynamicConfigService.sortSearchResourceBUserOpen()) {
52 return new ArrayList<>(); 52 return new ArrayList<>();
53 } 53 }
54 } 54 }
@@ -60,9 +60,9 @@ public class CsSearchResourceService { @@ -60,9 +60,9 @@ public class CsSearchResourceService {
60 60
61 //3、过滤出符合条件的数据 61 //3、过滤出符合条件的数据
62 List<Map<String, Object>> validResources = allValidResources.stream().filter(resource -> { 62 List<Map<String, Object>> validResources = allValidResources.stream().filter(resource -> {
63 - int resourceType = MapUtils.getIntValue(resource,"resourceType",0); 63 + int resourceType = MapUtils.getIntValue(resource, "resourceType", 0);
64 List<Integer> categoryIdList = SearchConvertUtils.stringToIntList((String) resource.get("categoryIds"), ","); 64 List<Integer> categoryIdList = SearchConvertUtils.stringToIntList((String) resource.get("categoryIds"), ",");
65 - return resourceType == 2 || (salesCategoryId!=null && categoryIdList.contains(salesCategoryId)); 65 + return resourceType == 2 || (salesCategoryId != null && categoryIdList.contains(salesCategoryId));
66 }).collect(Collectors.toList()); 66 }).collect(Collectors.toList());
67 67
68 //4、分页截取 68 //4、分页截取
@@ -117,7 +117,6 @@ public class CsSearchResourceService { @@ -117,7 +117,6 @@ public class CsSearchResourceService {
117 } 117 }
118 118
119 119
120 -  
121 //处理UFO首页的资源位直通车 120 //处理UFO首页的资源位直通车
122 public List<Map<String, Object>> queryUfoCsSearchResource(JSONObject productListData, int page, int viewNum) { 121 public List<Map<String, Object>> queryUfoCsSearchResource(JSONObject productListData, int page, int viewNum) {
123 try { 122 try {
@@ -145,8 +144,8 @@ public class CsSearchResourceService { @@ -145,8 +144,8 @@ public class CsSearchResourceService {
145 return new ArrayList<>(); 144 return new ArrayList<>();
146 } 145 }
147 int[] index = new int[countPerSize]; 146 int[] index = new int[countPerSize];
148 - index[0] = viewNum * 1/4;  
149 - index[1] = viewNum * 3/4; 147 + index[0] = viewNum * 1 / 4;
  148 + index[1] = viewNum * 3 / 4;
150 for (int i = 0; i < subResourceList.size(); i++) { 149 for (int i = 0; i < subResourceList.size(); i++) {
151 subResourceList.get(i).put("index", index[i]); 150 subResourceList.get(i).put("index", index[i]);
152 } 151 }
@@ -157,10 +156,9 @@ public class CsSearchResourceService { @@ -157,10 +156,9 @@ public class CsSearchResourceService {
157 156
158 // 获取模糊搜索资源位直通车 157 // 获取模糊搜索资源位直通车
159 public List<Map<String, Object>> queryFuzzySearchResourceList(Map<String, String> paramMap) { 158 public List<Map<String, Object>> queryFuzzySearchResourceList(Map<String, String> paramMap) {
160 - String keyword = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY,"");  
161 - Map<String, String> newParamMap = new HashMap<>();  
162 - newParamMap.put(SearchRequestParams.PARAM_SEARCH_QUERY,keyword);  
163 - return searchResourceFuzzyIndexBaseService.queryFuzzySearchResourcesByKeyWord(newParamMap); 159 + String keyword = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY, "");
  160 + boolean contain_xixuan = "Y".equalsIgnoreCase(MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_CONTIAN_XIXUAN, ""));
  161 + return searchResourceFuzzyIndexBaseService.queryFuzzySearchResourcesByKeyWord(keyword, contain_xixuan);
164 } 162 }
165 163
166 } 164 }