Authored by hugufei

优化suggest接口

@@ -40,279 +40,281 @@ import java.util.Map; @@ -40,279 +40,281 @@ import java.util.Map;
40 @Service 40 @Service
41 public class SuggestServiceImpl implements ISuggestService, ApplicationEventPublisherAware { 41 public class SuggestServiceImpl implements ISuggestService, ApplicationEventPublisherAware {
42 42
43 - private static final Logger logger = LoggerFactory.getLogger(SuggestServiceImpl.class);  
44 - private static final String SUGGEST_PARAM_APPTYPE = "app_type";  
45 - private static final String SUGGEST_PARAM_GLOBAL = "contain_global"; 43 + private static final Logger logger = LoggerFactory.getLogger(SuggestServiceImpl.class);
  44 + private static final String SUGGEST_PARAM_APPTYPE = "app_type";
  45 + private static final String SUGGEST_PARAM_GLOBAL = "contain_global";
46 46
47 - @Autowired  
48 - private SearchCommonService searchCommonService;  
49 - @Autowired  
50 - private SuggestHepler suggestHepler; 47 + @Autowired
  48 + private SearchCommonService searchCommonService;
  49 + @Autowired
  50 + private SuggestHepler suggestHepler;
51 51
52 - private ApplicationEventPublisher publisher; 52 + private ApplicationEventPublisher publisher;
53 53
54 - @Override  
55 - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {  
56 - this.publisher = applicationEventPublisher;  
57 - } 54 + @Override
  55 + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  56 + this.publisher = applicationEventPublisher;
  57 + }
58 58
59 - @Override  
60 - @SearchCacheAble(cacheInMinute = 60, cacheName = "SUGGEST", includeParams = { "query", "app_type", "contain_global", "is_encode" }, returnClass = SuggestApiResult.class)  
61 - public SuggestApiResult suggest(Map<String, String> paramMap) {  
62 - try {  
63 - logger.info("[func=suggest][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis()); 59 + @Override
  60 + @SearchCacheAble(cacheInMinute = 60, cacheName = "SUGGEST", includeParams = {"query", "app_type", "contain_global", "is_encode"}, returnClass = SuggestApiResult.class)
  61 + public SuggestApiResult suggest(Map<String, String> paramMap) {
  62 + try {
  63 + logger.info("[func=suggest][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
64 64
65 - // 1)关键参数验证  
66 - String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY);  
67 - if (StringUtils.isBlank(keyword)) {  
68 - return new SuggestApiResult().setCode(400).setMessage("关键字[query]参数为空!");  
69 - }  
70 - keyword = keyword.toLowerCase();  
71 - if (keyword.length() > 30) {  
72 - return new SuggestApiResult().setCode(400).setMessage("关键字[query]参数非法!");  
73 - }  
74 - paramMap.put(SearchRequestParams.PARAM_SEARCH_QUERY, keyword);  
75 - 65 + // 1)关键参数验证
  66 + String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY);
  67 + if (StringUtils.isBlank(keyword)) {
  68 + return new SuggestApiResult().setCode(400).setMessage("关键字[query]参数为空!");
  69 + }
  70 + keyword = keyword.toLowerCase();
  71 + if (keyword.length() > 30) {
  72 + return new SuggestApiResult().setCode(400).setMessage("关键字[query]参数非法!");
  73 + }
  74 + paramMap.put(SearchRequestParams.PARAM_SEARCH_QUERY, keyword);
76 75
77 - // 2)构建查询参数  
78 - SearchParam searchParam = this.buildSuggestSearchParam(paramMap);  
79 76
80 - // 3)进行ES检索  
81 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);  
82 - if (searchResult == null) {  
83 - return new SuggestApiResult().setCode(500).setMessage("SUGGEST EXCEPTION");  
84 - } 77 + // 2)构建查询参数
  78 + SearchParam searchParam = this.buildSuggestSearchParam(paramMap);
85 79
86 - // 4) 当建议词为空时,使用suggest 纠错  
87 - if (CollectionUtils.isEmpty(searchResult.getResultList())) {  
88 - SearchResult newSearchResult = this.suggestByCorrectSpelling(paramMap);  
89 - if (newSearchResult != null) {  
90 - searchResult = newSearchResult;  
91 - }  
92 - } 80 + // 3)进行ES检索
  81 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);
  82 + if (searchResult == null) {
  83 + return new SuggestApiResult().setCode(500).setMessage("SUGGEST EXCEPTION");
  84 + }
93 85
94 - // 5) 返回执行结果  
95 - JSONObject suggest = new JSONObject();  
96 - List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();  
97 - String countEsField = this.getCountField(paramMap);  
98 - for (Map<String, Object> map : searchResult.getResultList()) {  
99 - Map<String, Object> item = new HashMap<String, Object>();  
100 - item.put("item", map.get("keyword"));  
101 - item.put("frequency", map.get(countEsField));  
102 - item.put("type", map.get("type"));  
103 - itemList.add(item);  
104 - }  
105 - suggest.put("items", itemList);  
106 - return new SuggestApiResult().setCode(200).setMessage("suggest List.").setSuggest(suggest);  
107 - } catch (Exception e) {  
108 - logger.error(e.getMessage(), e);  
109 - publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_SUGGEST.getEventName(), EventReportEnum.SEARCHCONTROLLER_SUGGEST.getFunctionName(),  
110 - EventReportEnum.SEARCHCONTROLLER_SUGGEST.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));  
111 - return new SuggestApiResult().setCode(500).setMessage(e.getMessage()).setSuggest(null);  
112 - }  
113 - } 86 + // 4) 当建议词为空时,使用suggest 纠错
  87 + if (CollectionUtils.isEmpty(searchResult.getResultList())) {
  88 + SearchResult newSearchResult = this.suggestByCorrectSpelling(paramMap);
  89 + if (newSearchResult != null) {
  90 + searchResult = newSearchResult;
  91 + }
  92 + }
114 93
115 - /**  
116 - * 通过纠错的方式,查询建议词列表  
117 - *  
118 - * @param paramMap  
119 - * @return  
120 - */  
121 - private SearchResult suggestByCorrectSpelling(Map<String, String> paramMap) {  
122 - String keyword = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY).toLowerCase();  
123 - String newKeyword = (String) suggestHepler.correctSpellingKeyword(paramMap).getData();  
124 - if (StringUtils.isEmpty(newKeyword)) {  
125 - return null;  
126 - }  
127 - try {  
128 - logger.info("Switch the suggest keyword from [{}] to [{}].", keyword, newKeyword);  
129 - Map<String, String> newParamMap = new HashMap<>(paramMap.size());  
130 - newParamMap.putAll(paramMap);  
131 - newParamMap.put(SearchRequestParams.PARAM_SEARCH_QUERY, newKeyword); 94 + // 5) 返回执行结果
  95 + JSONObject suggest = new JSONObject();
  96 + List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
  97 + String countEsField = this.getCountField(paramMap);
  98 + for (Map<String, Object> map : searchResult.getResultList()) {
  99 + Map<String, Object> item = new HashMap<String, Object>();
  100 + item.put("item", map.get("keyword"));
  101 + item.put("frequency", map.get(countEsField));
  102 + item.put("type", map.get("type"));
  103 + itemList.add(item);
  104 + }
  105 + suggest.put("items", itemList);
  106 + return new SuggestApiResult().setCode(200).setMessage("suggest List.").setSuggest(suggest);
  107 + } catch (Exception e) {
  108 + logger.error(e.getMessage(), e);
  109 + publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_SUGGEST.getEventName(), EventReportEnum.SEARCHCONTROLLER_SUGGEST.getFunctionName(),
  110 + EventReportEnum.SEARCHCONTROLLER_SUGGEST.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
  111 + return new SuggestApiResult().setCode(500).setMessage(e.getMessage()).setSuggest(null);
  112 + }
  113 + }
132 114
133 - SearchParam newSearchParam = this.buildSuggestSearchParam(newParamMap);  
134 - return searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, newSearchParam);  
135 - } catch (Exception e) {  
136 - logger.error("Get new suggest result by keyword [" + newKeyword + "] failed!", e);  
137 - return null;  
138 - }  
139 - } 115 + /**
  116 + * 通过纠错的方式,查询建议词列表
  117 + *
  118 + * @param paramMap
  119 + * @return
  120 + */
  121 + private SearchResult suggestByCorrectSpelling(Map<String, String> paramMap) {
  122 + String keyword = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY).toLowerCase();
  123 + String newKeyword = (String) suggestHepler.correctSpellingKeyword(paramMap).getData();
  124 + if (StringUtils.isEmpty(newKeyword)) {
  125 + return null;
  126 + }
  127 + try {
  128 + logger.info("Switch the suggest keyword from [{}] to [{}].", keyword, newKeyword);
  129 + Map<String, String> newParamMap = new HashMap<>(paramMap.size());
  130 + newParamMap.putAll(paramMap);
  131 + newParamMap.put(SearchRequestParams.PARAM_SEARCH_QUERY, newKeyword);
140 132
141 - /**  
142 - * 构建查询参数  
143 - *  
144 - * @param paramMap  
145 - * @return  
146 - */  
147 - private SearchParam buildSuggestSearchParam(Map<String, String> paramMap) {  
148 - String keyword = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY).toLowerCase();  
149 - SearchParam searchParam = new SearchParam();  
150 - QueryBuilder query = QueryBuilders.boolQuery().should(QueryBuilders.prefixQuery("keyword", keyword)).should(QueryBuilders.prefixQuery("keyword.keyword_pinyin", keyword))  
151 - .should(QueryBuilders.prefixQuery("keyword.keyword_jianpin", keyword));  
152 - searchParam.setQuery(query);  
153 - searchParam.setOffset(0);  
154 - searchParam.setSize(10); 133 + SearchParam newSearchParam = this.buildSuggestSearchParam(newParamMap);
  134 + return searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, newSearchParam);
  135 + } catch (Exception e) {
  136 + logger.error("Get new suggest result by keyword [" + newKeyword + "] failed!", e);
  137 + return null;
  138 + }
  139 + }
155 140
156 - // count数量要>=2  
157 - final String countEsField = getCountField(paramMap);  
158 - BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();  
159 - boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));  
160 - boolFilter.must(QueryBuilders.rangeQuery(countEsField).gte(2));  
161 - searchParam.setFiter(boolFilter); 141 + /**
  142 + * 构建查询参数
  143 + *
  144 + * @param paramMap
  145 + * @return
  146 + */
  147 + private SearchParam buildSuggestSearchParam(Map<String, String> paramMap) {
  148 + String keyword = paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY).toLowerCase();
  149 + SearchParam searchParam = new SearchParam();
  150 + QueryBuilder query = QueryBuilders.boolQuery().should(QueryBuilders.prefixQuery("keyword", keyword)).should(QueryBuilders.prefixQuery("keyword.keyword_pinyin", keyword))
  151 + .should(QueryBuilders.prefixQuery("keyword.keyword_jianpin", keyword));
  152 + searchParam.setQuery(query);
  153 + searchParam.setOffset(0);
  154 + searchParam.setSize(10);
162 155
163 - // 3)设置排序字段  
164 - List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();  
165 - sortBuilders.add(SortBuilders.fieldSort("weight").order(SortOrder.DESC));  
166 - sortBuilders.add(SortBuilders.fieldSort(countEsField).order(SortOrder.DESC));  
167 - searchParam.setSortBuilders(sortBuilders);  
168 - return searchParam;  
169 - } 156 + // count数量要>=2
  157 + final String countEsField = getCountField(paramMap);
  158 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  159 + boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));
  160 + boolFilter.must(QueryBuilders.rangeQuery(countEsField).gte(2));
  161 + searchParam.setFiter(boolFilter);
170 162
171 - /**  
172 - * 根据搜索条件判断使用到的suggest索引的count字段名。  
173 - *  
174 - * @param paramMap  
175 - * 搜索条件  
176 - * @return count字段名  
177 - */  
178 - @Override  
179 - public String getCountField(Map<String, String> paramMap) {  
180 - // suggest 支持PC、APP、BLK走不同的count字段 字段同SuggestIndexBO里保持一直  
181 - if (paramMap.containsKey(SUGGEST_PARAM_APPTYPE) && "1".equals(paramMap.get(SUGGEST_PARAM_APPTYPE))) {  
182 - return "countForBlk";  
183 - } else if (paramMap.containsKey(SUGGEST_PARAM_GLOBAL) && "Y".equals(paramMap.get(SUGGEST_PARAM_GLOBAL))) {  
184 - return "countForApp";  
185 - } else {  
186 - return "count";  
187 - }  
188 - } 163 + // 3)设置排序字段
  164 + List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
  165 + sortBuilders.add(SortBuilders.fieldSort("weight").order(SortOrder.DESC));
  166 + sortBuilders.add(SortBuilders.fieldSort(countEsField).order(SortOrder.DESC));
  167 + searchParam.setSortBuilders(sortBuilders);
  168 + return searchParam;
  169 + }
189 170
190 - /**  
191 - * 到suggest索引进行拼写纠错处理。  
192 - *  
193 - * @param keyword  
194 - * 用户输入的关键词  
195 - * @return 纠错后的关键词。  
196 - */  
197 - @Override  
198 - public String getSpellingCorrectKeyword(String keyword) {  
199 - try {  
200 - Map<String, String> paramMap = new HashMap<String, String>();  
201 - paramMap.put("query", keyword);  
202 - return (String) suggestHepler.correctSpellingKeyword(paramMap).getData();  
203 - } catch (Exception e) {  
204 - logger.error("Get spelling correct keyword by [" + keyword + "] failed!", e);  
205 - return null;  
206 - }  
207 - } 171 + /**
  172 + * 根据搜索条件判断使用到的suggest索引的count字段名。
  173 + *
  174 + * @param paramMap 搜索条件
  175 + * @return count字段名
  176 + */
  177 + @Override
  178 + public String getCountField(Map<String, String> paramMap) {
  179 + // suggest 支持PC、APP、BLK走不同的count字段 字段同SuggestIndexBO里保持一直
  180 +// if (paramMap.containsKey(SUGGEST_PARAM_APPTYPE) && "1".equals(paramMap.get(SUGGEST_PARAM_APPTYPE))) {
  181 +// return "countForBlk";
  182 +// } else if (paramMap.containsKey(SUGGEST_PARAM_GLOBAL) && "Y".equals(paramMap.get(SUGGEST_PARAM_GLOBAL))) {
  183 +// return "countForApp";
  184 +// } else {
  185 +// return "count";
  186 +// }
  187 + if (paramMap.containsKey(SUGGEST_PARAM_APPTYPE) && "1".equals(paramMap.get(SUGGEST_PARAM_APPTYPE))) {
  188 + return "countForBlk";
  189 + } else {
  190 + return "countForApp";
  191 + }
  192 + }
208 193
209 - /**  
210 - * 根据关键词查询suggest索引  
211 - *  
212 - * @param paramMap  
213 - * 查询参数  
214 - * @return 满足查询要求的建议词  
215 - */  
216 - @Override  
217 - public SearchApiResult suggestList(Map<String, String> paramMap) {  
218 - try {  
219 - long begin = System.currentTimeMillis();  
220 - logger.info("[func=suggestList][param={}][begin={}]", paramMap, begin); 194 + /**
  195 + * 到suggest索引进行拼写纠错处理。
  196 + *
  197 + * @param keyword 用户输入的关键词
  198 + * @return 纠错后的关键词。
  199 + */
  200 + @Override
  201 + public String getSpellingCorrectKeyword(String keyword) {
  202 + try {
  203 + Map<String, String> paramMap = new HashMap<String, String>();
  204 + paramMap.put("query", keyword);
  205 + return (String) suggestHepler.correctSpellingKeyword(paramMap).getData();
  206 + } catch (Exception e) {
  207 + logger.error("Get spelling correct keyword by [" + keyword + "] failed!", e);
  208 + return null;
  209 + }
  210 + }
221 211
222 - String queryWord = paramMap.get("query");  
223 - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
224 - if (page < 1) {  
225 - throw new IllegalArgumentException("分页参数不合法");  
226 - } 212 + /**
  213 + * 根据关键词查询suggest索引
  214 + *
  215 + * @param paramMap 查询参数
  216 + * @return 满足查询要求的建议词
  217 + */
  218 + @Override
  219 + public SearchApiResult suggestList(Map<String, String> paramMap) {
  220 + try {
  221 + long begin = System.currentTimeMillis();
  222 + logger.info("[func=suggestList][param={}][begin={}]", paramMap, begin);
227 223
228 - // 1) 构建ES请求  
229 - SearchParam searchParam = new SearchParam();  
230 - QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();  
231 - if (StringUtils.isNotEmpty(queryWord)) {  
232 - // 是否精确匹配  
233 - String accurateQuery = paramMap.get("accurate");  
234 - if ("Y".equalsIgnoreCase(accurateQuery)) {  
235 - queryBuilder = QueryBuilders.matchQuery("standardKeyword", CharUtils.standardized(queryWord));  
236 - } else {  
237 - String keyword = queryWord.trim().toLowerCase();  
238 - queryBuilder = QueryBuilders.boolQuery().should(QueryBuilders.prefixQuery("keyword", keyword))  
239 - .should(QueryBuilders.prefixQuery("keyword.keyword_pinyin", keyword)).should(QueryBuilders.prefixQuery("keyword.keyword_jianpin", keyword));  
240 - }  
241 - } 224 + String queryWord = paramMap.get("query");
  225 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  226 + if (page < 1) {
  227 + throw new IllegalArgumentException("分页参数不合法");
  228 + }
242 229
243 - searchParam.setQuery(queryBuilder);  
244 - searchParam.setOffset((page - 1) * 10);  
245 - searchParam.setSize(10); 230 + // 1) 构建ES请求
  231 + SearchParam searchParam = new SearchParam();
  232 + QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
  233 + if (StringUtils.isNotEmpty(queryWord)) {
  234 + // 是否精确匹配
  235 + String accurateQuery = paramMap.get("accurate");
  236 + if ("Y".equalsIgnoreCase(accurateQuery)) {
  237 + queryBuilder = QueryBuilders.matchQuery("standardKeyword", CharUtils.standardized(queryWord));
  238 + } else {
  239 + String keyword = queryWord.trim().toLowerCase();
  240 + queryBuilder = QueryBuilders.boolQuery().should(QueryBuilders.prefixQuery("keyword", keyword))
  241 + .should(QueryBuilders.prefixQuery("keyword.keyword_pinyin", keyword)).should(QueryBuilders.prefixQuery("keyword.keyword_jianpin", keyword));
  242 + }
  243 + }
246 244
247 - // 2) 调用ES查询  
248 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);  
249 - if (searchResult == null) {  
250 - return null;  
251 - } 245 + searchParam.setQuery(queryBuilder);
  246 + searchParam.setOffset((page - 1) * 10);
  247 + searchParam.setSize(10);
252 248
253 - // 3) 返回结果  
254 - Map<String, Object> dataMap = new HashMap<>();  
255 - dataMap.put("total", searchResult.getTotal());  
256 - dataMap.put("page", searchResult.getPage());  
257 - dataMap.put("page_size", searchParam.getSize());  
258 - dataMap.put("page_total", searchResult.getTotalPage());  
259 - dataMap.put("suggest_list", searchResult.getResultList());  
260 - logger.info("[func=suggestList][cost={}]", System.currentTimeMillis() - begin);  
261 - return new SearchApiResult().setData(dataMap);  
262 - } catch (Exception e) {  
263 - logger.error(e.getMessage(), e);  
264 - return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);  
265 - }  
266 - } 249 + // 2) 调用ES查询
  250 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);
  251 + if (searchResult == null) {
  252 + return null;
  253 + }
267 254
268 - @Override  
269 - public SearchApiResult suggestListByCount(Map<String, String> paramMap) {  
270 - try {  
271 - long begin = System.currentTimeMillis();  
272 - logger.info("[func=suggestListByCount][param={}][begin={}]", paramMap, begin);  
273 - String count = paramMap.getOrDefault("count", "20");  
274 - int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));  
275 - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
276 - if (page < 1) {  
277 - throw new IllegalArgumentException("分页参数不合法");  
278 - }  
279 - // 1) 构建ES请求  
280 - SearchParam searchParam = new SearchParam();  
281 - // 1.1)过滤count  
282 - final String countEsField = getCountField(paramMap);  
283 - BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();  
284 - boolFilter.must(QueryBuilders.rangeQuery(countEsField).gte(Integer.valueOf(count)));  
285 - searchParam.setFiter(boolFilter);  
286 - // 1.2)根据count排序  
287 - List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();  
288 - sortBuilders.add(SortBuilders.fieldSort(countEsField).order(SortOrder.DESC));  
289 - searchParam.setSortBuilders(sortBuilders);  
290 - searchParam.setOffset((page - 1) * pageSize);  
291 - searchParam.setSize(pageSize);  
292 - // 2) 调用ES查询  
293 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);  
294 - if (searchResult == null) {  
295 - return null;  
296 - }  
297 - // 3) 返回结果  
298 - List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();  
299 - for (Map<String, Object> map : searchResult.getResultList()) {  
300 - Map<String, Object> item = new HashMap<String, Object>();  
301 - item.put("keyword", map.get("keyword"));  
302 - item.put("count", map.get(countEsField));  
303 - itemList.add(item);  
304 - }  
305 - Map<String, Object> dataMap = new HashMap<>();  
306 - dataMap.put("total", searchResult.getTotal());  
307 - dataMap.put("page", searchResult.getPage());  
308 - dataMap.put("page_size", searchParam.getSize());  
309 - dataMap.put("page_total", searchResult.getTotalPage());  
310 - dataMap.put("suggest_list", itemList);  
311 - logger.info("[func=suggestListByCount][cost={}]", System.currentTimeMillis() - begin);  
312 - return new SearchApiResult().setData(dataMap);  
313 - } catch (Exception e) {  
314 - logger.error(e.getMessage(), e);  
315 - return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);  
316 - }  
317 - } 255 + // 3) 返回结果
  256 + Map<String, Object> dataMap = new HashMap<>();
  257 + dataMap.put("total", searchResult.getTotal());
  258 + dataMap.put("page", searchResult.getPage());
  259 + dataMap.put("page_size", searchParam.getSize());
  260 + dataMap.put("page_total", searchResult.getTotalPage());
  261 + dataMap.put("suggest_list", searchResult.getResultList());
  262 + logger.info("[func=suggestList][cost={}]", System.currentTimeMillis() - begin);
  263 + return new SearchApiResult().setData(dataMap);
  264 + } catch (Exception e) {
  265 + logger.error(e.getMessage(), e);
  266 + return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);
  267 + }
  268 + }
  269 +
  270 + @Override
  271 + public SearchApiResult suggestListByCount(Map<String, String> paramMap) {
  272 + try {
  273 + long begin = System.currentTimeMillis();
  274 + logger.info("[func=suggestListByCount][param={}][begin={}]", paramMap, begin);
  275 + String count = paramMap.getOrDefault("count", "20");
  276 + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  277 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  278 + if (page < 1) {
  279 + throw new IllegalArgumentException("分页参数不合法");
  280 + }
  281 + // 1) 构建ES请求
  282 + SearchParam searchParam = new SearchParam();
  283 + // 1.1)过滤count
  284 + final String countEsField = getCountField(paramMap);
  285 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  286 + boolFilter.must(QueryBuilders.rangeQuery(countEsField).gte(Integer.valueOf(count)));
  287 + searchParam.setFiter(boolFilter);
  288 + // 1.2)根据count排序
  289 + List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
  290 + sortBuilders.add(SortBuilders.fieldSort(countEsField).order(SortOrder.DESC));
  291 + searchParam.setSortBuilders(sortBuilders);
  292 + searchParam.setOffset((page - 1) * pageSize);
  293 + searchParam.setSize(pageSize);
  294 + // 2) 调用ES查询
  295 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);
  296 + if (searchResult == null) {
  297 + return null;
  298 + }
  299 + // 3) 返回结果
  300 + List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
  301 + for (Map<String, Object> map : searchResult.getResultList()) {
  302 + Map<String, Object> item = new HashMap<String, Object>();
  303 + item.put("keyword", map.get("keyword"));
  304 + item.put("count", map.get(countEsField));
  305 + itemList.add(item);
  306 + }
  307 + Map<String, Object> dataMap = new HashMap<>();
  308 + dataMap.put("total", searchResult.getTotal());
  309 + dataMap.put("page", searchResult.getPage());
  310 + dataMap.put("page_size", searchParam.getSize());
  311 + dataMap.put("page_total", searchResult.getTotalPage());
  312 + dataMap.put("suggest_list", itemList);
  313 + logger.info("[func=suggestListByCount][cost={}]", System.currentTimeMillis() - begin);
  314 + return new SearchApiResult().setData(dataMap);
  315 + } catch (Exception e) {
  316 + logger.error(e.getMessage(), e);
  317 + return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);
  318 + }
  319 + }
318 } 320 }