压测 dome
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

179 lines
4.1 KiB

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_districts_xw"
)
func (e *EsService) EsConnect() *esclient.Client {
cfg := esclient.Config{
Addresses: []string{
"http://10.1.1.124:9200",
// "http://xxx.xxx.xxx.xxx:9200",
},
// Username: "elastic",
// Password: "123456",
}
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
}
// 查询文档- 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
}