package esutil import ( "encoding/json" "errors" "fmt" "strconv" "strings" "basic.com/pubsub/protomsg.git" ) // 查询底库人员信息 func Personinfos( queryIndex int, queryNums int, indexName string, serverIp string, serverPort string, analyServerId string) ([]*protomsg.Esinfo, error){ var dbinfos []*protomsg.Esinfo point := strconv.Itoa(queryIndex) number := strconv.Itoa(queryNums) JsonDSL := "" if indexName == "videopersons" { JsonDSL = ` { "from": ` + point + `, "query": { "bool": { "filter": [ { "term": { "analyServerId": "` + analyServerId + `" } } ] } }, "size":`+ number +`, "_source": [ "id", "faceFeature" ] }` }else { JsonDSL = ` { "from": ` + point + `, "query": { "match_all": {} }, "size":`+ number +`, "_source": [ "id", "tableId", "faceFeature" ] }` } //fmt.Println("url: "+"http://"+serverIp+":"+serverPort+"/"+indexName+"/_search","body: ",JsonDSL) buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(JsonDSL)) if err != nil { return dbinfos ,errors.New("http request dbtablename info is err!") } // 返回 _source 数组 sources, err := Sourcelist(buf) if err != nil { return dbinfos,err } // 返回所有查询的数据 dbpersoninfos := Parsesources(sources) return dbpersoninfos, nil } // 根据底库id查询底库信息 func Dbtablefosbyid (id []string, indexName string, serverIp string, serverPort string) ([]protomsg.Dbtable, error) { var dbinfo []protomsg.Dbtable dbtableId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1) var dbinfoRequest = ` { "query": { "bool": { "filter": [{ "terms": { "id": [ "`+ dbtableId +`" ] } }] } }, "size":1000000 } ` buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfoRequest)) if err != nil { return dbinfo, err } sources, err := Sourcelist(buf) if err != nil { return dbinfo , err } dbtable := Dbtablebyid(sources) return dbtable, nil } // 根据抓拍人员id查询抓拍人员信息 func Videopersonsinfosbyid (id []string, indexName string, serverIp string, serverPort string) ([]protomsg.Videopersons, error) { var videopersonsInfo []protomsg.Videopersons videopersonsPersonId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1) var dbinfoRequest = ` { "query": { "bool": { "filter": [{ "terms": { "id": [ "`+ videopersonsPersonId +`" ] } }] } }, "size":1000000 } ` buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfoRequest)) if err != nil { return videopersonsInfo, err } sources, err := Sourcelist(buf) if err != nil { return videopersonsInfo , err } videoperson := Videopersonsbyid(sources) println(videoperson) return videoperson,nil } // 根据底库人员id查询底库人员信息 func Dbpersoninfosbyid (id []string, indexName string, serverIp string, serverPort string) ([]protomsg.Dbperson, error) { var dbinfo []protomsg.Dbperson dbtablePersonId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1) var dbinfoRequest = ` { "query": { "bool": { "filter": [{ "terms": { "id": [ "`+ dbtablePersonId +`" ] } }] } }, "size":1000000 } ` buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfoRequest)) if err != nil { return dbinfo, err } sources, err := Sourcelist(buf) if err != nil { return dbinfo , err } dbperson := Dbpersonbyid(sources) println(dbperson) return dbperson,nil } // 根据tableid 查询tablename func Dbtablename(tableid string, indexName string, serverIp string, serverPort string) (tablename string, err error) { var dbinfotable =` { "query": { "bool": { "filter": [ { "term": { "id":"`+tableid+`" } } ] } }, "_source": [ "tableName" ], "size":1000000 } ` buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_search",[]byte(dbinfotable)) if err != nil { return "" ,errors.New("http request dbtablename info is err!") } sources, err := Sourcelist(buf) if err != nil { return "",err } for _, source := range sources { if name, ok := source["tableName"].(string); ok { tablename = name break } } return tablename, nil } //根据抓拍人员id更新(videourl)摄像机地址 func UpdateVideourlById(id string, videoUrl string, indexName string, serverIp string, serverPort string)(statu int){ var info interface{} var videoUrlInfo = ` { "script": { "source": "ctx._source.videoUrl='` + videoUrl + `'" }, "query": { "term": { "id": "` +id+ `" } } } ` buf, err := EsReq("POST", "http://"+serverIp+":"+serverPort+"/"+indexName+"/_update_by_query",[]byte(videoUrlInfo)) if err != nil { fmt.Println("http request videoUrlInfo info is err!") statu = 500 return } json.Unmarshal(buf, &info) out, ok := info.(map[string]interface{}) if !ok { fmt.Println("http response interface can not change map[string]interface{}") statu = 500 return } middle, ok := out["updated"].(float64) if !ok { fmt.Println("first updated change error!") statu = 500 return } if middle == 1{ statu = 200 return } if middle == 0{ statu = 201 return } return statu } //获取当前节点抓拍库所有人员ID func GetAllLocalVideopersonsId(compareArgs protomsg.CompareArgs,indexName string, serverIp string, serverPort string) (capturetable []string) { queryStr := "" queryBody := compareArgs.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 := compareArgs.SearchTime[0] lteDate := compareArgs.SearchTime[1] //判断任务ID taskIdStr := "" taskId := compareArgs.Tasks if taskId != nil && len(taskId) > 0 { esTaskId := strings.Replace(strings.Trim(fmt.Sprint(taskId), "[]"), " ", "\",\"", -1) taskIdStr = "{\"terms\":{\"taskId\":[\"" + esTaskId + "\"]}}," } //判断摄像机ID cameraIdStr := "" cameraId := compareArgs.TreeNodes if cameraId != nil && len(cameraId) > 0 { esCameraId := strings.Replace(strings.Trim(fmt.Sprint(cameraId), "[]"), " ", "\",\"", -1) cameraIdStr = "{\"terms\":{\"cameraId\":[\"" + esCameraId + "\"]}}," } //判断库表ID tableId := compareArgs.Tabs esTableId := "" esTableIdStr := "" if tableId != nil && len(tableId) > 0 { esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1) esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}}," } isCollectStr := "" isCollect := compareArgs.Collection if isCollect != "" { isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}}," } //使用es底层机制处理分页 analyServerId := compareArgs.AnalyServerId if analyServerId == "" { fmt.Println("no analyServerId") return } analyServerFilterStr := "{\"term\":{\"analyServerId\":\"" + analyServerId + "\"}}," //请求头 url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search?search_type=dfs_query_then_fetch" //请求体 prama := "{" + "\"size\":\"100000000\"," + "\"query\":{\"bool\":{" + queryStr + "\"filter\":[" + cameraIdStr + taskIdStr + isCollectStr + esTableIdStr + analyServerFilterStr + "{\"range\":{\"picDate\":{\"from\":\"" + gteDate + "\",\"to\":\"" + lteDate + "\",\"include_lower\":true,\"include_upper\":true,\"boost\":1}}}]}}," + "\"_source\":[\"id\"]" + "}" fmt.Println(url) fmt.Println(prama) buf, err := EsReq("POST", url,[]byte(prama)) if err != nil { fmt.Println("http request videoUrlInfo info is err!") return } sources, err := Sourcelist(buf) if err != nil { return } for _, source := range sources{ capturetable = append(capturetable, source["id"].(string)) } return capturetable } //获取底库人员ID func GetDbpersonsId(compareArgs protomsg.CompareArgs,indexName string, serverIp string, serverPort string) (source map[string][]string) { queryStr := "" queryBody := compareArgs.InputValue //检索框 if queryBody != "" { queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"personName^1.5\",\"age^1.5\",\"idCard^1.5\",\"phoneNum^1.5\",\"sex^2.0\",\"reserved^2.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}}]," } //判断库表ID tableId := compareArgs.Tabs esTableId := "" esTableIdStr := "" if tableId != nil && len(tableId) > 0 { esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1) esTableIdStr = "{\"terms\":{\"tableId\":[\"" + esTableId + "\"]}}" } prama := "{" + "\"size\":\"100000000\"," + "\"query\":{\"bool\":{" + queryStr + "\"filter\":[" + esTableIdStr + "]}}," + "\"_source\":[\"id\",\"tableId\"]" + "}" url := "http://" + serverIp + ":" + serverPort + "/" + indexName + "/_search?search_type=dfs_query_then_fetch" fmt.Println(url) fmt.Println(prama) buf, err := EsReq("POST", url,[]byte(prama)) if err != nil { fmt.Println("http request videoUrlInfo info is err!") return } sources, err := Sourcelist(buf) if err != nil { return } tabsource := make(map[string][]string) for _, source := range sources{ tableId := source["tableId"].(string) id := source["id"].(string) tabsource[tableId] = append(tabsource[tableId], id) } return tabsource }