diff --git a/api/index_management/document.go b/api/index_management/document.go index 1d38dcd8..ab639e5b 100644 --- a/api/index_management/document.go +++ b/api/index_management/document.go @@ -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 { diff --git a/api/index_management/index.go b/api/index_management/index.go index 0e40ac3f..74f7c391 100644 --- a/api/index_management/index.go +++ b/api/index_management/index.go @@ -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) } diff --git a/api/index_management/indices.go b/api/index_management/indices.go index f42d1135..b2baf635 100644 --- a/api/index_management/indices.go +++ b/api/index_management/indices.go @@ -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) +} diff --git a/api/index_management/rebuild.go b/api/index_management/rebuild.go index cb384ef1..304fa456 100644 --- a/api/index_management/rebuild.go +++ b/api/index_management/rebuild.go @@ -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) + reindexResp, err := client.Reindex(buf) if err != nil { return "", err } - resBody := struct { - Task string `json:"task"` - }{} - err = json.Unmarshal(reindexRes.Body, &resBody) - if err != nil { - return "", err + if body.ID == "" { + body.ID = util.GetUUID() } - 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 } diff --git a/api/init.go b/api/init.go index d95ac928..cf2baae5 100644 --- a/api/init.go +++ b/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", diff --git a/web/mock/datamanagement/indices.js b/web/mock/datamanagement/indices.js index 319ea1f8..cebf4b26 100644 --- a/web/mock/datamanagement/indices.js +++ b/web/mock/datamanagement/indices.js @@ -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) + // } } \ No newline at end of file diff --git a/web/mock/datamanagement/rebuild.js b/web/mock/datamanagement/rebuild.js index ce06788b..22e18bc1 100644 --- a/web/mock/datamanagement/rebuild.js +++ b/web/mock/datamanagement/rebuild.js @@ -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) + // } } \ No newline at end of file diff --git a/web/src/pages/DataManagement/Document.js b/web/src/pages/DataManagement/Document.js index af2c62bd..b4e6c9ea 100644 --- a/web/src/pages/DataManagement/Document.js +++ b/web/src/pages/DataManagement/Document.js @@ -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 {