From 6f023031f3f6f08ac441189be8b16838145edd3e Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期二, 14 一月 2020 15:31:27 +0800 Subject: [PATCH] add tools --- EsClient.go | 67 +++++++++++++++++++++++++++++++++ 1 files changed, 67 insertions(+), 0 deletions(-) diff --git a/EsClient.go b/EsClient.go index 0d608a9..bbdb4bb 100644 --- a/EsClient.go +++ b/EsClient.go @@ -542,6 +542,73 @@ return data, nil } +func SourceCreated(buf []byte) (result bool, err error) { + var info interface{} + json.Unmarshal(buf, &info) + out, ok := info.(map[string]interface{}) + if !ok { + return false, errors.New("http response interface can not change map[string]interface{}") + } + + middle, ok := out["result"].(string) + if !ok { + return false, errors.New("first total change error!") + } + if middle == "created" || middle == "updated" { + result = true + } + return result, nil +} + +func SourceDeleted(buf []byte) (total int, err error) { + var info interface{} + json.Unmarshal(buf, &info) + out, ok := info.(map[string]interface{}) + if !ok { + return -1, errors.New("http response interface can not change map[string]interface{}") + } + + middle, ok := out["deleted"].(float64) + if !ok { + return -1, errors.New("first total change error!") + } + total = int(middle) + return total, nil +} + +func SourceUpdated(buf []byte) (total int, err error) { + var info interface{} + json.Unmarshal(buf, &info) + out, ok := info.(map[string]interface{}) + if !ok { + return -1, errors.New("http response interface can not change map[string]interface{}") + } + + middle, ok := out["updated"].(float64) + if !ok { + return -1, errors.New("first total change error!") + } + total = int(middle) + return total, nil +} + +func SourceTotal(buf []byte) (total int, err error) { + var info interface{} + json.Unmarshal(buf, &info) + out, ok := info.(map[string]interface{}) + if !ok { + return -1, errors.New("http response interface can not change map[string]interface{}") + } + + middle, ok := out["hits"].(map[string]interface{}) + if !ok { + return -1, errors.New("first total change error!") + } + tmp := middle["total"].(float64) + total = int(tmp) + return total, nil +} + func EsReq(method string, url string, parama []byte) (buf []byte, err error) { //defer elapsed("page")() timeout := time.Duration(10 * time.Second) -- Gitblit v1.8.0