From e10fe22c6fc7b8f7969433acd1e89b885a51c225 Mon Sep 17 00:00:00 2001 From: dark <255317@qq.com> Date: Thu, 14 Mar 2024 05:44:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=BC=8F=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo.api | 6 + internal/handler/demohandler.go | 1 + .../esprofromjavasearchdistricthandler.go | 28 ++++ internal/handler/routes.go | 5 + .../logic/esprofromjavasearchdistrictlogic.go | 132 ++++++++++++++++++ model/es_dev/esmodel.go | 37 +++++ .../thoroughly_district_prod_91_model.go | 37 +++++ readme.md | 19 ++- 8 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 internal/handler/esprofromjavasearchdistricthandler.go create mode 100644 internal/logic/esprofromjavasearchdistrictlogic.go diff --git a/demo.api b/demo.api index a16b67b..38c2823 100644 --- a/demo.api +++ b/demo.api @@ -48,6 +48,12 @@ service demo-api { @handler EsProSearchDistrictHandler post /es/pro/searchDistrict(EsProQueryName) returns (Response) + @doc( + summary: "es pro 正式环境 正式查询", + ) + @handler EsProFromJavaSearchDistrictHandler + post /es/pro/fromJavaSearchDistrict(EsProQueryName) returns (Response) + @doc( summary: "测试返回503", ) diff --git a/internal/handler/demohandler.go b/internal/handler/demohandler.go index 35b3732..5818f9f 100644 --- a/internal/handler/demohandler.go +++ b/internal/handler/demohandler.go @@ -6,6 +6,7 @@ import ( "demo/internal/logic" "demo/internal/svc" "demo/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" ) diff --git a/internal/handler/esprofromjavasearchdistricthandler.go b/internal/handler/esprofromjavasearchdistricthandler.go new file mode 100644 index 0000000..e01ec79 --- /dev/null +++ b/internal/handler/esprofromjavasearchdistricthandler.go @@ -0,0 +1,28 @@ +package handler + +import ( + "net/http" + + "demo/internal/logic" + "demo/internal/svc" + "demo/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func EsProFromJavaSearchDistrictHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.EsProQueryName + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := logic.NewEsProFromJavaSearchDistrictLogic(r.Context(), svcCtx) + resp, err := l.EsProFromJavaSearchDistrict(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 01700ee..958fa2f 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/es/pro/searchDistrict", Handler: EsProSearchDistrictHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/es/pro/fromJavaSearchDistrict", + Handler: EsProFromJavaSearchDistrictHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/test503", diff --git a/internal/logic/esprofromjavasearchdistrictlogic.go b/internal/logic/esprofromjavasearchdistrictlogic.go new file mode 100644 index 0000000..76be45f --- /dev/null +++ b/internal/logic/esprofromjavasearchdistrictlogic.go @@ -0,0 +1,132 @@ +package logic + +import ( + "context" + "fmt" + + "demo/internal/svc" + "demo/internal/types" + es "demo/model/es_dev" + + "github.com/zeromicro/go-zero/core/logx" +) + +type EsProFromJavaSearchDistrictLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewEsProFromJavaSearchDistrictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EsProFromJavaSearchDistrictLogic { + return &EsProFromJavaSearchDistrictLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *EsProFromJavaSearchDistrictLogic) EsProFromJavaSearchDistrict(req *types.EsProQueryName) (resp *types.Response, err error) { + // es 查询语句 + esQuery := ` + { + "query": { + "bool":{ + "must":[ + { + "bool": { + "must": [ + {"match": + { + "districtName": "%s" + } + }, + {"term": + { + "districtCode": "%s" + } + }, + {"term": + { + "year": "%s" + } + }, + {"term": + { + "lotType":"%s" + } + } + ], + "must_not": [ + { + "bool": { + "must":[ + { + "bool": { + "should":[ + {"term": + { + "schoolId":"" + } + }, + {"bool": + { + "mustNot": [ + { + "exists": { + "field": "schoolId" + } + } + ] + } + } + ] + } + }, + { + "bool": { + "should":[ + {"term": + { + "schoolName":"" + } + }, + {"bool": + { + "mustNot": [ + { + "exists": { + "field": "schoolName" + } + } + ] + } + } + ] + } + } + ] + } + } + ] + } + } + ] + } + } + } + ` + // es 查询 + esQueryStr := fmt.Sprintf(esQuery, req.Name, req.Code, req.Year, req.LotType) + doc, err := es.EsServiceApp.EsSearchDocWithStrAndIndex(l.ctx, esQueryStr, int(req.Page), int(req.Size), "thoroughly_districts_xw") + + if err != nil { + return nil, err + } + + return &types.Response{ + Success: true, + Message: "success", + Result: doc, + }, nil + +} diff --git a/model/es_dev/esmodel.go b/model/es_dev/esmodel.go index 976fe99..bbcc6fa 100644 --- a/model/es_dev/esmodel.go +++ b/model/es_dev/esmodel.go @@ -140,3 +140,40 @@ func (e *EsService) EsSearchDocWithStr(ctx context.Context, query string, from i } return result, nil } + +// 查询文档- muti string +func (e *EsService) EsSearchDocWithStrAndIndex(ctx context.Context, query string, from int, size int, index string) (doc interface{}, err error) { + es := e.EsConnect() + // Build the request body. + // var buf bytes.Buffer + // if err := json.NewEncoder(&buf).Encode(query); err != nil { + // httpresult.ErrorResult("参数解析错误", err) + // } + // Perform the search request. + // string query to io.Reader + // query = `{"query":{"match_all":{}}}` + var buf = bytes.NewBufferString(query) + res, err := es.Search( + es.Search.WithContext(context.Background()), + es.Search.WithIndex(index), + es.Search.WithBody(buf), + es.Search.WithTrackTotalHits(true), + es.Search.WithPretty(), + es.Search.WithFrom(from), + es.Search.WithSize(size), + // es.Search.WithSort("@timestamp:desc"), + ) + + if err != nil { + // httpresult.ErrorResult("查询失败", err) + return nil, err + } + defer res.Body.Close() + result := map[string]interface{}{} + jserr := jsoniter.NewDecoder(res.Body).Decode(&result) + if jserr != nil { + + return nil, jserr + } + return result, nil +} diff --git a/model/es_pro/thoroughly_district_prod_91_model.go b/model/es_pro/thoroughly_district_prod_91_model.go index af235ef..4f0050b 100644 --- a/model/es_pro/thoroughly_district_prod_91_model.go +++ b/model/es_pro/thoroughly_district_prod_91_model.go @@ -142,3 +142,40 @@ func (e *EsService) EsSearchDocWithStr(ctx context.Context, query string, from i } return result, nil } + +// 查询文档- muti string +func (e *EsService) EsSearchDocWithStrAndIndex(ctx context.Context, query string, from int, size int, index string) (doc interface{}, err error) { + es := e.EsConnect() + // Build the request body. + // var buf bytes.Buffer + // if err := json.NewEncoder(&buf).Encode(query); err != nil { + // httpresult.ErrorResult("参数解析错误", err) + // } + // Perform the search request. + // string query to io.Reader + // query = `{"query":{"match_all":{}}}` + var buf = bytes.NewBufferString(query) + res, err := es.Search( + es.Search.WithContext(context.Background()), + es.Search.WithIndex(index), + es.Search.WithBody(buf), + es.Search.WithTrackTotalHits(true), + es.Search.WithPretty(), + es.Search.WithFrom(from), + es.Search.WithSize(size), + // es.Search.WithSort("@timestamp:desc"), + ) + + if err != nil { + // httpresult.ErrorResult("查询失败", err) + return nil, err + } + defer res.Body.Close() + result := map[string]interface{}{} + jserr := jsoniter.NewDecoder(res.Body).Decode(&result) + if jserr != nil { + + return nil, jserr + } + return result, nil +} diff --git a/readme.md b/readme.md index 6f78b1a..0b63bc0 100644 --- a/readme.md +++ b/readme.md @@ -3,4 +3,21 @@ goctl model pg datasource --url="postgres://postgres:123456@10.0.1.36:5432/postg # 本机 goctl model pg datasource --url="postgres://postgres:123456@127.0.0.1:5432/postgres?sslmode=disable" --table="te_thoroughly_register" -dir ./model # api 生成 -goctl api go -api demo.api -dir . \ No newline at end of file +goctl api go -api demo.api -dir . +## 编写api文件 +```go +domo.api +``` +## 运行工具,自动生成 +```shell +goctl api go -api demo.api -dir . +``` + +## 编写逻辑 /internal/logic + +## go run demo.go + +## 打包 +```shell +./build.bat +``` \ No newline at end of file