|
|
package com.yohomars.search.restapi;
|
|
|
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.error.event.SearchLogsEvent;
|
|
|
import com.yohomars.search.index.service.IYohoIndexService;
|
|
|
import com.yohomars.search.index.service.impl.RebuildFlagService;
|
|
|
import com.yohomars.search.job.IndexRebuildJob;
|
|
|
import com.yohomars.search.utils.*;
|
|
|
import com.yohomars.search.service.IndexService;
|
|
|
import com.yohomars.search.utils.ISearchConstans;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
...
|
...
|
@@ -19,18 +16,15 @@ import org.springframework.web.bind.annotation.RequestMethod; |
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 索引管理相关请求
|
|
|
*/
|
|
|
@Controller
|
|
|
public class IndexController implements ApplicationEventPublisherAware {
|
|
|
public class IndexController extends BaseController {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
|
|
|
private static final Logger SEARCH_EVENT_LOG = LoggerFactory.getLogger("SEARCH_EVENT_LOG");
|
|
|
|
|
|
@Autowired
|
|
|
private IYohoIndexService yohoIndexService;
|
...
|
...
|
@@ -38,28 +32,16 @@ public class IndexController implements ApplicationEventPublisherAware { |
|
|
private RebuildFlagService rebuildFlagService;
|
|
|
@Autowired
|
|
|
private IndexRebuildJob indexRebuildJob;
|
|
|
@Autowired
|
|
|
private IndexService indexService;
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/test")
|
|
|
@ResponseBody
|
|
|
public Map<String, Object> test() {
|
|
|
return getResultMap(200, "success");
|
|
|
return ReturnMessage(200, 0, "success");
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/testevent")
|
|
|
@ResponseBody
|
|
|
public Map<String, Object> testevent() {
|
|
|
publisher.publishEvent(new SearchLogsEvent("MqListener", EventReportEnum.INDEXCONTROLLER_INDEX_CREATE.getMoudleName(), "monitor", "test"));
|
|
|
// 记录上报的日志
|
|
|
SEARCH_EVENT_LOG.info("report to influxDb,EventName is [{}] ,MoudleName is [{}]", "MqListener_TEST", "consumer");
|
|
|
return getResultMap(200, "success");
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/index/create/{indexName}")
|
|
|
@ResponseBody
|
...
|
...
|
@@ -67,11 +49,9 @@ public class IndexController implements ApplicationEventPublisherAware { |
|
|
try {
|
|
|
yohoIndexService.createIndex(indexName, true);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.INDEXCONTROLLER_INDEX_CREATE.getEventName(), EventReportEnum.INDEXCONTROLLER_INDEX_CREATE.getFunctionName(),
|
|
|
EventReportEnum.INDEXCONTROLLER_INDEX_CREATE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return getResultMap(400, "create " + indexName + " error: " + e.getMessage());
|
|
|
return ReturnMessage(400, 1, "create " + indexName + " error: " + e.getMessage());
|
|
|
}
|
|
|
return getResultMap(200, "create " + indexName + " success");
|
|
|
return ReturnMessage(200, 0, "create " + indexName + " success");
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/index/exist/{indexName}")
|
...
|
...
|
@@ -80,13 +60,11 @@ public class IndexController implements ApplicationEventPublisherAware { |
|
|
try {
|
|
|
Boolean bool = yohoIndexService.indexExists(indexName);
|
|
|
if (bool) {
|
|
|
return getResultMap(200, indexName + "exist ");
|
|
|
return ReturnMessage(200, 0, indexName + "exist ");
|
|
|
}
|
|
|
return getResultMap(200, indexName + " not exist ");
|
|
|
return ReturnMessage(200, 0, indexName + " not exist ");
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.INDEXCONTROLLER_INDEX_EXIST.getEventName(), EventReportEnum.INDEXCONTROLLER_INDEX_EXIST.getFunctionName(),
|
|
|
EventReportEnum.INDEXCONTROLLER_INDEX_EXIST.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return getResultMap(400, indexName + "not exist " + "error: " + e.getMessage());
|
|
|
return ReturnMessage(200, 0, "not exist " + "error: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -94,17 +72,17 @@ public class IndexController implements ApplicationEventPublisherAware { |
|
|
@ResponseBody
|
|
|
public Map<String, Object> rebuildAll() {
|
|
|
if (rebuildFlagService.isRebuilding()) {
|
|
|
return getResultMap(400, "current has index rebuilding,please wait......");
|
|
|
return ReturnMessage(400, 0, "current has index rebuilding,please wait......");
|
|
|
}
|
|
|
indexRebuildJob.execute();
|
|
|
return getResultMap(200, "rebuildAll success");
|
|
|
return ReturnMessage(200, 0, "rebuildAll success");
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/index/rebuild/{indexName}")
|
|
|
@ResponseBody
|
|
|
public Map<String, Object> rebuild(@PathVariable String indexName, HttpServletRequest request) {
|
|
|
if (rebuildFlagService.isRebuilding()) {
|
|
|
return getResultMap(400, "current has index rebuilding,please wait......");
|
|
|
return ReturnMessage(400, 1, "current has index rebuilding,please wait......");
|
|
|
}
|
|
|
try {
|
|
|
boolean isExist = yohoIndexService.indexExists(indexName);
|
...
|
...
|
@@ -114,11 +92,9 @@ public class IndexController implements ApplicationEventPublisherAware { |
|
|
}
|
|
|
yohoIndexService.rebuild(indexName);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.INDEXCONTROLLER_INDEX_REBUILD.getEventName(), EventReportEnum.INDEXCONTROLLER_INDEX_REBUILD.getFunctionName(),
|
|
|
EventReportEnum.INDEXCONTROLLER_INDEX_REBUILD.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return getResultMap(400, "rebuild " + indexName + " error: " + e.getMessage());
|
|
|
return ReturnMessage(400, 1, "rebuild " + indexName + " error: " + e.getMessage());
|
|
|
}
|
|
|
return getResultMap(200, "rebuild " + indexName + " success");
|
|
|
return ReturnMessage(200, 0, "rebuild " + indexName + " success");
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -137,97 +113,16 @@ public class IndexController implements ApplicationEventPublisherAware { |
|
|
String id = paramMap.get("id");
|
|
|
String data = paramMap.get("data");
|
|
|
if (StringUtils.isBlank(indexName) || StringUtils.isBlank(action) || StringUtils.isBlank(id) || StringUtils.isBlank(data)) {
|
|
|
return ReturnMessage("400", "1", "参数为空");
|
|
|
return ReturnMessage(400, 1, "parameter is null");
|
|
|
} else if (indexName.equals(ISearchConstans.INDEX_NAME_BIZAREA) || indexName.equals(ISearchConstans.INDEX_NAME_STORE) || indexName.equals(ISearchConstans.INDEX_NAME_LINE) || indexName.equals(ISearchConstans.INDEX_NAME_TOPIC) || indexName.equals(ISearchConstans.INDEX_NAME_COMMENT)) {
|
|
|
return updateorDeleteIndex(indexName, action, paramMap);
|
|
|
return indexService.updateOrDeleteIndex(indexName, action, paramMap);
|
|
|
} else {
|
|
|
return ReturnMessage("400", "1", "index参数错误");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
return errorReturn("updateIndex", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* &action=update&id=1&data=name:测试,english_name:test
|
|
|
*
|
|
|
* @param indexName
|
|
|
* @param action
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<String, Object> updateorDeleteIndex(String indexName, String
|
|
|
action, Map<String, String> paramMap) {
|
|
|
try {
|
|
|
if (action.equals("update")) {
|
|
|
Map<String, Object> indexData = new HashMap<String, Object>();
|
|
|
String data = paramMap.get("data");
|
|
|
String[] fields = data.split(",");
|
|
|
if (fields == null) {
|
|
|
return ReturnMessage("400", "1", "data为空");
|
|
|
}
|
|
|
for (int i = 0; i < fields.length; i++) {
|
|
|
String field = fields[i];
|
|
|
String[] maps = field.split(":");
|
|
|
if (maps.length == 1) {
|
|
|
String[] array = new String[2];
|
|
|
array[0]=maps[0];
|
|
|
array[1]="";
|
|
|
indexData.put(array[0], array[1]);
|
|
|
}else{
|
|
|
indexData.put(maps[0], maps[1]);
|
|
|
}
|
|
|
}
|
|
|
yohoIndexService.updateIndexData(indexName, paramMap.get("id").toString(), indexData);
|
|
|
return ReturnMessage(400, 1, "index is not exist");
|
|
|
}
|
|
|
if (action.equals("delete")) {
|
|
|
yohoIndexService.deleteIndexData(indexName, paramMap.get("id").toString());
|
|
|
}
|
|
|
Map<String, Object> jsonMap = new LinkedHashMap<String, Object>();
|
|
|
jsonMap.put("code", 200);
|
|
|
jsonMap.put("status", 0);
|
|
|
return jsonMap;
|
|
|
} catch (Exception e) {
|
|
|
return errorReturn("updateorDeleteIndex", paramMap, e);
|
|
|
return ReturnMessage(400, 1, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将HttpServletRequest中被锁定的ParameterMap转化为普通的HashMap
|
|
|
*/
|
|
|
private Map<String, String> transParamType(HttpServletRequest request) {
|
|
|
return HttpServletRequestUtils.transParamType(request);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 异常返回
|
|
|
*/
|
|
|
private Map<String, Object> errorReturn(final String funName, final Map<String, ?> paramMap,
|
|
|
final Exception e) {
|
|
|
logger.error("[※查询]失败:[func={}][param={}][message={}][stack={}]", funName, JsonUtil.toJson(paramMap), e.getMessage(), e);
|
|
|
Map<String, Object> rtnMap = new HashMap<String, Object>();
|
|
|
rtnMap.put("code", 400);
|
|
|
rtnMap.put("status", "error");
|
|
|
rtnMap.put("error", e.getMessage());
|
|
|
return rtnMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 错误信息返回
|
|
|
*/
|
|
|
private Map<String, Object> ReturnMessage(String code, String status, String message) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("code", code);
|
|
|
map.put("status", status);
|
|
|
map.put("error", message);
|
|
|
return map;
|
|
|
}
|
|
|
} |
...
|
...
|
|