package v7
import (
"context"
"fmt"
"github.com/1340691923/ElasticView/pkg/infrastructure/es_sdk/pkg"
"github.com/1340691923/ElasticView/pkg/infrastructure/es_sdk/pkg/proto"
elasticV7 "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/elastic/go-elasticsearch/v7/esutil"
"net/http"
)
type EsClient7 struct {
client *elasticV7.Client
}
func NewEsClient7(cfg proto.Config) (pkg.EsI, error) {
obj := &EsClient7{}
esCfg, err := cfg.ConvertV7(nil, nil, nil)
if err != nil {
return nil, err
}
obj.init(esCfg)
return obj, nil
}
func (this *EsClient7) init(config elasticV7.Config) (err error) {
this.client, err = elasticV7.NewClient(config)
if err != nil {
fmt.Printf("Error creating the client: %s\n", err)
return
}
return
}
func (this *EsClient7) Version() int {
return 7
}
func (this *EsClient7) Ping(
ctx context.Context,
) (
res *proto.Response,
err error,
) {
httpRes, err := this.client.Ping(
this.client.Ping.WithContext(ctx),
)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) GetMapping(
ctx context.Context,
indexNames []string,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesGetMappingRequest{Index: indexNames}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) SnapshotCreate(
ctx context.Context,
repository string,
snapshot string,
waitForCompletion *bool,
reqJson proto.Json,
) (
res *proto.Response, err error) {
snapshotCreateService := esapi.SnapshotCreateRequest{
Repository: repository,
Snapshot: snapshot,
Body: esutil.NewJSONReader(reqJson),
WaitForCompletion: waitForCompletion,
}
var httpRes *esapi.Response
httpRes, err = snapshotCreateService.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) PerformRequest(
ctx context.Context,
req *http.Request,
) (
res *proto.Response, err error) {
if req != nil {
req = req.WithContext(ctx)
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
httpRes, err := this.client.Perform(req)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) SnapshotDelete(
ctx context.Context,
repository string,
snapshot string,
) (
res *proto.Response,
err error) {
req := esapi.SnapshotDeleteRequest{
Repository: repository,
Snapshot: snapshot,
}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) RestoreSnapshot(
ctx context.Context,
repository string,
snapshot string,
waitForCompletion *bool,
reqJson proto.Json,
) (
res *proto.Response,
err error,
) {
request := esapi.SnapshotRestoreRequest{
Snapshot: snapshot,
Repository: repository,
Body: esutil.NewJSONReader(reqJson),
WaitForCompletion: waitForCompletion,
}
httpRes, err := request.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) Refresh(
ctx context.Context,
indexNames []string,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesRefreshRequest{Index: indexNames}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) Open(
ctx context.Context,
indexNames []string,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesOpenRequest{Index: indexNames}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) Flush(
ctx context.Context,
indexNames []string,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesFlushRequest{Index: indexNames}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) IndicesClearCache(
ctx context.Context,
indexNames []string,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesClearCacheRequest{Index: indexNames}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) IndicesClose(
ctx context.Context,
indexNames []string,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesCloseRequest{Index: indexNames}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) IndicesForcemerge(
ctx context.Context,
indexNames []string,
maxNumSegments *int,
) (
res *proto.Response,
err error,
) {
req := esapi.IndicesForcemergeRequest{Index: indexNames, MaxNumSegments: maxNumSegments}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) DeleteByQuery(
ctx context.Context,
indexNames []string,
documents []string,
body interface{},
) (
res *proto.Response,
err error,
) {
req := esapi.DeleteByQueryRequest{Index: indexNames, DocumentType: []string{}, Body: esutil.NewJSONReader(body)}
httpRes, err := req.Do(ctx, this.client)
if err != nil {
return nil, err
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) SnapshotStatus(
ctx context.Context,
repository string,
snapshot []string,
ignoreUnavailable *bool,
) (
res *proto.Response,
err error,
) {
request := esapi.SnapshotStatusRequest{
Repository: repository,
Snapshot: snapshot,
IgnoreUnavailable: ignoreUnavailable,
}
httpRes, err := request.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) SnapshotGetRepository(
ctx context.Context,
repository []string,
) (
res *proto.Response,
err error,
) {
request := esapi.SnapshotGetRepositoryRequest{
Repository: repository,
}
httpRes, err := request.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) SnapshotCreateRepository(
ctx context.Context,
repository string,
reqJson proto.Json,
) (
res *proto.Response,
err error,
) {
request := esapi.SnapshotCreateRepositoryRequest{
Repository: repository,
Body: esutil.NewJSONReader(reqJson),
}
httpRes, err := request.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) SnapshotDeleteRepository(
ctx context.Context,
repository []string,
) (
res *proto.Response,
err error,
) {
request := esapi.SnapshotDeleteRepositoryRequest{
Repository: repository,
}
httpRes, err := request.Do(ctx, this.client)
if err != nil {
return
}
res, err = proto.NewResponse(httpRes.StatusCode, httpRes.Header, httpRes.Body)
return
}
func (this *EsClient7) GetIndices(
ctx context.Context,
catRequest proto.CatIndicesRequest,
) (
res *proto.Response,
err error,
) {
req := esapi.CatIndicesRequest{
Index: catRequest.Index,
Bytes: catRequest.By