From 05d5acd62cb9b28f3854e6815284d6842bd0460e Mon Sep 17 00:00:00 2001 From: silenceqi Date: Mon, 18 Jan 2021 23:17:26 +0800 Subject: [PATCH] add index settings api --- api/index_management/indices.go | 38 +++++++++++++++++++++++++++++++++ api/init.go | 2 ++ 2 files changed, 40 insertions(+) diff --git a/api/index_management/indices.go b/api/index_management/indices.go index 5aeea770..6cf31bc2 100644 --- a/api/index_management/indices.go +++ b/api/index_management/indices.go @@ -55,3 +55,41 @@ func (handler APIHandler) HandleGetIndicesAction(w http.ResponseWriter, req *htt resBody["payload"] = catIndices handler.WriteJSON(w, resBody, http.StatusOK) } + +func (handler APIHandler) HandleGetSettingsAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + client := elastic.GetClient(handler.Config.Elasticsearch) + indexName := ps.ByName("index") + resBody := newResponseBody() + indexes, err := client.GetIndexSettings(indexName) + if err != nil { + resBody["status"] = false + resBody["error"] = err + handler.WriteJSON(w, resBody, http.StatusOK) + return + } + resBody["payload"] = indexes + handler.WriteJSON(w, resBody, http.StatusOK) +} + +func (handler APIHandler) HandleUpdateSettingsAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + client := elastic.GetClient(handler.Config.Elasticsearch) + indexName := ps.ByName("index") + settings := map[string]interface{}{} + resBody := newResponseBody() + err := handler.DecodeJSON(req, &settings) + if err != nil { + resBody["status"] = false + resBody["error"] = err + handler.WriteJSON(w, resBody, http.StatusOK) + return + } + err = client.UpdateIndexSettings(indexName, settings) + if err != nil { + resBody["status"] = false + resBody["error"] = err + handler.WriteJSON(w, resBody, http.StatusOK) + return + } + resBody["payload"] = true + handler.WriteJSON(w, resBody, http.StatusOK) +} \ No newline at end of file diff --git a/api/init.go b/api/init.go index 7dd507ce..e92756cf 100644 --- a/api/init.go +++ b/api/init.go @@ -31,6 +31,8 @@ func Init(cfg *config.AppConfig) { 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) + ui.HandleUIMethod(api.GET, pathPrefix+"index/:index/_settings", handler.HandleGetSettingsAction) + ui.HandleUIMethod(api.PUT, pathPrefix+"index/:index/_settings", handler.HandleUpdateSettingsAction) task.RegisterScheduleTask(task.ScheduleTask{ Description: "sync reindex task result to index infinireindex",