---
panlei
2019-08-06 870cdf6b042a3a8848afac02fc47181b23d13094
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package insertdata
 
import (
    "encoding/json"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "ruleprocess/logger"
    "strconv"
    "strings"
    "time"
    "bytes"
)
 
type Reps struct {
    FaceFeature string `json:"faceFeature"`
    ID          string
}
 
// 获取es 数据结构
func GetEsDataReq(url string, parama string) ([]Reps, error) {
 
    var reps Reps
    var repss []Reps
    fmt.Println("es 查询请求路径" + url) //  配置信息 获取
    req, err := http.NewRequest("POST", url, strings.NewReader(parama))
 
    if err != nil {
        fmt.Println("build request error! ")
        return nil, err
    }
 
    req.Header.Add("Content-Type", "application/json")
    timeout := time.Duration(100 * time.Second)
    client := &http.Client{Timeout: timeout}
    resp, err := client.Do(req)
 
    if err != nil {
        return nil, err
    }
 
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
 
    jsonStr := string(body)
    var dat map[string]interface{}
    dec := json.NewDecoder(strings.NewReader(jsonStr))
 
    if err := dec.Decode(&dat); err == io.EOF {
        fmt.Println(err.Error())
        return nil, err
    } else if err != nil {
        fmt.Println(err.Error())
        return nil, err
    }
    dat, ok := dat["hits"].(map[string]interface{})
    if !ok {
        return nil, errors.New("data is not type of  map[string]interface{}")
    }
 
    for _, value := range dat["hits"].([]interface{}) {
        source, ok := value.(map[string]interface{})["_source"].(map[string]interface{})
        if !ok {
            return nil, errors.New("value is not type of map[string]interface{}")
        }
        reps.FaceFeature, ok = source["faceFeature"].(string)
        if !ok {
            fmt.Println("faceFeature is not string", source["faceFeature"])
            continue
        }
        reps.ID, ok = source["ID"].(string)
        if !ok {
            fmt.Println("ID is not string", source["ID"])
            continue
        }
 
        repss = append(repss, reps)
    }
    return repss, nil
 
}
 
func PostAction(querynum int, queryindex int) ([]Reps, error) {
    defer elapsed("page")()
    number := strconv.Itoa(querynum)
    point := strconv.Itoa(queryindex)
    url := "http://192.168.1.182:9200/videopersons/_search"
    parma := `{
       "from":` + point + `,
       "query": {
           "match_all": {}
       },
       "sort": [
       {
           "picDate": {
               "order": "desc"
           }
       }
       ],
       "size":` + number + `,
       "_source": [
       "faceFeature",
       "ID"
       ]
   }`
 
    repss, err := GetEsDataReq(url, parma)
    return repss, err
}
 
/** 插入es:
***  url: http://192.168.1.203:9200/personaction/perVideoAction/c58dbddf-4580-43f3-b76a-3213ec809df2?pretty
**param: // json [json slice] 
*/
 
/** 添加或者删除收藏
*** url: http://192.168.1.203:9200/videopersons/perVideoPicture/_id/_update
    param: []byte 
    json: {
        "doc": {
            "collection": "1" // 1 is 收藏 0 取消收藏
        }
        "detect_noop": false
    }
**/
 
func EsReq(method string, url string, parama []byte) (maps map[string]interface{},err error) {
    timeout := time.Duration(10 * time.Second) 
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama)) 
    request.Header.Set("Content-type", "application/json")
 
    if err != nil {
        fmt.Println("build request fail !")
        return nil, err
    }
 
    resp, err := client.Do(request)
    if err != nil{
        fmt.Println("request error: ", err)
        return nil,err
    }
 
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err) 
    }
    decoder := make(map[string]interface{})
    if err := json.Unmarshal([]byte(string(body)), &decoder); err != nil {
        return nil, err
    }
    fmt.Println(string(body))
    logger.Info(string(body))
    return decoder ,nil
}
 
 
func elapsed(what string) func() {
    start := time.Now()
    return func() {
        fmt.Printf("%s took %v\n", what, time.Since(start))
    }
}