diff --git a/demo.api b/demo.api index 2f0814e..a16b67b 100644 --- a/demo.api +++ b/demo.api @@ -11,6 +11,15 @@ type EsQueryName { Size int32 `json:"size"` } +type EsProQueryName { + Name string `json:"name"` + Code string `json:"code"` + LotType string `json:"lotType"` + Year string `json:"year"` + Page int32 `json:"page"` + Size int32 `json:"size"` +} + // 通用api返回参数 type Response { Success bool `json:"success,default=true"` // 是否成功 @@ -31,12 +40,13 @@ service demo-api { ) @handler EsPressureTestHandler post /es/pressureTest(EsQueryName) returns (Response) + @doc( - summary: "es 业务压测 demo", + summary: "es pro 正式环境", ) - @handler EsBusinessPressureTestHandler - post /es/businessPressureTest returns (Response) + @handler EsProSearchDistrictHandler + post /es/pro/searchDistrict(EsProQueryName) returns (Response) @doc( summary: "测试返回503", diff --git a/internal/handler/esprosearchdistricthandler.go b/internal/handler/esprosearchdistricthandler.go new file mode 100644 index 0000000..2b7c22e --- /dev/null +++ b/internal/handler/esprosearchdistricthandler.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 EsProSearchDistrictHandler(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.NewEsProSearchDistrictLogic(r.Context(), svcCtx) + resp, err := l.EsProSearchDistrict(&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 a6b6f28..01700ee 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -24,8 +24,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodPost, - Path: "/es/businessPressureTest", - Handler: EsBusinessPressureTestHandler(serverCtx), + Path: "/es/pro/searchDistrict", + Handler: EsProSearchDistrictHandler(serverCtx), }, { Method: http.MethodGet, diff --git a/internal/logic/espressuretestlogic.go b/internal/logic/espressuretestlogic.go index ff15887..ae79b9d 100644 --- a/internal/logic/espressuretestlogic.go +++ b/internal/logic/espressuretestlogic.go @@ -6,7 +6,7 @@ import ( "demo/internal/svc" "demo/internal/types" - "demo/model/es" + es "demo/model/es_dev" "github.com/zeromicro/go-zero/core/logx" ) diff --git a/internal/logic/esprosearchdistrictlogic.go b/internal/logic/esprosearchdistrictlogic.go new file mode 100644 index 0000000..5a662f5 --- /dev/null +++ b/internal/logic/esprosearchdistrictlogic.go @@ -0,0 +1,72 @@ +package logic + +import ( + "context" + "fmt" + + "demo/internal/svc" + "demo/internal/types" + es "demo/model/es_pro" + + "github.com/zeromicro/go-zero/core/logx" +) + +type EsProSearchDistrictLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewEsProSearchDistrictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EsProSearchDistrictLogic { + return &EsProSearchDistrictLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *EsProSearchDistrictLogic) EsProSearchDistrict(req *types.EsProQueryName) (resp *types.Response, err error) { + + esQuery := ` + { + "query": { + "bool": { + "must": [ + { + "match_phrase": { + "districtName": "%s" + } + }, + { + "match_phrase": { + "year": "%s" + } + }, + { + "match_phrase": { + "lotType": "%s" + } + }, + { + "match_phrase": { + "districtCode": "%s" + } + } + ] + } + } + } + ` + + esQueryStr := fmt.Sprintf(esQuery, req.Name, req.Year, req.LotType, req.Code) + doc, err := es.EsServiceApp.EsSearchDocWithStr(l.ctx, esQueryStr, int(req.Page), int(req.Size)) + if err != nil { + return nil, err + } + + return &types.Response{ + Success: true, + Message: "success", + Result: doc, + }, nil +} diff --git a/internal/types/types.go b/internal/types/types.go index 4ff0586..25b4a64 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -11,6 +11,15 @@ type EsQueryName struct { Size int32 `json:"size"` } +type EsProQueryName struct { + Name string `json:"name"` + Code string `json:"code"` + LotType string `json:"lotType"` + Year string `json:"year"` + Page int32 `json:"page"` + Size int32 `json:"size"` +} + type Response struct { Success bool `json:"success,default=true"` // 是否成功 Message string `json:"message,optional"` // 消息 diff --git a/model/es/esmodel.go b/model/es_dev/esmodel.go similarity index 100% rename from model/es/esmodel.go rename to model/es_dev/esmodel.go diff --git a/model/es_pro/thoroughly_district_prod_91_model.go b/model/es_pro/thoroughly_district_prod_91_model.go new file mode 100644 index 0000000..af235ef --- /dev/null +++ b/model/es_pro/thoroughly_district_prod_91_model.go @@ -0,0 +1,144 @@ +package es + +import ( + "bytes" + "context" + "encoding/json" + + esclient "github.com/elastic/go-elasticsearch/v7" + jsoniter "github.com/json-iterator/go" +) + +type ( + defaultEsService interface { + EsConnect() *esclient.Client + EsCreateIndex() + EsDeleteIndex() + EsAddDoc() + EsDeleteDoc() + EsUpdateDoc() + EsSearchDoc(index string, query map[string]interface{}, from int, size int) interface{} + } + EsService struct { + } +) + +var ( + EsServiceApp = new(EsService) + logIndex = "thoroughly_district_prod_91" +) + +func (e *EsService) EsConnect() *esclient.Client { + cfg := esclient.Config{ + Addresses: []string{ + "http://172.16.102.18:9200", + "http://172.16.102.59:9200", + "http://172.16.102.60:9200", + // "http://xxx.xxx.xxx.xxx:9200", + }, + Username: "elastic", + Password: "QWEasd#2022#zsbm", + } + es, err := esclient.NewClient(cfg) + if err != nil { + panic(err) + } + return es +} + +// 创建索引 +func (e *EsService) EsCreateIndex() { + +} + +// 删除索引 +func (e *EsService) EsDeleteIndex() { + +} + +// 添加文档 +func (e *EsService) EsAddDoc() { + +} + +// 删除文档 +func (e *EsService) EsDeleteDoc() { + +} + +// 更新文档 +func (e *EsService) EsUpdateDoc() { + +} + +// 查询文档- map[string]interface{} +func (e *EsService) EsSearchDoc(ctx context.Context, query map[string]interface{}, from int, size int) (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) + return nil, err + } + // Perform the search request. + res, err := es.Search( + es.Search.WithContext(context.Background()), + es.Search.WithIndex(logIndex), + 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 +} + +// 查询文档- muti string +func (e *EsService) EsSearchDocWithStr(ctx context.Context, query string, from int, size int) (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(logIndex), + 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 +}