liuxiaolong
2019-07-20 0c37c84b7a870c159cc03e445dadc3772affbb7d
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
package controllers
 
import (
    "basic.com/dbapi.git"
    "fmt"
    "strconv"
    "strings"
 
    "github.com/gin-gonic/gin"
    "webserver/extend/code"
    "webserver/extend/config"
    "webserver/extend/esutil"
    "webserver/extend/util"
)
 
type EsSearchController struct{}
 
// @Summary 检索
// @Description 信息检索和比对检索
// @Accept  json
// @Produce json
// @Tags es
// @Param obj body models.EsSearch true "底库数据"
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false}"
// @Router /data/api-v/es/esSearch [POST]
func (sc *EsSearchController) PostEsSearch(c *gin.Context) {
    searchBody := make(map[string]interface{}, 0)
 
    c.BindJSON(&searchBody)
 
    data := findEsData(searchBody)
    util.ResponseFormat(c, code.Success, data)
}
 
func findEsData(searchBody map[string]interface{}) map[string]interface{} {
    webPage := int(searchBody["page"].(float64))
 
    webSize := int(searchBody["size"].(float64))
    from := strconv.Itoa((webPage - 1) * webSize)
    //esFrom := strconv.Itoa(from)
    //esSize := strconv.Itoa(webSize)
    size := strconv.Itoa(webPage * webSize)
    //请求索引
    index := config.EsInfo.EsIndex.VideoPersons.IndexName + "," + config.EsInfo.EsIndex.Personaction.IndexName
    queryStr := ""
    queryBody := searchBody["inputValue"].(string)
    //检索框
    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"].([]interface{})[0].(string)
    lteDate := searchBody["searchTime"].([]interface{})[1].(string)
    //判断任务ID
    taskIdStr := ""
    taskId := searchBody["tasks"].([]interface{})
    if taskId != nil && len(taskId) > 0 {
        esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1)
        taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}},"
    }
    //判断摄像机ID
    cameraIdStr := ""
    cameraId := searchBody["treeNodes"].([]interface{})
    if cameraId != nil && len(cameraId) > 0 {
        esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1)
        cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}},"
    }
 
    //判断库表ID
    tableId := searchBody["tabs"].([]interface{})
    esTableId := ""
    esTableIdStr := ""
    if tableId != nil && len(tableId) > 0 {
        esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
        index = config.EsInfo.EsIndex.VideoPersons.IndexName
        esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
    }
    isCollectStr := ""
    isCollect := searchBody["collection"].(string)
    if isCollect != "" {
        isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
    }
 
    //使用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\":\"" + from + "\"," +
        "\"size\":\"" + size + "\"," +
        "\"query\":{\"bool\":{" + queryStr +
        "\"filter\":[" +
        cameraIdStr +
        taskIdStr +
        isCollectStr +
        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
}
 
//sdkTYype字典"\"_source\":[\"BaseName\",\"Gender\",\"Race\",\"content\",\"idcard\",\"picAddress\",\"picDate\",\"sdkType\",\"Age\",\"personId\",\"personIsHub\",\"personPicUrl\",\"picLocalUrl\",\"picSmUrl\",\"videoIp\",\"videoNum\",\"cameraId\",\"ageDescription\",\"likePer\"]" +
//func sdkTypeToValue(i int) string {
//    value := []string{"人脸", "车辆", "人体", "入侵", "拥挤", "靠右行", "人员异常", "个体静止"}
//    return value[i-1]
//}
 
//获取Dbtablepersons信息
/*func getDBPersonInfo(personId string) map[string]string {
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_search"
 
    prama := "{\"query\":{\"term\":{\"uuid\":\"" + personId + "\"}},\"_source\":[\"personName\",\"phoneNum\"]}"
 
    tokenRes := esutil.GetEsDataReq(url, prama, true)
    personRes := make(map[string]string)
    //fmt.Println(tokenRes["datalist"].([]interface{}))
    result := tokenRes["datalist"].([]interface{})
    for _, value := range result {
        userMap := value.(map[string]interface{})
        if personName, ok := userMap["personName"]; ok {
            personRes["personName"] = personName.(string)
        } else {
            personRes["personName"] = ""
        }
        if phoneNum, ok := userMap["phoneNum"]; ok {
            personRes["phoneNum"] = phoneNum.(string)
        } else {
            personRes["phoneNum"] = ""
        }
        if sex, ok := userMap["sex"]; ok {
            personRes["sex"] = sex.(string)
        } else {
            personRes["sex"] = ""
        }
        // personRes["phoneNum"] = value.(map[string]interface{})["phoneNum"].(string)
        // personRes["sex"] = value.(map[string]interface{})["sex"].(string)
    }
    //fmt.Println(personRes)
    if len(personRes) < 1 {
        personRes["personName"] = ""
        personRes["phoneNum"] = ""
        personRes["sex"] = ""
    }
    return personRes
}*/