SuggestController.java
6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.yoho.search.consumer.restapi;
import com.yoho.search.base.utils.CharUtils;
import com.yoho.search.base.constants.ISearchConstants;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.consumer.common.IYohoIndexService;
import com.yoho.search.consumer.index.rebuild.RebuildFlagService;
import com.yoho.search.consumer.job.SuggestionJob;
import com.yoho.search.consumer.service.daoService.SuggestWordDefService;
import com.yoho.search.consumer.suggests.common.KeywordType;
import com.yoho.search.consumer.suggests.counter.KeywordCounterService;
import com.yoho.search.dal.model.SuggestWordDef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author wangnan
* @version 2019/6/4
*/
@Controller
public class SuggestController {
private static final Logger logger = LoggerFactory.getLogger(SuggestController.class);
@Autowired
private IYohoIndexService yohoIndexService;
@Autowired
private RebuildFlagService rebuildFlagService;
@Autowired
private SuggestionJob suggestionJob;
@Autowired
private SuggestWordDefService suggestWordDefService;
@Autowired
private KeywordCounterService keywordCounterService;
@RequestMapping(value = "/index/suggestion/flow")
public Map<String, Object> runSuggestionFlow() {
try {
if (rebuildFlagService.isRebuilding()) {
return getResultMap(400, "current has index rebuilding, please wait......");
}
suggestionJob.executeAll();
return getResultMap(200, "success");
} catch (Exception e) {
logger.error("[func=runSuggestionFlow][step=execption][e={}]", e.getMessage());
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 400);
rtnMap.put("msg", e.getMessage());
return rtnMap;
}
}
@RequestMapping(value = "/index/suggestionCounter")
public Map<String, Object> suggestionCounter(@RequestParam String flowName) {
try {
if (rebuildFlagService.isRebuilding()) {
return getResultMap(400, "current has index rebuilding, please wait......");
}
if ("all".equalsIgnoreCase(flowName)) {
suggestionJob.executeCounter();
return getResultMap(200, "success");
}
suggestionJob.executeCounterForSingleFlow(flowName);
return getResultMap(200, "success");
} catch (Exception e) {
logger.error(e.getMessage(), e);
return this.getErrorResult(e);
}
}
@RequestMapping(value = "/index/suggestionDiscovery")
public Map<String, Object> suggestionDiscovery(@RequestParam String flowName) {
try {
if (rebuildFlagService.isRebuilding()) {
return getResultMap(400, "current has index rebuilding, please wait......");
}
if ("all".equalsIgnoreCase(flowName)) {
suggestionJob.executeDiscovery();
} else {
suggestionJob.executeDiscoveryForSingleFlow(flowName);
}
return getResultMap(200, "success");
} catch (Exception e) {
logger.error(e.getMessage(), e);
return this.getErrorResult(e);
}
}
@RequestMapping(value = "/index/suggestWordMgr")
public Map<String, Object> suggestWordMgr(@RequestParam String keyword, @RequestParam(defaultValue = "false") boolean isDelete) {
try {
SuggestWordDef suggestWordDef = suggestWordDefService.selectByKeyword(keyword);
if (isDelete) {
if (suggestWordDef == null) {
throw new RuntimeException("The keyword is not found!");
}
if (!Integer.valueOf(1).equals(suggestWordDef.getStatus())) {
throw new RuntimeException("The keyword has been deleted already!");
}
suggestWordDefService.updateStatusByPrimaryKey(suggestWordDef.getId(), 0);
yohoIndexService.deleteIndexData(ISearchConstants.INDEX_NAME_SUGGEST, MD5Util.string2MD5(keyword.trim().toLowerCase()));
} else {
if (suggestWordDef != null) {
throw new RuntimeException("The keyword has been added!");
}
suggestWordDef = keywordCounterService.countKeyword(keyword);
suggestWordDef.setStatus(1);
suggestWordDef.setType(KeywordType.Customized.getType());
suggestWordDef.setWeight(KeywordType.Customized.getWeightValue());
suggestWordDefService.insertBatch(Arrays.asList(suggestWordDef));
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("keyword", keyword);
dataMap.put("standardKeyword", CharUtils.standardized(keyword));
dataMap.put("type", suggestWordDef.getType());
dataMap.put("weight", suggestWordDef.getWeight());
dataMap.put("count", suggestWordDef.getCount());
dataMap.put("countForApp", suggestWordDef.getCountForApp());
dataMap.put("countForBlk", suggestWordDef.getCountForBlk());
yohoIndexService.addIndexData(ISearchConstants.INDEX_NAME_SUGGEST, MD5Util.string2MD5(keyword.trim().toLowerCase()), dataMap);
}
return getResultMap(200, "success");
} catch (Exception e) {
logger.error(e.getMessage(), e);
return this.getErrorResult(e);
}
}
private Map<String, Object> getResultMap(final int code, final String message) {
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", code);
rtnMap.put("msg", message);
return rtnMap;
}
private Map<String, Object> getErrorResult(Exception e) {
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 400);
rtnMap.put("msg", e.getMessage());
return rtnMap;
}
}