From 6237d3adfa151dbc034ae1ac46d06fff888e3494 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期五, 18 十月 2019 11:13:37 +0800
Subject: [PATCH] add GetEsDataReq

---
 EsClient.go |   48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 47 insertions(+), 1 deletions(-)

diff --git a/EsClient.go b/EsClient.go
index 8b2afb9..1f2d4bc 100644
--- a/EsClient.go
+++ b/EsClient.go
@@ -5,14 +5,60 @@
     "encoding/json"
     "errors"
     "fmt"
+    "io"
     "io/ioutil"
     "net/http"
 	"strconv"
-	"time"
+    "strings"
+    "time"
 
     "basic.com/pubsub/protomsg.git"
 )
 
+func GetEsDataReq(url string, parama string, isSource bool) map[string]interface{} {
+    //fmt.Println("es 鏌ヨ璇锋眰璺緞" + url) //  閰嶇疆淇℃伅 鑾峰彇
+    var dat map[string]interface{}
+    req, err := http.NewRequest("POST", url, strings.NewReader(parama))
+    req.Header.Add("Content-Type", "application/json")
+    timeout := time.Duration(10 * time.Second) //瓒呮椂鏃堕棿50ms
+    client := &http.Client{Timeout: timeout}
+    resp, err := client.Do(req)
+    if err != nil {
+        fmt.Println(err)
+        return dat
+    }
+    defer resp.Body.Close()
+    dec := json.NewDecoder(resp.Body)
+    if err := dec.Decode(&dat); err == io.EOF {
+        fmt.Println(err.Error())
+        return dat
+    } else if err != nil {
+        fmt.Println(err.Error())
+        return dat
+    }
+    // 鏄惁闇�瑕� 瑙f瀽 es 杩斿洖鐨� source
+    if isSource {
+        dat = dat["hits"].(map[string]interface{})
+        var data = make(map[string]interface{}, 2)
+        data["total"] = dat["total"]
+        sources := []interface{}{}
+        for _, value := range dat["hits"].([]interface{}) {
+            source := value.(map[string]interface{})["_source"].(map[string]interface{})
+            //source["id"] = source["id"]
+            /*sdkType := source["sdkType"]
+			if sdkType != nil {
+				sdk, _ := strconv.Atoi(sdkType.(string))
+				source["sdkType"] = sdkTypeToValue(sdk)
+			}*/
+            sources = append(sources, source)
+        }
+        data["datalist"] = sources
+        return data
+    } else {
+        return dat
+    }
+
+}
 
 func Parsesources(sources []map[string]interface{}) (esinfos []*protomsg.Esinfo) {
     var ok bool

--
Gitblit v1.8.0