代码拉取完成,页面将自动刷新
package es
import (
"bytes"
"context"
"encoding/json"
"fmt"
"gitee.com/kzangv/gsf-fof/logger"
"net/http"
"time"
)
func OperationApiLog(c *Client, op, idx, body string) func() {
bTm := time.Now()
return func() {
spendTime := time.Now().Sub(bTm) / time.Millisecond
if spendTime > time.Duration(c.ref.Cfg.SlowThreshold) { // 查询超过阈值
c.Log.Warn(" ES[%d ms]: %s/%s -- %s", spendTime, idx, op, body)
} else if c.Log.Level() >= logger.Info {
c.Log.Info(" ES[%d ms]: %s/%s -- %s", spendTime, idx, op, body)
}
}
}
type Operation struct {
val *Client
ApiLog func(c *Client, op, idx, body string) func()
}
func NewOperation(c *Client) *Operation {
return &Operation{val: c, ApiLog: OperationApiLog}
}
func (c *Operation) Search(idx string, body string, receive interface{}, ctx context.Context) (cnt uint64, err error) {
reqData := &bytes.Buffer{}
reqData.WriteString(body)
defer c.ApiLog(c.val, "Search", idx, body)()
resp, err := c.val.Client.Search(c.val.Client.Search.WithIndex(idx), c.val.Client.Search.WithBody(reqData), c.val.Client.Search.WithContext(ctx))
if err == nil {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
data := RespSearch{}
data.Hits.Hits = receive
if err = json.NewDecoder(resp.Body).Decode(&data); err == nil {
cnt = data.Hits.Total.Value
}
} else {
raw := RespError{}
if err = json.NewDecoder(resp.Body).Decode(&raw); err == nil {
err = fmt.Errorf(raw.Error.Reason)
}
}
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。