refactor
This commit is contained in:
parent
f63a70126d
commit
e58c2412af
|
@ -1,7 +1,6 @@
|
|||
package index_management
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -27,17 +26,20 @@ func (handler APIHandler) HandleAddDocumentAction(w http.ResponseWriter, req *ht
|
|||
resResult := newResponseBody()
|
||||
err := handler.DecodeJSON(req, &reqBody)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10001"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err.Error()
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
indexName := ps.ByName("index")
|
||||
id := util.GetUUID()
|
||||
id := ps.ByName("id")
|
||||
if strings.Trim(id, "/") == "" {
|
||||
util.GetUUID()
|
||||
}
|
||||
_, err = client.Index(indexName, id, reqBody)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10002"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -52,8 +54,8 @@ func (handler APIHandler) HandleUpdateDocumentAction(w http.ResponseWriter, req
|
|||
resResult := newResponseBody()
|
||||
err := handler.DecodeJSON(req, &reqBody)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10001"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -61,8 +63,8 @@ func (handler APIHandler) HandleUpdateDocumentAction(w http.ResponseWriter, req
|
|||
id := ps.ByName("id")
|
||||
resp, err := client.Get(indexName, id)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10004"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err.Error()
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -72,8 +74,8 @@ func (handler APIHandler) HandleUpdateDocumentAction(w http.ResponseWriter, req
|
|||
}
|
||||
_, err = client.Index(indexName, id, source)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10005"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err.Error()
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -88,8 +90,8 @@ func (handler APIHandler) HandleDeleteDocumentAction(w http.ResponseWriter, req
|
|||
id := ps.ByName("id")
|
||||
_, err := client.Delete(indexName, id)
|
||||
if err != nil {
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["errno"] = "E10006"
|
||||
resResult["error"] = err.Error()
|
||||
resResult["status"] = false
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -102,8 +104,8 @@ func (handler APIHandler) HandleSearchDocumentAction(w http.ResponseWriter, req
|
|||
resResult := newResponseBody()
|
||||
err := handler.DecodeJSON(req, &reqBody)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10001"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err.Error()
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -139,65 +141,18 @@ func (handler APIHandler) HandleSearchDocumentAction(w http.ResponseWriter, req
|
|||
var reqBytes = []byte(query)
|
||||
resp, err := client.SearchWithRawQueryDSL(indexName, reqBytes)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10007"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["status"] = false
|
||||
resResult["error"] = err.Error()
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
result := formatESSearchResult(resp)
|
||||
//result := formatESSearchResult(resp)
|
||||
|
||||
_, _, idxs, err := client.GetMapping(false, indexName)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E10008"
|
||||
resResult["errmsg"] = err.Error()
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
result["mappings"] = idxs
|
||||
resResult["payload"] = result
|
||||
resResult["payload"] = resp
|
||||
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
}
|
||||
|
||||
func (handler APIHandler) HandleGetIndicesAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
indices, err := getESIndices(handler.Config.Elasticsearch)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
handler.WriteJSON(w, map[string]interface{}{
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
"payload": indices,
|
||||
}, http.StatusOK)
|
||||
}
|
||||
|
||||
func getESIndices(esName string) ([]string, error) {
|
||||
client := elastic.GetClient(esName)
|
||||
esConfig := elastic.GetConfig(esName)
|
||||
url := fmt.Sprintf("%s/_cat/indices?format=json", esConfig.Endpoint)
|
||||
result, err := client.Request("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var catIndices = []struct {
|
||||
Index string `json:"index"`
|
||||
}{}
|
||||
err = json.Unmarshal(result.Body, &catIndices)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var indices = []string{}
|
||||
for _, index := range catIndices {
|
||||
if strings.HasPrefix(index.Index, ".") {
|
||||
continue
|
||||
}
|
||||
indices = append(indices, index.Index)
|
||||
}
|
||||
|
||||
return indices, nil
|
||||
}
|
||||
|
||||
func formatESSearchResult(esResp *elastic.SearchResponse) map[string]interface{} {
|
||||
total := esResp.Hits.Total
|
||||
if len(esResp.Hits.Hits) == 0 {
|
||||
|
|
|
@ -28,76 +28,78 @@ func (handler APIHandler) GetDictListAction(w http.ResponseWriter, req *http.Req
|
|||
from, _ = strconv.Atoi(fromStr)
|
||||
size, _ = strconv.Atoi(sizeStr)
|
||||
tags = strings.Split(tag, ",")
|
||||
resp = newResponseBody()
|
||||
)
|
||||
if len(tags) > 3 {
|
||||
tags = tags[0:3]
|
||||
}
|
||||
rel, err := model2.GetDictList(from, size, name, tags)
|
||||
if err != nil {
|
||||
handler.Error(w, err)
|
||||
}
|
||||
resp := map[string]interface{}{
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
"data": rel,
|
||||
resp["error"] = err
|
||||
resp["status"] = false
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
return
|
||||
}
|
||||
resp["payload"] = rel
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
}
|
||||
|
||||
func (handler APIHandler) CreateDictItemAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
//id := ps.ByName("id")
|
||||
id := ps.ByName("id")
|
||||
if strings.Trim(id, "/") == "" {
|
||||
id = util.GetUUID()
|
||||
}
|
||||
createdAt := time.Now()
|
||||
|
||||
resp := newResponseBody()
|
||||
dict := model2.Dict{
|
||||
ID: util.GetUUID(),
|
||||
ID: id,
|
||||
CreatedAt: createdAt,
|
||||
UpdatedAt: createdAt,
|
||||
}
|
||||
err := handler.DecodeJSON(req, &dict)
|
||||
if err != nil {
|
||||
handler.WriteJSON(w, map[string]interface{}{
|
||||
"payload": nil,
|
||||
"errno": "E100001",
|
||||
"errmsg": err.Error(),
|
||||
}, http.StatusOK)
|
||||
resp["status"] = false
|
||||
resp["error"] = err
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
err = orm.Save(dict)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
resp["status"] = false
|
||||
resp["error"] = err
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
return
|
||||
}
|
||||
handler.WriteJSON(w, map[string]interface{}{
|
||||
"payload": dict,
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
}, http.StatusOK)
|
||||
resp["payload"] = dict
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
}
|
||||
|
||||
func (handler APIHandler) DeleteDictItemAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
id := ps.ByName("id")
|
||||
dict := model2.Dict{}
|
||||
dict.ID = id
|
||||
resp := newResponseBody()
|
||||
|
||||
err := orm.Delete(dict)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
resp["status"] = false
|
||||
resp["error"] = err
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
return
|
||||
}
|
||||
handler.WriteJSON(w, map[string]interface{}{
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
}, http.StatusOK)
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
}
|
||||
|
||||
func (handler APIHandler) UpdateDictItemAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
dict := model2.Dict{}
|
||||
err := handler.DecodeJSON(req, &dict)
|
||||
resp := newResponseBody()
|
||||
if err != nil {
|
||||
handler.WriteJSON(w, map[string]interface{}{
|
||||
"payload": nil,
|
||||
"errno": "E100002",
|
||||
"errmsg": err.Error(),
|
||||
}, http.StatusOK)
|
||||
resp["status"] = false
|
||||
resp["error"] = err
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
return
|
||||
|
||||
}
|
||||
|
@ -105,13 +107,13 @@ func (handler APIHandler) UpdateDictItemAction(w http.ResponseWriter, req *http.
|
|||
|
||||
err = orm.Update(dict)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
resp["status"] = false
|
||||
resp["error"] = err
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
return
|
||||
}
|
||||
handler.WriteJSON(w, map[string]interface{}{
|
||||
"payload": dict,
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
}, http.StatusOK)
|
||||
resp["payload"] = dict
|
||||
handler.WriteJSON(w, resp, http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -11,11 +11,7 @@ import (
|
|||
func (handler APIHandler) HandleGetMappingsAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
client := elastic.GetClient(handler.Config.Elasticsearch)
|
||||
indexName := ps.ByName("index")
|
||||
resBody := map[string]interface{}{
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
"payload": nil,
|
||||
}
|
||||
resBody := newResponseBody()
|
||||
var copyAll = false
|
||||
if indexName == "*" {
|
||||
indexName = ""
|
||||
|
@ -23,8 +19,8 @@ func (handler APIHandler) HandleGetMappingsAction(w http.ResponseWriter, req *ht
|
|||
}
|
||||
_, _, idxs, err := client.GetMapping(copyAll, indexName)
|
||||
if err != nil {
|
||||
resBody["errno"] = "E30001"
|
||||
resBody["errmsg"] = err.Error()
|
||||
resBody["error"] = err
|
||||
resBody["status"] = false
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
@ -40,3 +36,17 @@ func (handler APIHandler) HandleGetMappingsAction(w http.ResponseWriter, req *ht
|
|||
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
}
|
||||
|
||||
func (handler APIHandler) HandleGetIndicesAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
client := elastic.GetClient(handler.Config.Elasticsearch)
|
||||
catIndices, err := client.GetIndices()
|
||||
resBody := newResponseBody()
|
||||
if err != nil {
|
||||
resBody["status"] = false
|
||||
resBody["error"] = err
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
return
|
||||
}
|
||||
resBody["payload"] = catIndices
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
httprouter "infini.sh/framework/core/api/router"
|
||||
|
@ -14,37 +15,35 @@ import (
|
|||
|
||||
func (handler APIHandler) ReindexAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
reindexItem := &model.InfiniReindex{}
|
||||
resResult := map[string]interface{}{
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
"payload": nil,
|
||||
id := ps.ByName("id")
|
||||
if strings.Trim(id, "/") != "" {
|
||||
reindexItem.ID = id
|
||||
}
|
||||
resResult := newResponseBody()
|
||||
|
||||
err := handler.DecodeJSON(req, reindexItem)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E20001"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["error"] = err
|
||||
resResult["status"] = false
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
//fmt.Println(reindexItem)
|
||||
|
||||
taskID, err := reindex(handler.Config.Elasticsearch, reindexItem)
|
||||
ID, err := reindex(handler.Config.Elasticsearch, reindexItem)
|
||||
if err != nil {
|
||||
resResult["errno"] = "E20002"
|
||||
resResult["errmsg"] = err.Error()
|
||||
resResult["error"] = err
|
||||
resResult["status"] = false
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
return
|
||||
}
|
||||
resResult["payload"] = taskID
|
||||
resResult["payload"] = ID
|
||||
handler.WriteJSON(w, resResult, http.StatusOK)
|
||||
}
|
||||
|
||||
func reindex(esName string, body *model.InfiniReindex) (string, error) {
|
||||
client := elastic.GetClient(esName)
|
||||
esConfig := elastic.GetConfig(esName)
|
||||
url := fmt.Sprintf("%s/_reindex?wait_for_completion=false", esConfig.Endpoint)
|
||||
source := map[string]interface{}{
|
||||
"index": body.Source.Index,
|
||||
}
|
||||
|
@ -66,19 +65,14 @@ func reindex(esName string, body *model.InfiniReindex) (string, error) {
|
|||
}
|
||||
buf, _ := json.Marshal(esBody)
|
||||
//fmt.Println(string(buf))
|
||||
reindexRes, err := client.Request("POST", url, buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resBody := struct {
|
||||
Task string `json:"task"`
|
||||
}{}
|
||||
err = json.Unmarshal(reindexRes.Body, &resBody)
|
||||
reindexResp, err := client.Reindex(buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if body.ID == "" {
|
||||
body.ID = util.GetUUID()
|
||||
body.TaskId = resBody.Task
|
||||
}
|
||||
body.TaskId = reindexResp.Task
|
||||
body.Status = model.ReindexStatusRunning
|
||||
body.CreatedAt = time.Now()
|
||||
|
||||
|
@ -91,28 +85,21 @@ func reindex(esName string, body *model.InfiniReindex) (string, error) {
|
|||
|
||||
func newResponseBody() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"errno": "0",
|
||||
"errmsg": "",
|
||||
"payload": nil,
|
||||
"status": true,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (handler APIHandler) HandleDeleteRebuildAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
var ids = []string{}
|
||||
id := ps.ByName("id")
|
||||
var ids = []string{id}
|
||||
resBody := newResponseBody()
|
||||
err := handler.DecodeJSON(req, &ids)
|
||||
err := deleteTasksByIds(handler.Config.Elasticsearch, ids)
|
||||
if err != nil {
|
||||
resBody["errno"] = "E30001"
|
||||
resBody["errmsg"] = err.Error()
|
||||
resBody["error"] = err
|
||||
resBody["status"] = false
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
return
|
||||
}
|
||||
err = deleteTasksByTerms(handler.Config.Elasticsearch, ids)
|
||||
if err != nil {
|
||||
resBody["errno"] = "E30002"
|
||||
resBody["errmsg"] = err.Error()
|
||||
}
|
||||
resBody["payload"] = true
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
}
|
||||
|
@ -132,20 +119,20 @@ func (handler APIHandler) HandleGetRebuildListAction(w http.ResponseWriter, req
|
|||
)
|
||||
esResp, err := model.GetRebuildList(esName, from, size, name)
|
||||
if err != nil {
|
||||
resBody["errno"] = "E20003"
|
||||
resBody["errmsg"] = err.Error()
|
||||
resBody["error"] = err.Error()
|
||||
resBody["status"] = false
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
return
|
||||
}
|
||||
err = SyncRebuildResult(esName)
|
||||
if err != nil {
|
||||
resBody["errno"] = "E20004"
|
||||
resBody["errmsg"] = err.Error()
|
||||
resBody["status"] = false
|
||||
resBody["error"] = err
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
resBody["payload"] = formatESSearchResult(esResp)
|
||||
resBody["payload"] = esResp
|
||||
handler.WriteJSON(w, resBody, http.StatusOK)
|
||||
}
|
||||
|
||||
|
@ -189,11 +176,11 @@ func SyncRebuildResult(esName string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func buildTermsQuery(terms []string) string {
|
||||
func buildTermsQuery(fieldName string, terms []string) string {
|
||||
esBody := `{
|
||||
"query":{
|
||||
"terms": {
|
||||
"_id": [
|
||||
"%s": [
|
||||
%s
|
||||
]
|
||||
}
|
||||
|
@ -203,24 +190,14 @@ func buildTermsQuery(terms []string) string {
|
|||
for _, term := range terms {
|
||||
strTerms += fmt.Sprintf(`"%s",`, term)
|
||||
}
|
||||
esBody = fmt.Sprintf(esBody, strTerms[0:len(strTerms)-1])
|
||||
esBody = fmt.Sprintf(esBody, fieldName, strTerms[0:len(strTerms)-1])
|
||||
return esBody
|
||||
}
|
||||
|
||||
func deleteTasksByTerms(esName string, terms []string) error {
|
||||
func deleteTasksByIds(esName string, terms []string) error {
|
||||
client := elastic.GetClient(esName)
|
||||
esConfig := elastic.GetConfig(esName)
|
||||
url := fmt.Sprintf("%s/infinireindex/_delete_by_query", esConfig.Endpoint)
|
||||
esBody := buildTermsQuery(terms)
|
||||
result, err := client.Request("POST", url, []byte(esBody))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var deleteRes = struct {
|
||||
Deleted int `json:"deleted"`
|
||||
Total int `json:"total"`
|
||||
}{}
|
||||
err = json.Unmarshal(result.Body, &deleteRes)
|
||||
esBody := buildTermsQuery("_id", terms)
|
||||
deleteRes, err := client.DeleteByQuery("infinireindex", []byte(esBody))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
15
api/init.go
15
api/init.go
|
@ -17,21 +17,20 @@ func Init(cfg *config.AppConfig) {
|
|||
var pathPrefix = "/_search-center/"
|
||||
//ui.HandleUIMethod(api.POST, "/api/get_indices",index_management.API1)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"dict/_search", handler.GetDictListAction)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"dict/_create", handler.CreateDictItemAction)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"dict/*id", handler.CreateDictItemAction)
|
||||
//ui.HandleUIMethod(api.GET, "/api/dict/:id",handler.GetDictItemAction)
|
||||
ui.HandleUIMethod(api.DELETE, pathPrefix+"dict/:id", handler.DeleteDictItemAction)
|
||||
//ui.HandleUIMethod(api.DELETE, "/api/dict/", handler.DeleteDictItemAction2)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"dict/_update", handler.UpdateDictItemAction)
|
||||
ui.HandleUIMethod(api.PUT, pathPrefix+"dict/:id", handler.UpdateDictItemAction)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"doc/:index/_search", handler.HandleSearchDocumentAction)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"doc/:index/_create", handler.HandleAddDocumentAction)
|
||||
ui.HandleUIMethod(api.PUT, pathPrefix+"doc/:index/:id", handler.HandleUpdateDocumentAction)
|
||||
ui.HandleUIMethod(api.DELETE, pathPrefix+"doc/:index/:id", handler.HandleDeleteDocumentAction)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"indices/_cat", handler.HandleGetIndicesAction)
|
||||
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"rebuild/_create", handler.ReindexAction)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"rebuild/list", handler.HandleGetRebuildListAction)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"rebuild/_delete", handler.HandleDeleteRebuildAction)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"indices/_mappings/:index", handler.HandleGetMappingsAction)
|
||||
ui.HandleUIMethod(api.POST, pathPrefix+"rebuild/*id", handler.ReindexAction)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"rebuild/_search", handler.HandleGetRebuildListAction)
|
||||
ui.HandleUIMethod(api.DELETE, pathPrefix+"rebuild/:id", handler.HandleDeleteRebuildAction)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"_cat/indices", handler.HandleGetIndicesAction)
|
||||
ui.HandleUIMethod(api.GET, pathPrefix+"index/:index/_mappings", handler.HandleGetMappingsAction)
|
||||
|
||||
task.RegisterScheduleTask(task.ScheduleTask{
|
||||
Description: "sync reindex task result to index infinireindex",
|
||||
|
|
|
@ -45875,10 +45875,10 @@ const mappings = {
|
|||
};
|
||||
|
||||
export default {
|
||||
'get /_search-center/indices/_cat': function(req, res){
|
||||
res.send(data)
|
||||
},
|
||||
'get /_search-center/indices/_mappings/:index': function(req, res){
|
||||
res.send(mappings)
|
||||
}
|
||||
// 'get /_search-center/indices/_cat': function(req, res){
|
||||
// res.send(data)
|
||||
// },
|
||||
// 'get /_search-center/indices/_mappings/:index': function(req, res){
|
||||
// res.send(mappings)
|
||||
// }
|
||||
}
|
|
@ -426,7 +426,7 @@ let data = {
|
|||
};
|
||||
|
||||
export default {
|
||||
'get /_search-center/rebuild/list': function(req, res){
|
||||
res.send(data)
|
||||
}
|
||||
// 'get /_search-center/rebuild/list': function(req, res){
|
||||
// res.send(data)
|
||||
// }
|
||||
}
|
|
@ -8,6 +8,7 @@ import Editor, {monaco} from '@monaco-editor/react';
|
|||
import moment from 'moment';
|
||||
import {createDependencyProposals} from './autocomplete';
|
||||
import InputSelect from '@/components/infini/InputSelect';
|
||||
import {getFields} from '@/utils/elasticsearch';
|
||||
|
||||
function findParentIdentifier(textUntilPosition){
|
||||
let chars = textUntilPosition;
|
||||
|
@ -202,6 +203,9 @@ class EditableCell extends React.Component {
|
|||
// return
|
||||
// }
|
||||
const {properties} = doclist.mappings[record._index].mappings;
|
||||
if(!properties[key]){
|
||||
return '';
|
||||
}
|
||||
return properties[key].type;
|
||||
}
|
||||
|
||||
|
@ -296,14 +300,7 @@ class EditableCell extends React.Component {
|
|||
let keys = [];
|
||||
let sortObj = {};
|
||||
if(doclist.mappings){
|
||||
for(let mkey in doclist.mappings){
|
||||
Object.keys(doclist.mappings[mkey].mappings.properties).forEach(key=>{
|
||||
if(!keys.includes(key)){
|
||||
keys.push(key);
|
||||
sortObj[key] = this.isSortable(doclist.mappings[mkey].mappings.properties[key].type);
|
||||
}
|
||||
})
|
||||
}
|
||||
keys = getFields(doclist.index, doclist.mappings)
|
||||
}
|
||||
for(let key of keys){
|
||||
if(["_index"].includes(key)){
|
||||
|
@ -344,7 +341,7 @@ class EditableCell extends React.Component {
|
|||
<Table
|
||||
components={components}
|
||||
bordered
|
||||
rowKey="id"
|
||||
rowKey={record=>(record._index+record.id)}
|
||||
onChange={this.handleTableChange}
|
||||
size="small"
|
||||
loading={doclist.isLoading}
|
||||
|
@ -398,6 +395,12 @@ class Doucment extends React.Component {
|
|||
if(!cluster){
|
||||
return
|
||||
}
|
||||
dispatch({
|
||||
type: 'document/fetchMappings',
|
||||
payload: {
|
||||
cluster,
|
||||
}
|
||||
});
|
||||
dispatch({
|
||||
type: 'document/fetchIndices',
|
||||
payload: {
|
||||
|
@ -510,10 +513,10 @@ class Doucment extends React.Component {
|
|||
//console.log(this.props.document);
|
||||
let clusterIndices = this.props.document.clusterIndices || [];
|
||||
|
||||
clusterIndices = clusterIndices.map((index) =>{
|
||||
clusterIndices = clusterIndices.filter(index => !index.index.startsWith('.')).map((index) =>{
|
||||
return {
|
||||
label: index,
|
||||
value: index,
|
||||
label: index.index,
|
||||
value: index.index,
|
||||
};
|
||||
})
|
||||
const clusters = ["single-es"];
|
||||
|
|
|
@ -3,14 +3,14 @@ import { connect } from 'dva';
|
|||
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
|
||||
import {Steps, Card, Form, Select, Input,Button, Divider,message, InputNumber} from 'antd';
|
||||
import InputSelect from '@/components/infini/InputSelect';
|
||||
import {getFields} from '@/utils/elasticsearch';
|
||||
|
||||
const {Step} = Steps;
|
||||
const {Option} = Select;
|
||||
const {TextArea} = Input;
|
||||
|
||||
@Form.create()
|
||||
@connect(({document,rebuild}) => ({
|
||||
document,
|
||||
@connect(({rebuild}) => ({
|
||||
rebuild,
|
||||
}))
|
||||
class Rebuild extends Component {
|
||||
|
@ -19,12 +19,6 @@ class Rebuild extends Component {
|
|||
}
|
||||
componentDidMount(){
|
||||
const {dispatch} = this.props;
|
||||
dispatch({
|
||||
type:'document/fetchIndices',
|
||||
payload:{
|
||||
cluster: 'sinlge-es'
|
||||
}
|
||||
})
|
||||
dispatch({
|
||||
type: 'rebuild/fetchMappings',
|
||||
payload: {
|
||||
|
@ -37,29 +31,7 @@ class Rebuild extends Component {
|
|||
return [];
|
||||
}
|
||||
let {mappings} = this.props.rebuild;
|
||||
let filterMappings = {};
|
||||
if(index.indexOf("*")>0){
|
||||
index = index.replace("*", '');
|
||||
for(let key in mappings){
|
||||
if(key.startsWith(index)){
|
||||
filterMappings['key'] = mappings[key];
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(!mappings[index]){
|
||||
return [];
|
||||
}
|
||||
filterMappings[index] = mappings[index];
|
||||
}
|
||||
|
||||
let fields = [];
|
||||
for(let key in filterMappings){
|
||||
for(let fi in filterMappings[key].mappings.properties){
|
||||
fields.push(fi);
|
||||
}
|
||||
}
|
||||
|
||||
return fields;
|
||||
return getFields(index, mappings);
|
||||
}
|
||||
handleSourceIndexChange = (v) =>{
|
||||
const {dispatch, form} = this.props;
|
||||
|
@ -74,12 +46,12 @@ class Rebuild extends Component {
|
|||
})
|
||||
}
|
||||
renderSteps = (currentStep) => {
|
||||
let {clusterIndices} = this.props.document;
|
||||
clusterIndices = clusterIndices || [];
|
||||
let indices = clusterIndices.map((item)=>{
|
||||
let {mappings} = this.props.rebuild;
|
||||
mappings = mappings || {};
|
||||
let indices = Object.keys(mappings).map((key)=>{
|
||||
return {
|
||||
label: item,
|
||||
value: item,
|
||||
label: key,
|
||||
value: key,
|
||||
}
|
||||
});
|
||||
var stepDom = '';
|
||||
|
|
|
@ -79,7 +79,9 @@ class RebuildList extends React.Component {
|
|||
const {dispatch} = this.props;
|
||||
dispatch({
|
||||
type: 'rebuildlist/deleteTask',
|
||||
payload: [record.id],
|
||||
payload: {
|
||||
id: record.id,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import {getDocList, saveDoc, deleteDoc, addDoc, getIndices} from '@/services/doc';
|
||||
import {getDocList, saveDoc, deleteDoc, addDoc} from '@/services/doc';
|
||||
import {getMappings, getIndices} from '@/services/indices';
|
||||
import {formatESSearchResult} from '@/utils/utils';
|
||||
import { message } from 'antd';
|
||||
|
||||
function encodeObjectField(doc){
|
||||
|
@ -44,7 +46,7 @@ export default {
|
|||
}
|
||||
});
|
||||
let res = yield call(getDocList, payload);
|
||||
if(res.errno != "0"){
|
||||
if(res.status === false){
|
||||
message.warn("加载数据失败")
|
||||
yield put({
|
||||
type: 'saveData',
|
||||
|
@ -54,7 +56,8 @@ export default {
|
|||
})
|
||||
return
|
||||
}
|
||||
let indices = Object.keys(res.payload.mappings); //indices state can remove
|
||||
res.payload = formatESSearchResult(res.payload);
|
||||
let indices = []; //indices state can remove
|
||||
if(res.payload.data && res.payload.data.length > 0){
|
||||
for(let doc of res.payload.data){
|
||||
if(!indices.includes(doc._index)){
|
||||
|
@ -90,7 +93,7 @@ export default {
|
|||
return;
|
||||
}
|
||||
let res = yield call(saveDoc, payload);
|
||||
if(res.errno != "0"){
|
||||
if(res.status === false){
|
||||
message.warn("保存数据失败")
|
||||
return
|
||||
}
|
||||
|
@ -118,7 +121,7 @@ export default {
|
|||
if(typeof res == 'string'){
|
||||
res = JSON.parse(res);
|
||||
}
|
||||
if(res.errno != "0"){
|
||||
if(res.status === false){
|
||||
message.warn("删除数据失败")
|
||||
return
|
||||
}
|
||||
|
@ -143,7 +146,7 @@ export default {
|
|||
return;
|
||||
}
|
||||
let res = yield call(addDoc, payload);
|
||||
if(res.errno != "0"){
|
||||
if(res.status === false){
|
||||
message.warn("添加文档失败")
|
||||
return
|
||||
}
|
||||
|
@ -162,7 +165,7 @@ export default {
|
|||
},
|
||||
*fetchIndices({payload}, {call, put}){
|
||||
let resp = yield call(getIndices)
|
||||
if(resp.errno != "0"){
|
||||
if(resp.status === false){
|
||||
message.warn("获取数据失败")
|
||||
return
|
||||
}
|
||||
|
@ -173,6 +176,19 @@ export default {
|
|||
cluster: payload.cluster,
|
||||
}
|
||||
})
|
||||
},
|
||||
*fetchMappings({payload}, {call, put}){
|
||||
let resp = yield call(getMappings, payload);
|
||||
if(resp.status === false){
|
||||
message.warn("get mappings failed")
|
||||
return
|
||||
}
|
||||
yield put({
|
||||
type: 'saveData',
|
||||
payload: {
|
||||
mappings: resp.payload,
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
reducers: {
|
||||
|
|
|
@ -15,7 +15,7 @@ export default {
|
|||
effects:{
|
||||
*addTask({payload}, {call, put}){
|
||||
let resp = yield call(reindex, payload);
|
||||
if(resp.errno != "0"){
|
||||
if(!resp.status){
|
||||
message.warn("rebuild failed")
|
||||
return
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ export default {
|
|||
},
|
||||
*fetchMappings({payload}, {call, put}){
|
||||
let resp = yield call(getMappings, payload);
|
||||
if(resp.errno != "0"){
|
||||
if(resp.status === false){
|
||||
message.warn("get mappings failed")
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import {getRebuildList,reindex, deleteRebuild} from '@/services/rebuild';
|
||||
import { message } from 'antd';
|
||||
import {formatESSearchResult} from '@/utils/utils';
|
||||
|
||||
|
||||
const delay = (ms) => new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
|
@ -25,10 +27,11 @@ export default {
|
|||
from: (payload.pageIndex - 1) * payload.pageSize,
|
||||
size: payload.pageSize,
|
||||
})
|
||||
if(resp.errno != "0"){
|
||||
if(!resp.status){
|
||||
message.error('fetch data failed')
|
||||
return
|
||||
}
|
||||
resp.payload = formatESSearchResult(resp.payload)
|
||||
yield put({
|
||||
type: 'saveData',
|
||||
payload: {
|
||||
|
@ -84,12 +87,12 @@ export default {
|
|||
}
|
||||
})
|
||||
let resp = yield call(deleteRebuild, payload);
|
||||
if(resp.errno != "0"){
|
||||
if(resp.status === false){
|
||||
message.error("delete failed")
|
||||
return
|
||||
}
|
||||
let {data, total} = yield select(state=>state.rebuildlist);
|
||||
let newData = data.filter(item=> !payload.includes(item.id));
|
||||
let newData = data.filter(item=> payload.id != item.id);
|
||||
yield put({
|
||||
type: 'saveData',
|
||||
payload: {
|
||||
|
|
|
@ -63,10 +63,11 @@ export default {
|
|||
effects: {
|
||||
*fetchDictList({payload}, {call, put}){
|
||||
const resp = yield call(getDictList, payload);
|
||||
if(resp.errno != "0" || !resp.data.Result){
|
||||
if(resp.status ===false){
|
||||
message.error(resp.error)
|
||||
return
|
||||
}
|
||||
resp.data.Result = resp.data.Result.map((item)=>{
|
||||
resp.payload.Result = resp.payload.Result.map((item)=>{
|
||||
item.content = utf8.decode(atob(item.content))
|
||||
return item;
|
||||
})
|
||||
|
@ -77,8 +78,8 @@ export default {
|
|||
yield put({
|
||||
type: 'saveData',
|
||||
payload: {
|
||||
dictList: resp.data.Result,
|
||||
total: resp.data.Total,
|
||||
dictList: resp.payload.Result,
|
||||
total: resp.payload.Total,
|
||||
search: search,
|
||||
},
|
||||
});
|
||||
|
@ -90,7 +91,7 @@ export default {
|
|||
}
|
||||
upVals.content = btoa(utf8.encode(upVals.content));
|
||||
const rel = yield call(addDict, upVals);
|
||||
if(rel.errno != "0"){
|
||||
if(rel.status === false){
|
||||
message.warn('添加失败:'+ rel.errmsg)
|
||||
return
|
||||
}
|
||||
|
@ -110,7 +111,7 @@ export default {
|
|||
let rawContent = payload.content;
|
||||
payload.content = btoa(utf8.encode(payload.content));
|
||||
const rel = yield call(updateDict, payload);
|
||||
if(rel.errno != "0"){
|
||||
if(rel.status === false){
|
||||
message.warn('修改:'+ rel.errmsg)
|
||||
return
|
||||
}
|
||||
|
@ -131,7 +132,7 @@ export default {
|
|||
if(typeof rel !== 'object'){
|
||||
rel = JSON.parse(rel);
|
||||
}
|
||||
if(rel.errno != "0"){
|
||||
if(rel.status === false){
|
||||
message.warn('删除失败:'+ rel.errmsg)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -25,15 +25,10 @@ export async function deleteDoc(params) {
|
|||
}
|
||||
|
||||
export async function addDoc(params) {
|
||||
let id = params.data.id || '';
|
||||
delete(params.data, 'id');
|
||||
return request(`${pathPrefix}/doc/${params.index}/_create`, {
|
||||
return request(`${pathPrefix}/doc/${params.index}/${id}`, {
|
||||
method: 'POST',
|
||||
body: params.data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getIndices(params) {
|
||||
return request(`${pathPrefix}/indices/_cat`, {
|
||||
method: 'GET'
|
||||
});
|
||||
}
|
|
@ -3,9 +3,16 @@ import {pathPrefix} from './common';
|
|||
|
||||
export async function getMappings(payload){
|
||||
let index = payload.index || '*'
|
||||
let url = `${pathPrefix}/indices/_mappings/${index}`;
|
||||
let url = `${pathPrefix}/index/${index}/_mappings`;
|
||||
return request(url,{
|
||||
method: 'GET',
|
||||
expirys: 0,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function getIndices(params) {
|
||||
return request(`${pathPrefix}/_cat/indices`, {
|
||||
method: 'GET'
|
||||
});
|
||||
}
|
|
@ -2,7 +2,8 @@ import request from '@/utils/request';
|
|||
import {pathPrefix} from './common';
|
||||
|
||||
export async function reindex(payload){
|
||||
let url = `${pathPrefix}/rebuild/_create`;
|
||||
let id = payload.id || '';
|
||||
let url = `${pathPrefix}/rebuild/${id}`;
|
||||
return request(url,{
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
|
@ -11,17 +12,17 @@ export async function reindex(payload){
|
|||
}
|
||||
|
||||
export async function deleteRebuild(payload){
|
||||
let url = `${pathPrefix}/rebuild/_delete`;
|
||||
let id = payload.id;
|
||||
let url = `${pathPrefix}/rebuild/${id}`;
|
||||
return request(url,{
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
method: 'DELETE',
|
||||
expirys: 0,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function getRebuildList(payload){
|
||||
let url = `${pathPrefix}/rebuild/list?`;
|
||||
let url = `${pathPrefix}/rebuild/_search?`;
|
||||
payload.from && (url+=`from=${payload.from}`)
|
||||
payload.size && (url+=`&size=${payload.size}`)
|
||||
payload.name && (url+=`&name=${payload.name}`)
|
||||
|
|
|
@ -13,7 +13,8 @@ export async function getDictList(payload){
|
|||
}
|
||||
|
||||
export async function addDict(payload){
|
||||
return request(`${pathPrefix}/dict/_create`,{
|
||||
let id = payload.id || '';
|
||||
return request(`${pathPrefix}/dict/${id}`,{
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
expirys: 0,
|
||||
|
@ -28,8 +29,9 @@ export async function deleteDict(payload){
|
|||
}
|
||||
|
||||
export async function updateDict(payload){
|
||||
return request(`${pathPrefix}/dict/_update`,{
|
||||
method: 'POST',
|
||||
let id = payload.id || '';
|
||||
return request(`${pathPrefix}/dict/${id}`,{
|
||||
method: 'PUT',
|
||||
body: payload,
|
||||
expirys: 0,
|
||||
});
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
export function getFields(index, mappings){
|
||||
if(!index){
|
||||
return [];
|
||||
}
|
||||
let filterMappings = {};
|
||||
if(index.indexOf("*")>0){
|
||||
index = index.replace("*", '');
|
||||
for(let key in mappings){
|
||||
if(key.startsWith(index)){
|
||||
filterMappings['key'] = mappings[key];
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(!mappings[index]){
|
||||
return [];
|
||||
}
|
||||
filterMappings[index] = mappings[index];
|
||||
}
|
||||
|
||||
let fields = [];
|
||||
for(let key in filterMappings){
|
||||
for(let fi in filterMappings[key].mappings.properties){
|
||||
fields.push(fi);
|
||||
}
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
|
@ -181,3 +181,25 @@ export function formatWan(val) {
|
|||
export function isAntdPro() {
|
||||
return window.location.hostname === 'preview.pro.ant.design';
|
||||
}
|
||||
|
||||
export function formatESSearchResult(esResp) {
|
||||
const total = esResp.hits.total
|
||||
if(esResp.hits.hits.length == 0){
|
||||
return {
|
||||
total: total,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
let dataArr = [];
|
||||
for(let hit of esResp.hits.hits) {
|
||||
if(!hit._source.id){
|
||||
hit._source["id"] = hit._id
|
||||
}
|
||||
hit._source["_index"] = hit._index
|
||||
dataArr.push(hit._source)
|
||||
}
|
||||
return {
|
||||
total: total,
|
||||
data: dataArr,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue