sunty
2019-10-18 6b27a131645082a26f8473349f70f62328b8c170
add GetEsDataReq
1个文件已修改
46 ■■■■■ 已修改文件
EsClient.go 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
EsClient.go
@@ -5,14 +5,58 @@
    "encoding/json"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "strconv"
    "time"
    "strings"
    "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
    }
    // 是否需要 解析 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