0


Go实战全家桶之十三:go esWebClient基于泛型的通用ES客户端eswebrequest封装

go1.20 自动完成结构体转换。

测试用例

package eswebrequest

import (
    "fmt"
    "git.ichub.com/general/webcli120/goconfig/ichubconfig"
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "git.ichub.com/general/webcli120/goweb/pagees/esconst"

    "git.ichub.com/general/webcli120/goweb/pagereqcli"
    "github.com/agiledragon/gomonkey"
    "github.com/olivere/elastic/v7"
    "github.com/sirupsen/logrus"
    "github.com/stretchr/testify/suite"

    "reflect"
    "testing"
)

type TestCmsWebRequestSuite struct {
    suite.Suite

    rootdir string
    dbtype  string
    cfg     *ichubconfig.IchubConfig
}

func TestCmsWebRequestSuites(t *testing.T) {
    suite.Run(t, new(TestCmsWebRequestSuite))
}

var patches *gomonkey.Patches // = gomonkey.ApplyGlobalVar(&common.Env, "test")

func (suite *TestCmsWebRequestSuite) SetupSuite() {
    logrus.Info(" setup suite")

    // patches = gomonkey.ApplyGlobalVar(&common.Env, "test")
    // defer patches.Reset()
}

func (suite *TestCmsWebRequestSuite) TearDownSuite() {
    logrus.Info(" teardown suite")
    // patches.Reset()

}
func (suite *TestCmsWebRequestSuite) SetupTest() {
    logrus.Info(" setup test")
}

func (suite *TestCmsWebRequestSuite) TearDownTest() {
    logrus.Info(" teardown test")
}

 

type MyStruct struct{}

func (ms *MyStruct) MethodToMock() string {
    return "original"
}

func (this *TestCmsWebRequestSuite) Test102_MockStructMethod() {
    var ms *MyStruct
    patches := gomonkey.ApplyMethod(reflect.TypeOf(ms), "MethodToMock", func(_ *MyStruct) string {
       return "mocked"
    })
    defer patches.Reset()

    // 现在 ms.MethodToMock() 将返回 "mocked"
    result := ms.MethodToMock()
    if result != "mocked" {
       logrus.Errorf("Expected 'mocked', got '%s'", result)
       return
    }

    golog.Info("ok")
}

func (this *TestCmsWebRequestSuite) Test103_MakeQuery() {

    var q = pagereqcli.Default()
    q.CmdType = esconst.ES_HTTPCLI_QUERY
    q.IndexName = "dev_wms_orderlist"
    q.RestTotalHitsAsInt = true
    var query = elastic.NewBoolQuery().Must(elastic.NewTermQuery("id", 919932898974433283))
    q.Query(query)

    golog.Info(q)

}
func (this *TestCmsWebRequestSuite) Test104_WebReques() {

    var q = DefaultOf[*CmsListEs](elastic.NewMatchAllQuery())
    q.SetPageSize(2)

    var result = q.GeneralQuery()

    golog.Info(result)
}
func (this *TestCmsWebRequestSuite) Test105_WebRequest() {

    var q = Default[*CmsListEs]()
    q.Query(elastic.NewTermQuery("content_id", 895864411552153601))
    q.SetPageSize(2)
    var result = q.GeneralQuery()

    golog.Info(result)
}

func (this *TestCmsWebRequestSuite) Test106_WebRequest() {

    var cmd = DefaultCmd[*CmsListEs]()
    cmd.EsMust().EsTerm("content_id", 895864411552153601)
    scriptString := fmt.Sprintf("ctx._source.user_name=params.user_name;")
    updateInfo := make(map[string]interface{})
    updateInfo["user_name"] = "abc"
    var result = cmd.UpdateByQuery(scriptString, updateInfo)
    var result1 = cmd.UpdateByQueryParam(updateInfo)
    if result.IsFailed() {
       golog.Error(result, result1)
    }

}通用

查询

package eswebrequest

import (
    "git.ichub.com/general/webcli120/goconfig/base/baseiface"
    "git.ichub.com/general/webcli120/goconfig/base/jsonutils"
    "git.ichub.com/general/webcli120/goweb/page"
    "git.ichub.com/general/webcli120/goweb/pagees/esconst"
    "git.ichub.com/general/webcli120/goweb/pagemodel"
    "git.ichub.com/general/webcli120/goweb/pagereq"
    "git.ichub.com/general/webcli120/goweb/pagereqcli"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient"
    "github.com/gogf/gf/util/gconv"
    "github.com/olivere/elastic/v7"
    "gorm.io/gorm/schema"

    "reflect"
)

// result, *result.Data.(*[]*model.CmsColumnEs)
type WebRequest[T schema.Tabler] struct {
    *pagereqcli.HttpCliQuery
    Records     []T `json:"records"`
    EsWebClient *eswebclient.EsWebClient
}

func Default[T schema.Tabler]() *WebRequest[T] {
    var q = &WebRequest[T]{
       HttpCliQuery: pagereqcli.Default(),
       EsWebClient:  eswebclient.FindBeanEsWebClient(),
    }

    q.Records = []T{}
    q.SetModels(&q.Records)
    q.SetIndexTable(NewOfTablerType[T]())
    return q
}
func DefaultOf[T schema.Tabler](generalQ elastic.Query) *WebRequest[T] {
    var q = Default[T]()
    q.Query(generalQ)
    return q
}
func (self *WebRequest[T]) Query(q elastic.Query) *WebRequest[T] {

    self.HttpCliQuery.Query(q)
    return self
}
func (self *WebRequest[T]) SetModels(models any) *WebRequest[T] {
    self.HttpCliQuery.SetModels(models)
    return self
}

// get
func (self *WebRequest[T]) Get(ids any) *pagemodel.PageResult[T] {
    var result = self.CmsGet(self.IndexTable(), ids) //HttpCliQuery) //    result.Data = self.Records
    var pageResult = pagemodel.NewPageResult[T]()
    pageResult.ValueOfPageResult(result) //    pageResult.Data = *self.Records
    pageResult.PageResult.Data = self.Records

    return pageResult
}
func (self *WebRequest[T]) CmsGet(indexTable baseiface.IIndexTable, ids any) *page.PageResult {
    var id = gconv.String(ids)
    var q = self.cms2QueryReq(indexTable, id)
    q.CmdType = esconst.ES_QUERY_TYPE_GET

    var result = self.EsWebClient.Get(self.IndexTable().TableName(), id)
    if result.IsSuccess() {
       jsonutils.FromJson(jsonutils.ToJsonStr(result.Data), indexTable)
       result.Data = self.IndexTable()
    }
    return result

}
func (self *WebRequest[T]) cms2QueryReq(indexTable baseiface.IIndexTable, id string) *pagereq.QueryRequest {
    var q = pagereq.Default()
    q.IndexName = indexTable.TableName()
    q.Id = id

    return q
}

func (self *WebRequest[T]) GeneralQuery() *pagemodel.PageResult[T] {
    var result = self.EsWebClient.GeneralQuerySource(self.HttpCliQuery) // result.Data = self.Records
    var pageResult = pagemodel.NewPageResult[T]()
    pageResult.ValueOfPageResult(result) //    pageResult.Data = *self.Records
    pageResult.PageResult.Data = self.Records

    return pageResult
}
func (self *WebRequest[T]) GeneralQueryMax() *pagemodel.PageResult[T] {
    self.SetPageSize(10000)
    return self.GeneralQuery()
}

func (self *WebRequest[T]) Result2List(result *page.PageResult) []T {
    return *result.Data.(*[]T)
}
func (self *WebRequest[T]) Result2Tablers(result *pagemodel.PageResult[T]) []schema.Tabler {

    var rs = make([]schema.Tabler, 0)
    for _, i := range result.Data {
       rs = append(rs, i)
    }
    return rs
}
func (self *WebRequest[T]) Stru2Filter(result *pagemodel.PageResult[T]) []any {

    var rs = self.Result2Tablers(result)
    return pagemodel.Stru2Filter(rs, self.Source)

}
func NewOfTablerType[T schema.Tabler]() T {
    var t T
    var typeOf = reflect.TypeOf(t)
    if typeOf.Kind() == reflect.Ptr {
       typeOf = typeOf.Elem()
    }
    var value = reflect.New(typeOf)
    return value.Interface().(T)
}

func (self *WebRequest[T]) CmsExistId(id string) *page.PageResult {

    var q = self.cms2QueryReq(self.IndexTable(), id)
    q.CmdType = esconst.ES_QUERY_TYPE_EXISTS_ID

    return self.EsWebClient.ExistId(q)

}更新

修改

package eswebrequest

import (
    "errors"
    "fmt"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/base/baseiface"
    "git.ichub.com/general/webcli120/goconfig/base/jsonutils"
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "git.ichub.com/general/webcli120/goweb/page"
    "git.ichub.com/general/webcli120/goweb/pagees/esconst"
    "git.ichub.com/general/webcli120/goweb/pagereq"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient"
    "github.com/gogf/gf/util/gconv"
    "gorm.io/gorm/schema"
)

type CmdRequest[T schema.Tabler] struct {
    *pagereq.CmdRequest
    EsWebClient *eswebclient.EsWebClient
}

func DefaultCmd[T schema.Tabler]() *CmdRequest[T] {
    var q = &CmdRequest[T]{
       CmdRequest:  pagereq.DefaultCmd(),
       EsWebClient: eswebclient.FindBeanEsWebClient(),
    }

    q.SetIndexTable(NewOfTablerType[T]())
    return q
}
func (self *CmdRequest[T]) WebSaveIndex(id any) *basedto.IchubResult {
    var result = self.insertIfNoIndex(id)
    if result.IsFailed() {
       golog.Error(result)
    }
    return result

}

func (self *CmdRequest[T]) SaveIndex(id any, doc map[string]any) *basedto.IchubResult {

    //return self.insertIfNoIndex(id)
    var cmd = self.cms2CmdReq(self.IndexTable(), gconv.String(id), doc)
    cmd.CmdType = esconst.ES_CMD_TYPE_INSERT_IF_NOTEXISTS
    return self.EsWebClient.InsertIfNotExists(cmd)

}
func (self *CmdRequest[T]) SaveIndexStru(id any, stru any) *basedto.IchubResult {
    return self.SaveIndex(id, jsonutils.Stru2Map(stru))
}

func (self *CmdRequest[T]) insertIfNoIndex(id any) *basedto.IchubResult {

    var cmd = self.cms2CmdReq(self.IndexTable(), gconv.String(id), self.IndexTable())
    cmd.CmdType = esconst.ES_CMD_TYPE_INSERT_IF_NOTEXISTS
    var result = self.EsWebClient.InsertIfNotExists(cmd)
    if result.IsFailed() {
       golog.Error("[CmsInsertIfNoIndex] ", result)
    }
    return result
}
func (self *CmdRequest[T]) cms2CmdReq(indexTable baseiface.IIndexTable, id string, some any) *pagereq.CmdRequest {
    var cmd = pagereq.DefaultCmd()
    cmd.IndexName = indexTable.TableName()
    cmd.Id = id
    if some != nil {
       cmd.Doc = jsonutils.Stru2Map(some)
    }
    return cmd
}
func (self *CmdRequest[T]) CmsExistId(id string) *page.PageResult {

    var q = self.cms2QueryReq(self.IndexTable(), id)
    q.CmdType = esconst.ES_QUERY_TYPE_EXISTS_ID

    return self.EsWebClient.ExistId(q)

}
func (self *CmdRequest[T]) cms2QueryReq(indexTable baseiface.IIndexTable, id string) *pagereq.QueryRequest {
    var q = pagereq.Default()
    q.IndexName = indexTable.TableName()
    q.Id = id

    return q
}
func (self *CmdRequest[T]) SaveIndexAny(indexTable eswebclient.IndexMapping) *basedto.IchubResult {

    return nil //self.SaveIndex(indexTable.TablePkeyValue(), jsonutils.Stru2Map(indexTable))

}

func (self *CmdRequest[T]) UpdataParam2Script(fieldAsParams map[string]interface{}) string {
    var scriptString string
    for key := range fieldAsParams {
       scriptString += fmt.Sprintf("ctx._source.%s=params.%s;", key, key)
    }
    return scriptString
}

func (self *CmdRequest[T]) BulkUpsert() *basedto.IchubResult {
    return self.EsWebClient.BulkUpsert(self.CmdRequest)
}

// 参数名=字段名
func (self *CmdRequest[T]) UpdateByQueryParam(fieldAsParams map[string]any) *basedto.IchubResult {
    var script = self.UpdataParam2Script(fieldAsParams)
    return self.UpdateByQuery(script, fieldAsParams)
}
func (self *CmdRequest[T]) UpdateByQuery(updateClause string, fieldParams map[string]any) *basedto.IchubResult {
    if len(fieldParams) == 0 {
       golog.Info("no params")
       return nil
    }
    var cmd = self.CmdRequest
    cmd.SetIndexTable(self.IndexTable())
    cmd.ScriptAs(updateClause, fieldParams)
    cmd.CmdType = esconst.ES_CMD_TYPE_UPDATEBYQUERY
    var result = self.EsWebClient.Cmd(cmd)
    if !result.IsSuccess() {
       golog.Error("[UpdateByQuery] ", result)
    }
    return result
}
func (self *CmdRequest[T]) Update(ids any, doc map[string]any) *basedto.IchubResult {
    var id = gconv.String(ids)
    var cmd = self.cms2CmdReq(self.IndexTable(), id, doc)
    cmd.SetIndexTable(self.IndexTable())
    cmd.CmdType = esconst.ES_CMD_TYPE_UPDATE
    var result = self.EsWebClient.Cmd(cmd)
    if !result.IsSuccess() {
       golog.Error("[Update] ", result)
    }

    return result
}
func (self *CmdRequest[T]) Update2Err(id string, doc map[string]any) error {
    var result = self.Update(id, doc)
    if result.IsSuccess() {
       return nil
    }
    golog.Error("Update", result)
    return errors.New(result.Msg)
}

func (self *CmdRequest[T]) DeleteByQuery() *basedto.IchubResult {
    return self.EsWebClient.DeleteByQuery(self.CmdRequest)
}
func (self *CmdRequest[T]) Delete(id any) *basedto.IchubResult {
    return self.EsWebClient.Delete(self.IndexTable(), id)
}
func (self *CmdRequest[T]) BulkDelete(ids ...any) *basedto.IchubResult {
    return self.EsWebClient.BulkDelete(self.IndexTable(), ids...)
}

元数据
package eswebrequest

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goweb/pagereq"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient"

    "reflect"
)

type MetaRequest[T eswebclient.IndexMapping] struct {
    *pagereq.MetadataRequest
    EsWebClient *eswebclient.EsWebClient
    index       eswebclient.IndexMapping
}

func (self *MetaRequest[T]) Index() eswebclient.IndexMapping {
    return self.index
}

func (self *MetaRequest[T]) SetIndex(index eswebclient.IndexMapping) {
    self.index = index
}

func NewOfIndexType[T eswebclient.IndexMapping]() T {
    var t T
    var typeOf = reflect.TypeOf(t)
    if typeOf.Kind() == reflect.Ptr {
       typeOf = typeOf.Elem()
    }
    var value = reflect.New(typeOf)
    return value.Interface().(T)
}
func DefaultMeta[T eswebclient.IndexMapping]() *MetaRequest[T] {
    var q = &MetaRequest[T]{
       MetadataRequest: pagereq.DefaultMetadata(),
       EsWebClient:     eswebclient.FindBeanEsWebClient(),
    }
    q.index = NewOfIndexType[T]()
    q.SetIndexTable(q.index)
    return q
}
func (self *MetaRequest[T]) MetaDropIndex() *basedto.IchubResult {
    return self.EsWebClient.MetaDropIndex(self.IndexTable())
}
func (self *MetaRequest[T]) MetaCreateIndex() *basedto.IchubResult {
    return self.EsWebClient.MetaCreateIndex(self.index)
}
func (self *MetaRequest[T]) MetaIndexExists() *basedto.IchubResult {
    return self.EsWebClient.MetaIndexExists(self.IndexTable())
}

func (self *MetaRequest[T]) CreateIndexIfNotExist() *basedto.IchubResult {
    return self.EsWebClient.CreateIndexIfNotExist(self.index)
}
func (self *MetaRequest[T]) CreateIndexesIfNotExist() *basedto.IchubResult {
    return self.EsWebClient.CreateIndexesIfNotExist(self.index)
}
func (self *MetaRequest[T]) MetaGetMapping() *basedto.IchubResult {
    return self.EsWebClient.MetaGetMapping(self.index)
}
标签: 1024程序员节

本文转载自: https://blog.csdn.net/leijmdas/article/details/143212029
版权归原作者 leijmdas 所有, 如有侵权,请联系我们删除。

“Go实战全家桶之十三:go esWebClient基于泛型的通用ES客户端eswebrequest封装”的评论:

还没有评论