Authored by Gino Zhang

支持每天定时爬虫处理

1 package com.yoho.search.service.restapi; 1 package com.yoho.search.service.restapi;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.List;  
5 -import java.util.Map;  
6 - 3 +import com.yoho.search.service.cache.LocalCacheService;
  4 +import com.yoho.search.service.personalized.PersonalVectorFeatureSearch;
  5 +import com.yoho.search.service.service.SearchKeyWordService;
  6 +import com.yoho.search.service.servicenew.IProductListService;
  7 +import com.yoho.search.service.vo.KeyWordWithCount;
  8 +import com.yoho.search.service.vo.SearchApiResult;
7 import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken; 9 import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
8 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.stereotype.Controller; 11 import org.springframework.stereotype.Controller;
@@ -12,133 +14,142 @@ import org.springframework.web.bind.annotation.RequestMethod; @@ -12,133 +14,142 @@ import org.springframework.web.bind.annotation.RequestMethod;
12 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.bind.annotation.RequestParam;
13 import org.springframework.web.bind.annotation.ResponseBody; 15 import org.springframework.web.bind.annotation.ResponseBody;
14 16
15 -import com.yoho.search.service.cache.LocalCacheService;  
16 -import com.yoho.search.service.personalized.PersonalVectorFeatureSearch;  
17 -import com.yoho.search.service.service.SearchKeyWordService;  
18 -import com.yoho.search.service.servicenew.IProductListService;  
19 -import com.yoho.search.service.vo.KeyWordWithCount;  
20 -import com.yoho.search.service.vo.SearchApiResult; 17 +import java.util.ArrayList;
  18 +import java.util.List;
  19 +import java.util.Map;
21 20
22 @Controller 21 @Controller
23 @RequestMapping(value = "/tools") 22 @RequestMapping(value = "/tools")
24 public class ToolsController { 23 public class ToolsController {
25 24
26 - @Autowired  
27 - private SearchKeyWordService searchKeyWordService;  
28 - @Autowired  
29 - private LocalCacheService localCacheService;  
30 - @Autowired  
31 - private PersonalVectorFeatureSearch personalVectorFeatureSearch;  
32 - @Autowired  
33 - private IProductListService productListService; 25 + @Autowired
  26 + private SearchKeyWordService searchKeyWordService;
  27 + @Autowired
  28 + private LocalCacheService localCacheService;
  29 + @Autowired
  30 + private PersonalVectorFeatureSearch personalVectorFeatureSearch;
  31 + @Autowired
  32 + private IProductListService productListService;
  33 +
  34 + /**
  35 + * 获取热搜词结果
  36 + *
  37 + * @param request
  38 + * @return
  39 + */
  40 + @RequestMapping(method = RequestMethod.GET, value = "/hotSearchWords")
  41 + @ResponseBody
  42 + public SearchApiResult hotSearchWords(@RequestParam(defaultValue = "1000") int limit,
  43 + @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
  44 + @RequestParam(defaultValue = "true") boolean returnTodayRecords) {
  45 + SearchApiResult searchApiResult = new SearchApiResult();
  46 + Map<String, Object> results = searchKeyWordService.getHotkeyWords(limit, onlyShowKeyWord ? false : returnTodayRecords);
  47 + if (!onlyShowKeyWord) {
  48 + return searchApiResult.setData(results);
  49 + }
34 50
35 - /**  
36 - * 获取热搜词结果  
37 - *  
38 - * @param request  
39 - * @return  
40 - */  
41 - @RequestMapping(method = RequestMethod.GET, value = "/hotSearchWords")  
42 - @ResponseBody  
43 - public SearchApiResult hotSearchWords(@RequestParam(defaultValue = "1000") int limit,  
44 - @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,  
45 - @RequestParam(defaultValue = "true") boolean returnTodayRecords) {  
46 - SearchApiResult searchApiResult = new SearchApiResult();  
47 - Map<String, Object> results = searchKeyWordService.getHotkeyWords(limit, onlyShowKeyWord ? false : returnTodayRecords);  
48 - if (!onlyShowKeyWord) {  
49 - return searchApiResult.setData(results);  
50 - } 51 + List<String> keywords = new ArrayList<>();
  52 + for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("-1")) {
  53 + keywords.add(keyWordWithCount.getKeyWord());
  54 + }
  55 + return searchApiResult.setData(keywords);
  56 + }
51 57
52 - List<String> keywords = new ArrayList<>();  
53 - for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>)results.get("-1")) {  
54 - keywords.add(keyWordWithCount.getKeyWord());  
55 - }  
56 - return searchApiResult.setData(keywords);  
57 - } 58 + /**
  59 + * 获取空结果搜索词
  60 + *
  61 + * @return
  62 + */
  63 + @RequestMapping(method = RequestMethod.GET, value = "/emptyResultKeywords")
  64 + @ResponseBody
  65 + public SearchApiResult emptyResultKeywords(@RequestParam(defaultValue = "1000") int limit,
  66 + @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
  67 + @RequestParam(defaultValue = "true") boolean returnTodayRecords) {
  68 + SearchApiResult searchApiResult = new SearchApiResult();
  69 + Map<String, Object> results = searchKeyWordService.getEmptyKeyWords(limit, onlyShowKeyWord ? false : returnTodayRecords);
  70 + if (!onlyShowKeyWord) {
  71 + return searchApiResult.setData(results);
  72 + }
58 73
59 - /**  
60 - * 获取空结果搜索词  
61 - * @return  
62 - */  
63 - @RequestMapping(method = RequestMethod.GET, value = "/emptyResultKeywords")  
64 - @ResponseBody  
65 - public SearchApiResult emptyResultKeywords(@RequestParam(defaultValue = "1000") int limit,  
66 - @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,  
67 - @RequestParam(defaultValue = "true") boolean returnTodayRecords) {  
68 - SearchApiResult searchApiResult = new SearchApiResult();  
69 - Map<String, Object> results = searchKeyWordService.getEmptyKeyWords(limit, onlyShowKeyWord ? false : returnTodayRecords);  
70 - if (!onlyShowKeyWord) {  
71 - return searchApiResult.setData(results);  
72 - } 74 + List<String> keywords = new ArrayList<>();
  75 + for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("-1")) {
  76 + keywords.add(keyWordWithCount.getKeyWord());
  77 + }
  78 + return searchApiResult.setData(keywords);
  79 + }
73 80
74 - List<String> keywords = new ArrayList<>();  
75 - for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>)results.get("-1")) {  
76 - keywords.add(keyWordWithCount.getKeyWord());  
77 - }  
78 - return searchApiResult.setData(keywords);  
79 - } 81 + /**
  82 + * 获取一页搜索词
  83 + *
  84 + * @param request
  85 + * @return
  86 + */
  87 + @RequestMapping(method = RequestMethod.GET, value = "/lessKeyWords")
  88 + @ResponseBody
  89 + public SearchApiResult lessKeyWords(@RequestParam(defaultValue = "1000") int limit,
  90 + @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,
  91 + @RequestParam(defaultValue = "true") boolean returnTodayRecords) {
  92 + SearchApiResult searchApiResult = new SearchApiResult();
  93 + Map<String, Object> results = searchKeyWordService.getLessKeyWords(limit, onlyShowKeyWord ? false : returnTodayRecords);
  94 + if (!onlyShowKeyWord) {
  95 + return searchApiResult.setData(results);
  96 + }
80 97
81 - /**  
82 - * 获取一页搜索词  
83 - *  
84 - * @param request  
85 - * @return  
86 - */  
87 - @RequestMapping(method = RequestMethod.GET, value = "/lessKeyWords")  
88 - @ResponseBody  
89 - public SearchApiResult lessKeyWords(@RequestParam(defaultValue = "1000") int limit,  
90 - @RequestParam(defaultValue = "false") boolean onlyShowKeyWord,  
91 - @RequestParam(defaultValue = "true") boolean returnTodayRecords) {  
92 - SearchApiResult searchApiResult = new SearchApiResult();  
93 - Map<String, Object> results = searchKeyWordService.getLessKeyWords(limit, onlyShowKeyWord ? false : returnTodayRecords);  
94 - if (!onlyShowKeyWord) {  
95 - return searchApiResult.setData(results);  
96 - } 98 + List<String> keywords = new ArrayList<>();
  99 + for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>) results.get("-1")) {
  100 + keywords.add(keyWordWithCount.getKeyWord());
  101 + }
  102 + return searchApiResult.setData(keywords);
  103 + }
97 104
98 - List<String> keywords = new ArrayList<>();  
99 - for (KeyWordWithCount keyWordWithCount : (List<KeyWordWithCount>)results.get("-1")) {  
100 - keywords.add(keyWordWithCount.getKeyWord());  
101 - }  
102 - return searchApiResult.setData(keywords);  
103 - } 105 + /**
  106 + * 删除redis上的key
  107 + * @param redisKey
  108 + * @return
  109 + */
  110 + @RequestMapping(method = RequestMethod.GET, value = "/deleteRedis")
  111 + @ResponseBody
  112 + public SearchApiResult deleteRedisKey(@RequestParam String redisKey) {
  113 + SearchApiResult searchApiResult = new SearchApiResult();
  114 + return searchApiResult.setData(searchKeyWordService.deleteRedisKey(redisKey));
  115 + }
104 116
105 - /**  
106 - * 获取分词结果  
107 - *  
108 - * @param request  
109 - * @return  
110 - */  
111 - @RequestMapping(method = RequestMethod.GET, value = "/analyzeTokens")  
112 - @ResponseBody  
113 - public SearchApiResult onePageKeywords(@RequestParam(defaultValue = "") String text, @RequestParam(defaultValue = "ik_complex") String analyzer) {  
114 - List<AnalyzeToken> analyzeTokens = searchKeyWordService.getAnalyzeTokens(text, analyzer);  
115 - SearchApiResult searchApiResult = new SearchApiResult();  
116 - return searchApiResult.setData(analyzeTokens);  
117 - } 117 + /**
  118 + * 获取分词结果
  119 + *
  120 + * @param request
  121 + * @return
  122 + */
  123 + @RequestMapping(method = RequestMethod.GET, value = "/analyzeTokens")
  124 + @ResponseBody
  125 + public SearchApiResult onePageKeywords(@RequestParam(defaultValue = "") String text, @RequestParam(defaultValue = "ik_complex") String analyzer) {
  126 + List<AnalyzeToken> analyzeTokens = searchKeyWordService.getAnalyzeTokens(text, analyzer);
  127 + SearchApiResult searchApiResult = new SearchApiResult();
  128 + return searchApiResult.setData(analyzeTokens);
  129 + }
118 130
119 - /**  
120 - * 清除本地缓存  
121 - *  
122 - * @param request  
123 - * 请求  
124 - * @return  
125 - */  
126 - @RequestMapping(method = RequestMethod.GET, value = "/clearLocalCache")  
127 - @ResponseBody  
128 - public SearchApiResult clearLocalCache() {  
129 - localCacheService.clearLocalCache();  
130 - return new SearchApiResult().setMessage("清除本地缓存成功");  
131 - } 131 + /**
  132 + * 清除本地缓存
  133 + *
  134 + * @param request 请求
  135 + * @return
  136 + */
  137 + @RequestMapping(method = RequestMethod.GET, value = "/clearLocalCache")
  138 + @ResponseBody
  139 + public SearchApiResult clearLocalCache() {
  140 + localCacheService.clearLocalCache();
  141 + return new SearchApiResult().setMessage("清除本地缓存成功");
  142 + }
132 143
133 - @RequestMapping(method = RequestMethod.GET, value = "/calVectorFeature")  
134 - @ResponseBody  
135 - public SearchApiResult calVectorFeature(@RequestParam String uid, @RequestParam String skns, @RequestParam String version) throws Exception {  
136 - SearchApiResult productListResult = productListService.productListBySknList(skns);  
137 - if (productListResult.getCode() != 200) {  
138 - return productListResult;  
139 - } 144 + @RequestMapping(method = RequestMethod.GET, value = "/calVectorFeature")
  145 + @ResponseBody
  146 + public SearchApiResult calVectorFeature(@RequestParam String uid, @RequestParam String skns, @RequestParam String version) throws Exception {
  147 + SearchApiResult productListResult = productListService.productListBySknList(skns);
  148 + if (productListResult.getCode() != 200) {
  149 + return productListResult;
  150 + }
140 151
141 - Map<String, String> productVectorFeatureMap = (Map<String, String>) productListResult.getData();  
142 - return new SearchApiResult().setData(personalVectorFeatureSearch.calVectorFeature(uid, version, productVectorFeatureMap));  
143 - } 152 + Map<String, String> productVectorFeatureMap = (Map<String, String>) productListResult.getData();
  153 + return new SearchApiResult().setData(personalVectorFeatureSearch.calVectorFeature(uid, version, productVectorFeatureMap));
  154 + }
144 } 155 }
@@ -212,4 +212,14 @@ public class SearchKeyWordService { @@ -212,4 +212,14 @@ public class SearchKeyWordService {
212 } 212 }
213 } 213 }
214 214
  215 + public String deleteRedisKey(String redisKey){
  216 + if(yhNoSyncRedisTemplate.hasKey(redisKey)){
  217 + yhNoSyncRedisTemplate.delete(redisKey);
  218 + return "The key has been deleted succede!";
  219 + }
  220 + else{
  221 + return "The key doesn't exist.";
  222 + }
  223 + }
  224 +
215 } 225 }