8 changed files with 269 additions and 6 deletions
@ -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) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -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 |
||||
|
} |
@ -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 |
||||
|
} |
Loading…
Reference in new issue