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))
|
}
|
}
|