From f3863b12653fc47996073710b56e792de0be4c8d Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期二, 25 六月 2019 17:57:05 +0800 Subject: [PATCH] 根据新索引结构修改算法和接口 --- controllers/es.go | 104 +++++++++++++++++++++++++++++---------------------- 1 files changed, 59 insertions(+), 45 deletions(-) diff --git a/controllers/es.go b/controllers/es.go index 18fe7cd..f3dd501 100644 --- a/controllers/es.go +++ b/controllers/es.go @@ -4,15 +4,15 @@ "encoding/json" "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" - "math/rand" - "strconv" - "strings" ) type EsController struct{} @@ -31,15 +31,24 @@ c.BindJSON(&searchBody) isEsSource := true databases := searchBody.DataBases - page := searchBody.Page;if page <= 0 {page = 1} - size := searchBody.Size;if size <= 0 {size = 8} + page := searchBody.Page + if page <= 0 { + page = 1 + } + size := searchBody.Size + if size <= 0 { + size = 8 + } from := (page - 1) * size to := page * size - threshold := searchBody.Threshold;if threshold < 0 || threshold > 100 {threshold = 60} + threshold := searchBody.Threshold + if threshold < 0 || threshold > 100 { + threshold = 60 + } picUrl := searchBody.PicUrl /*feature := searchBody.Feature*/ - var featByte []byte; - if len(picUrl) > 3 { // linux + var featByte []byte + if len(picUrl) > 3 { // linux /*fileName := picUrl // picIp 瀹氫箟鍦� fileController weedfs 鏂囦欢鏈嶅姟鍣� 璁块棶 璺緞 鍓嶇紑 detect := gorun.GetSimpleFaceDetect(fileName) if len(detect) != 1 { @@ -76,25 +85,30 @@ if to > dataLen { to = dataLen } - sources = sources[from:to] // 鏁版嵁 閮ㄥ垎鑾峰彇 - if !isEsSource { // 浜哄憳鏁版嵁瑕佸姞搴曞簱鍚嶇О + sources = sources[from:to] // 鏁版嵁 閮ㄥ垎鑾峰彇 + if !isEsSource { // 浜哄憳鏁版嵁瑕佸姞搴曞簱鍚嶇О for _, sou := range sources { tableId := sou.(map[string]interface{})["tableId"] if tableId != nil { info := QueryDbTableInfo(tableId.(string)) - if info["tableName"] != nil {sou.(map[string]interface{})["tableName"] = info["tableName"] - }else {sou.(map[string]interface{})["tableName"] = "鍏朵粬"} - if info["bwType"] != nil {sou.(map[string]interface{})["bwType"] = info["bwType"] - }else {sou.(map[string]interface{})["bwType"] = "4"} + if info["tableName"] != nil { + sou.(map[string]interface{})["tableName"] = info["tableName"] + } else { + sou.(map[string]interface{})["tableName"] = "鍏朵粬" + } + if info["bwType"] != nil { + sou.(map[string]interface{})["bwType"] = info["bwType"] + } else { + sou.(map[string]interface{})["bwType"] = "4" + } } } } - dmap := make(map[string]interface{},2) + dmap := make(map[string]interface{}, 2) dmap["datalist"] = sources dmap["total"] = dataLen util.ResponseFormat(c, code.Success, dmap) } - // @Summary 鏌ヨ鎽勫儚鏈轰互鍙婂惎绠楁硶 // @Description 鍏宠仈鏌ヨ鎽勫儚鏈轰互鍙婂惎绠楁硶 @@ -111,51 +125,52 @@ util.ResponseFormat(c, code.ComError, "娌℃湁璁板綍") return } - camList := make([]map[string]interface{},0,5) + camList := make([]map[string]interface{}, 0, 5) for _, cam := range rows { newCam := make(map[string]interface{}) newCam["cameraid"] = cam.Id newCam["rtspUrl"] = cam.Rtsp tasks := cam.CamTask - taskList := make([]map[string]interface{},0) + taskList := make([]map[string]interface{}, 0) if tasks != nil { - taskmap:= make(map[string]interface{}) - for _,task := range tasks { + taskmap := make(map[string]interface{}) + for _, task := range tasks { taskId := taskmap[task.Taskid] if taskId != nil { cameraTasks := taskmap[task.Taskid].([]models.CameraTask) - cameraTasks = append(cameraTasks,task) + cameraTasks = append(cameraTasks, task) taskmap[task.Taskid] = cameraTasks - }else { - cameraTasks := make([]models.CameraTask,0) - taskmap[task.Taskid] = append(cameraTasks,task) + } else { + cameraTasks := make([]models.CameraTask, 0) + taskmap[task.Taskid] = append(cameraTasks, task) } } for key, value := range taskmap { i := make(map[string]interface{}) i["taskid"] = key i["sdklist"] = value - taskList = append(taskList,i) + taskList = append(taskList, i) } } newCam["tasklist"] = taskList - camList = append(camList,newCam) + camList = append(camList, newCam) } - c.JSON(200,camList) + c.JSON(200, camList) } func searchEsData(searchBody models.EsSearch) map[string]interface{} { //璇锋眰绱㈠紩 - index := config.EsInfo.EsIndex.VideoPersons.IndexName // wp鍙煡浜鸿劯鏁版嵁 - queryStr := ""; + index := config.EsInfo.EsIndex.VideoPersons.IndexName // wp鍙煡浜鸿劯鏁版嵁 + queryStr := "" queryBody := searchBody.InputValue //妫�绱㈡ if queryBody != "" { - queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"tableName^1.5\",\"gender^2.0\",\"race^2.0\",\"content^1.0\",\"idcard^1.8\",\"picAddress^1.0\"]," + + queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"tableName^1.5\",\"sex^2.0\",\"race^2.0\",\"content^1.0\",\"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] + gteDate := searchBody.SearchTime[0] + lteDate := searchBody.SearchTime[1] //鍒ゆ柇浠诲姟ID taskIdStr := "" taskId := searchBody.Tasks @@ -164,11 +179,11 @@ taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}}," } //鍒ゆ柇鎽勫儚鏈篒D - videoReqNumStr := "" - videoReqNum := searchBody.TreeNodes - if videoReqNum != nil && len(videoReqNum) > 0 { - esVideoReqNum := strings.Replace(strings.Trim(fmt.Sprint(videoReqNum), "[]"), " ", "\",\"", -1) - videoReqNumStr = "{\"terms\":{\"videoReqNum\":[\"" + esVideoReqNum + "\"]}}," + 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 @@ -178,10 +193,10 @@ esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1) esTableIdStr = "{\"terms\":{\"tableId\":[\"" + esTableId + "\"]}}," } - collectionStr := "" - collection := searchBody.Collection - if collection != "" { - collectionStr = "{\"term\":{\"collection\":\"" + collection + "\"}}," + isCollectionStr := "" + isCollection := searchBody.Collection + if isCollection != "" { + isCollectionStr = "{\"term\":{\"collection\":\"" + isCollection + "\"}}," } webPage := searchBody.Page webSize := searchBody.Size @@ -194,22 +209,21 @@ "/" + index + "/_search?search_type=dfs_query_then_fetch" //璇锋眰浣� prama := "{\"from\":\"" + esFrom + "\",\"size\":\"" + esSize + "\"," + -// prama := "{\"size\":\"0\"," + + // prama := "{\"size\":\"0\"," + "\"query\":{\"bool\":{" + queryStr + "\"filter\":[" + - videoReqNumStr + + cameraIdStr + taskIdStr + - collectionStr + + isCollectionStr + esTableIdStr + "{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," + "\"sort\":[{\"_score\":{\"order\":\"desc\"}},{\"picDate\":{\"order\":\"desc\"}}]," + - "\"_source\":[\"tableName\",\"gender\",\"race\",\"content\",\"IDCard\",\"picAddress\",\"picDate\",\"sdkType\",\"age\",\"personId\",\"personIsHub\",\"personPicUrl\",\"picLocalUrl\",\"picSmUrl\",\"videoIp\",\"videoNum\",\"videoReqNum\",\"baseInfo\"]}"; + "\"_source\":[\"baseInfo\",\"alarmRules\",\"sex\",\"analyServerName\",\"sdkName\",\"ageDescription\",\"content\",\"id\",\"cameraAddr\",\"picMaxUrl\",\"picDate\",\"race\",\"videoUrl\",\"picSmUrl\",\"taskName\",\"personIsHub\",\"isAlarm\",\"analyServerIp\",\"cameraId\"]}" fmt.Println(prama) //鏁版嵁瑙f瀽 tokenRes := esutil.GetEsDataReq(url, prama, true) return tokenRes } - func sourceCompare(sources []interface{}, isEsSource bool, campareByte []byte, threshold float32) []interface{} { var filterName = "feature" -- Gitblit v1.8.0