sunty
2019-07-29 75c6f6fea50b977cb72a50f4609a5cbbbf235c35
controllers/esSearch.go
@@ -5,6 +5,7 @@
   "fmt"
   "strconv"
   "strings"
   "webserver/extend/logger"
   "github.com/gin-gonic/gin"
   "webserver/extend/code"
@@ -32,6 +33,37 @@
   data := findEsData(searchBody)
   util.ResponseFormat(c, code.Success, data)
}
//获取监控等级
func getAlarmLevel(alarmlevel []interface{}) ([]string) {
   d := dbapi.DicApi{}
        res,data := d.FindByType("ALARMLEVEL")
        if data == nil {
                logger.Debug("data is nil")
        }
   logger.Debug(res)
      // logger.Debug(data)
   alarmLevel := make(map[int]string)
   tmp := data.(map[string]interface{})
      for _,value := range tmp["ALARMLEVEL"].([]interface{}) {
         // logger.Debug(value.(map[string]interface{})["value"],"      ",value.(map[string]interface{})["name"])
         val := value.(map[string]interface{})["value"].(string)
         name := value.(map[string]interface{})["name"].(string)
         vl,err := strconv.Atoi(val)
         if err != nil {
            logger.Debug(err)
         }
         alarmLevel[vl] = name
      }
   alarmLevelRes := make([]string,len(alarmlevel))
      for i,v := range  alarmlevel  {
         if v.(int) < 0 {
            alarmLevelRes[i] = "撤防"
         }else {
            alarmLevelRes[i] =  alarmLevel[v.(int)]
         }
      }
   return alarmLevelRes
}
func findEsData(searchBody map[string]interface{}) map[string]interface{} {
   webPage := int(searchBody["page"].(float64))
@@ -47,7 +79,7 @@
   queryBody := searchBody["inputValue"].(string)
   //检索框
   if queryBody != "" {
      queryStr = "\"must\":[{\"multi_match\":{\"query\":\"" + queryBody + "\",\"fields\":[\"alarmRules.alarmLevel^1.5\",\"ageDescription^1.5\",\"taskName^1.5\",\"tableName^1.5\",\"sex^2.0\",\"race^2.0\",\"content^1.0\",\"idCard^1.8\",\"cameraAddr^1.0\"]," +
      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)
@@ -74,14 +106,22 @@
   if tableId != nil && len(tableId) > 0 {
      esTableId = strings.Replace(strings.Trim(fmt.Sprint(tableId), "[]"), " ", "\",\"", -1)
      index = config.EsInfo.EsIndex.VideoPersons.IndexName
      esTableIdStr = "{\"terms\":{\"tableId\":[\"" + esTableId + "\"]}},"
      esTableIdStr = "{\"terms\":{\"baseInfo.tableId\":[\"" + esTableId + "\"]}},"
   }
   //判断收藏状态
   isCollectStr := ""
   isCollect := searchBody["collection"].(string)
   if isCollect != "" {
      isCollectStr = "{\"term\":{\"isCollect\":\"" + isCollect + "\"}},"
   }
   //判断布防等级id
   alarmLevelId :=searchBody["alarmlevel"].([]interface{})
   alarmLevelStr := ""
   if len(alarmLevelId) > 0 {
      alarmLevelTypes := strings.Replace(strings.Trim(fmt.Sprint(getAlarmLevel(alarmLevelId)), "[]"), " ", "\",\"", -1)
      alarmLevelStr = "{\"terms\":{\"alarmRules.alarmLevel\":[\"" + alarmLevelTypes + "\"]}},"
   }
   //使用es底层机制处理分页
   //请求头
   url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
@@ -96,6 +136,7 @@
      "\"query\":{\"bool\":{" + queryStr +
      "\"filter\":[" +
      cameraIdStr +
      alarmLevelStr +
      taskIdStr +
      isCollectStr +
      esTableIdStr +
@@ -104,7 +145,7 @@
      "\"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)
   logger.Debug(prama)
   //数据解析
   tokenRes := esutil.GetEsDataReq(url, prama, true)
   return tokenRes
@@ -125,7 +166,7 @@
   tokenRes := esutil.GetEsDataReq(url, prama, true)
   personRes := make(map[string]string)
   //fmt.Println(tokenRes["datalist"].([]interface{}))
   //logger.Debug(tokenRes["datalist"].([]interface{}))
   result := tokenRes["datalist"].([]interface{})
   for _, value := range result {
      userMap := value.(map[string]interface{})
@@ -147,7 +188,7 @@
      // personRes["phoneNum"] = value.(map[string]interface{})["phoneNum"].(string)
      // personRes["sex"] = value.(map[string]interface{})["sex"].(string)
   }
   //fmt.Println(personRes)
   //logger.Debug(personRes)
   if len(personRes) < 1 {
      personRes["personName"] = ""
      personRes["phoneNum"] = ""