add index settings api

This commit is contained in:
silenceqi 2021-01-18 23:17:26 +08:00
parent 5e685f47c7
commit 05d5acd62c
2 changed files with 40 additions and 0 deletions

View File

@ -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)
}

View File

@ -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",