Browse Source

正式查询

master
dark 1 year ago
parent
commit
e10fe22c6f
  1. 6
      demo.api
  2. 1
      internal/handler/demohandler.go
  3. 28
      internal/handler/esprofromjavasearchdistricthandler.go
  4. 5
      internal/handler/routes.go
  5. 132
      internal/logic/esprofromjavasearchdistrictlogic.go
  6. 37
      model/es_dev/esmodel.go
  7. 37
      model/es_pro/thoroughly_district_prod_91_model.go
  8. 17
      readme.md

6
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",
)

1
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"
)

28
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)
}
}
}

5
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",

132
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
}

37
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
}

37
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
}

17
readme.md

@ -4,3 +4,20 @@ 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 .
## 编写api文件
```go
domo.api
```
## 运行工具,自动生成
```shell
goctl api go -api demo.api -dir .
```
## 编写逻辑 /internal/logic
## go run demo.go
## 打包
```shell
./build.bat
```
Loading…
Cancel
Save