diff --git a/server/elasticClient/client.go b/server/elasticClient/client.go index 97ea936eaf6eccb153446183010709d07a3f010b..e69ac2e498e7b396185bd099e346e01c9f6a744f 100644 --- a/server/elasticClient/client.go +++ b/server/elasticClient/client.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "fmt" + "io" "net" "net/http" "time" @@ -51,6 +52,32 @@ func InitElasticClient() { Global_elastic = &ElasticClient_v7{ Client: es_client, - Ctx: context.Background(), + Ctx: pluginclient.Global_Context, + } +} + +func (client *ElasticClient_v7) Search(index string, body io.Reader) ([]byte, error) { + resp, err := Global_elastic.Client.Search( + Global_elastic.Client.Search.WithContext(context.Background()), + Global_elastic.Client.Search.WithIndex(index), + Global_elastic.Client.Search.WithBody(body), + Global_elastic.Client.Search.WithTrackTotalHits(true), + Global_elastic.Client.Search.WithPretty(), + ) + if err != nil { + err = errors.Errorf("%+v **errstack**0", err.Error()) + return nil, err + } + defer resp.Body.Close() + if resp.IsError() { + err = errors.Errorf("%+v **errstack**0", resp.String()) + return nil, err + } else { + resp_body_bytes, err := io.ReadAll(resp.Body) + if err != nil { + err = errors.Errorf("%+v **warn**0", err.Error()) + return nil, err + } + return resp_body_bytes, nil } } diff --git a/server/pluginclient/pluginClient.go b/server/pluginclient/pluginClient.go index 6bf797cfc42a62c90534853e072364e2158c7306..aa731e8392fa23e763a47ec04f46541721fe8936 100644 --- a/server/pluginclient/pluginClient.go +++ b/server/pluginclient/pluginClient.go @@ -3,7 +3,6 @@ package pluginclient import ( "context" - "gitee.com/openeuler/PilotGo-plugin-elk/conf" "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/plugin/client" ) @@ -13,7 +12,6 @@ var Global_Client *client.Client var Global_Context context.Context func InitPluginClient() { - PluginInfo.Url = "http://" + conf.Global_Config.Elk.Addr Global_Client = client.DefaultClient(PluginInfo) // 注册插件扩展点