liuxiaolong
2019-09-18 711e90df27648aff82eda78d16dbbc159cceb625
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
package controllers
 
import (
    "basic.com/dbapi.git"
    "fmt"
    "github.com/gin-gonic/gin"
    "math/rand"
    "strconv"
    "strings"
    "webserver/extend/code"
    "webserver/extend/config"
    "webserver/extend/esutil"
    "webserver/extend/util"
    "webserver/models"
    "webserver/service"
)
 
type EsController struct{}
 
// @Summary 比对数据查询
// @Description  比对数据查询
// @Accept  json
// @Produce json
// @Tags es
// @Param reqMap body models.EsSearch true "collection 为空"
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
// @Router /data/api-v/es/queryEsCompareData [POST]
func PostEsCompareData(c *gin.Context) {
    searchBody := new(models.EsSearch)
    err := c.BindJSON(&searchBody)
    if err != nil || searchBody.PicUrl == "" || len(searchBody.DataBases) == 0 {
        util.ResponseFormat(c, code.RequestParamError, "参数有误")
        return
    }
    if searchBody.CompareNum !="" {
        //二次搜索,不需要再比对了
        co := service.GetCompResultByNum(searchBody.CompareNum)
        if co != nil {
            //二次搜索和排序
            twiceM := GetCompareDataTwice(co,searchBody)
            util.ResponseFormat(c,code.Success,twiceM)
            return
        } else {
            m :=make(map[string]interface{},0)
            m["compareNum"] = searchBody.CompareNum
            m["total"] = 0
            m["totalList"] = []CompareResult{}
            util.ResponseFormat(c,code.CompareResultGone,m)
            return
        }
    }
    m :=make(map[string]interface{},0)
    m["compareNum"] = searchBody.CompareNum
    m["total"] = 0
    m["totalList"] = []CompareResult{}
    util.ResponseFormat(c,code.CompareResultGone,m)
}
 
func searchEsData(searchBody models.EsSearch) map[string]interface{} {
    //请求索引
    index := config.EsInfo.EsIndex.VideoPersons.IndexName //  wp只查人脸数据
    queryStr := ""
    queryBody := searchBody.InputValue
    //检索框
    if queryBody != "" {
        queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"alarmRules.alarmLevel^1.5\",\"ageDescription^1.5\",\"taskName^1.5\",\"baseInfo.tableName^1.5\",\"sex^2.0\",\"race^2.0\",\"content^1.0\",\"baseInfo.idCard^1.8\",\"cameraAddr^1.0\"]," +
            "\"type\":\"cross_fields\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1}}],"
    }
    gteDate := searchBody.SearchTime[0]
    lteDate := searchBody.SearchTime[1]
    //判断任务ID
    taskIdStr := ""
    taskId := searchBody.Tasks
    if taskId != nil && len(taskId) > 0 {
        esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1)
        taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}},"
    }
    //判断摄像机ID
    cameraIdStr := ""
    cameraId := searchBody.TreeNodes
    if cameraId != nil && len(cameraId) > 0 {
        esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
        cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}},"
    }
    //判断库表ID
    tableId := searchBody.Tabs
    esTableId := ""
    esTableIdStr := ""
    if tableId != nil && len(tableId) > 0 {
        esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
        esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
    }
    isCollectionStr := ""
    isCollection := searchBody.Collection
    if isCollection != "" {
        isCollectionStr = "{\"term\":{\"collection\":\"" + isCollection + "\"}},"
    }
    webPage := searchBody.Page
    webSize := searchBody.Size
    from := (webPage - 1) * webSize
    esFrom := strconv.Itoa(from)
    esSize := strconv.Itoa(webSize)
    //使用es底层机制处理分页
    //请求头
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + index + "/_search?search_type=dfs_query_then_fetch"
 
    var setApi dbapi.SysSetApi
    _, sysconf := setApi.GetServerInfo()
    analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + sysconf.ServerId + "\"}},"
 
    //请求体
    prama := "{\"from\":\"" + esFrom + "\",\"size\":\"" + esSize + "\"," +
        //    prama := "{\"size\":\"0\"," +
        "\"query\":{\"bool\":{" + queryStr +
        "\"filter\":[" +
        cameraIdStr +
        taskIdStr +
        isCollectionStr +
        esTableIdStr +
        analyServerFilterStr +
        "{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," +
        "\"sort\":[{\"_score\":{\"order\":\"desc\"}},{\"picDate\":{\"order\":\"desc\"}}]," +
        "\"_source\":[\"baseInfo\",\"alarmRules\",\"sex\",\"analyServerName\",\"sdkName\",\"ageDescription\",\"content\",\"id\",\"cameraAddr\",\"picMaxUrl\",\"picDate\",\"race\",\"videoUrl\",\"picSmUrl\",\"taskName\",\"personIsHub\",\"isAlarm\",\"analyServerIp\",\"cameraId\"]}"
    fmt.Println(prama)
    //数据解析
    tokenRes := esutil.GetEsDataReq(url, prama, true)
    return tokenRes
}
 
func sourceCompare(sources []interface{}, isEsSource bool, campareByte []byte, threshold float32) []interface{} {
    var filterName = "feature"
    if isEsSource {
        filterName = "FaceFeature"
    }
    fmt.Println("查询" + filterName)
    dataSource := make([]interface{}, 0, 20)
    for _, obj := range sources {
        source := obj.(map[string]interface{})
        //feature := source[filterName].(string)                 //  linux
        //featByte, _ := base64.StdEncoding.DecodeString(feature)    //  linux
        //score := gosdk.FaceCompare(featByte, campareByte)            //  linux
        score := rand.Float32() // windows
        if score >= threshold*0.01 {
            source["score"] = int(score * 100)
            dataSource = append(dataSource, source)
        }
    }
    return dataSource
}