开源
goweb: https://gitee.com/ichub/goweb/settings#index
需求
UML
代码位置
测试用例:
func (this *TestPageEsRequestSuite) Test002_NestBoolQuery() { var req1 = Default() req1.EsShould().EsMatch("dept_name", "olivere") req1.EsTerm("dept_id", "570362815982862336") req1.EsMust().EsTerm("dept_id", "570362815982862336") var req = Default() req.EsMust().EsMatch("dept_name", "olivere") req.Nest(req1, req1) cmdReq.IndexName = "ichub_sys_dept" cmdReq.EsMatch("dept_name", "olivere") cmdReq.Source = "dept_id,dept_name" cmdReq.Nest(req) result, _ := cmdReq.Nest(req, req).EsBoolQueryResult() this.Equal(200, result.Code) logrus.Info(cmdReq, result) }
结果:
{
"code": 200,
"msg": "成功",
"page_size": 2,
"current": 1,
"total": 1,
"data": [
{
"dept_name": "olivere",
"dept_id": "570362815982862336"
}
]
}
请求:
{
"page_size": 2,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
}
],
"es_bool_type": 3,
"cmd_category": 7,
"index_alias": "",
"index_name": "ichub_sys_dept",
"cmd_type": 0,
"source": "dept_id,dept_name",
"id": "",
"ids": null,
"PageEsRequests": [
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [],
"PageEsBoolTypes": []
},
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [],
"PageEsBoolTypes": []
}
],
"PageEsBoolTypes": [
0,
0
]
},
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [],
"PageEsBoolTypes": []
},
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [],
"PageEsBoolTypes": []
}
],
"PageEsBoolTypes": [
0,
0
]
},
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [],
"PageEsBoolTypes": []
},
{
"page_size": 20,
"current": 1,
"order_by": [],
"fields": [
{
"field": "dept_name",
"op_type": "match",
"values": [
"olivere"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
},
{
"field": "dept_id",
"op_type": "term",
"values": [
"570362815982862336"
]
}
],
"es_bool_type": 0,
"cmd_category": 7,
"index_alias": "",
"index_name": "",
"cmd_type": 0,
"source": "",
"id": "",
"ids": null,
"PageEsRequests": [],
"PageEsBoolTypes": []
}
],
"PageEsBoolTypes": [
0,
0
]
}
],
"PageEsBoolTypes": [
3,
3,
3
]
}
增删改
package pagereq import ( "gitee.com/ichub/goconfig/common/base/baseutils/jsonutils" "gitee.com/ichub/goweb/common/pagemsg/pagees" "gitee.com/ichub/goweb/common/pagemsg/pagees/esconst" "github.com/sirupsen/logrus" "github.com/stretchr/testify/suite" "testing" "time" ) type TestCmdRequest struct { suite.Suite cmdRequest *CmdRequest indexName string QueryRequest *QueryRequest } const Indexname = "ichub_sys_dept" func TestCmdRequests(t *testing.T) { suite.Run(t, new(TestCmdRequest)) } func (this *TestCmdRequest) SetupTest() { logrus.Info("SetupTest") this.cmdRequest = NewCmdRequest() this.QueryRequest = NewQueryRequest() this.cmdRequest.IndexName = Indexname //"ichub_sys_dept" } func (this *TestCmdRequest) Test001_Delete() { this.cmdRequest.Id = "999" var re, e = this.cmdRequest.Delete() logrus.Info(jsonutils.ToJsonPretty(re), e) } func (this *TestCmdRequest) Test002_DeleteByQuery() { this.cmdRequest.EsFilter().EsTerm("dept_id", "1222") ret, err := this.cmdRequest.DeleteByQuery() logrus.Info(ret, err) } func (this *TestCmdRequest) Test003_BulkDelete() { this.cmdRequest.Ids = []string{"999"} var res, e = this.cmdRequest.BulkDelete() logrus.Info(jsonutils.ToJsonPretty(res), e) } func (this *TestCmdRequest) Test004_Update() { const creaby = "creaUpdate1" this.cmdRequest.Id = "101" this.cmdRequest.Doc = map[string]interface{}{"create_by": creaby} var res, e = this.cmdRequest.Update() logrus.Info(jsonutils.ToJsonPretty(res), e) var r, err = this.Query(this.cmdRequest.Id) logrus.Info(r, err) this.Equal(1, len(r.DataAsArrays())) this.Equal(creaby, r.DataAsArrays()[0]["create_by"].(string)) } func (this *TestCmdRequest) Test004_UpdateQuery() { const creaby = "Test004_UpdateQuery" this.cmdRequest.Id = "101" this.cmdRequest.EsTerm("dept_id", "101") var ps = map[string]interface{}{"create_by": creaby} this.cmdRequest.ScriptAs(`ctx._source.create_by = params.create_by`, ps) var res, err = this.cmdRequest.UpdateByQuery() logrus.Info(jsonutils.ToJsonPretty(res), err) var r, _ = this.Query(this.cmdRequest.Id) this.Equal(1, len(r.DataAsArrays())) this.Equal(creaby, r.DataAsArrays()[0]["create_by"].(string)) logrus.Info("cmdreq=", this.cmdRequest.ToPrettyString()) } func (this *TestCmdRequest) Test005_BulkUpdate() { const creaby = "bulkUpdate0001" const deptid = "101" var doc = map[string]interface{}{"create_by": creaby} this.cmdRequest.AddDoc(deptid, doc) var res, e = this.cmdRequest.BulkUpdate() logrus.Info(jsonutils.ToJsonPretty(res), e) if e != nil { logrus.Error(e) return } time.Sleep(time.Second) var r, err = this.Query(deptid) logrus.Info(r, err) this.Equal(true, err == nil) this.Equal(1, len(r.DataAsArrays())) this.Equal(creaby, r.DataAsArrays()[0]["create_by"].(string)) } func (this *TestCmdRequest) Test006_Upsert() { const creaby = "creaUpsert6" this.cmdRequest.Id = "101" this.cmdRequest.Doc = map[string]interface{}{"create_by": creaby} var res, err = this.cmdRequest.Upsert() logrus.Info(jsonutils.ToJsonPretty(res), err) time.Sleep(time.Second) var r, _ = this.Query(this.cmdRequest.Id) this.Equal(1, len(r.DataAsArrays())) this.Equal(creaby, r.DataAsArrays()[0]["create_by"].(string)) } func (this *TestCmdRequest) Test007_DocAsUpsert() { const id = "282828" const creaby = "creaDocAsUpsert7" this.cmdRequest.Id = id this.cmdRequest.Doc = map[string]interface{}{ "create_by": creaby, "dept_id": id, } //var res, err = this.cmdRequest.DocAsUpsert() this.cmdRequest.CmdType = esconst.ES_CMD_TYPE_DOCASUPSERT var res = this.cmdRequest.Exceute() logrus.Info(jsonutils.ToJsonPretty(res)) this.Equal(200, res.Code) time.Sleep(time.Second) var r, _ = this.Query(id) this.Equal(1, len(r.DataAsArrays())) this.Equal(creaby, r.DataAsArrays()[0]["create_by"].(string)) } func (this *TestCmdRequest) Query(id string) (*pagees.PageEsResult, error) { var queryRequest = this.QueryRequest queryRequest.PageSize = 2 queryRequest.IndexName = Indexname queryRequest.EsTerm("dept_id", id) var res, err = queryRequest.EsQueryResult() logrus.Info(jsonutils.ToJsonPretty(res), err) return res, err } func (this *TestCmdRequest) Test008_query() { this.QueryRequest.EsTerm("dept_id", 101) logrus.Info(this.QueryRequest) this.cmdRequest.EsTerm("dept_id", 101) this.cmdRequest.CmdType = esconst.ES_CMD_TYPE_UPDATE //logrus.Info(jsonutils.ToJsonPretty(this.cmdRequest)) logrus.Error("cmd=", this.cmdRequest) }标签:name,cmdRequest,查询,dept,API,Go,type,id,op From: https://blog.csdn.net/leijmdas/article/details/139161514