123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package elastic
- import (
- "context"
- "encoding/json"
- "eta/eta_forum_hub/models/chart_collect"
- "eta/eta_forum_hub/services/alarm_msg"
- "eta/eta_forum_hub/utils"
- "fmt"
- )
- // SearchChartCollectData 查询es中的收藏图表数据
- func SearchChartCollectData(indexName, keywordStr string, userId int, permissionChartClassifyIdList []int, collectClassifyIds []string, adminIdsSlice []int, from, size int) (list []*chart_collect.ChartCollectEsItem, total int64, err error) {
- list = make([]*chart_collect.ChartCollectEsItem, 0)
- defer func() {
- if err != nil {
- utils.FileLog.Info("SearchChartCollectData Err:" + err.Error())
- go alarm_msg.SendAlarmMsg("SearchChartCollectData Err:"+err.Error(), 3)
- }
- }()
- client := utils.EsClient
- mustMap := make([]interface{}, 0)
- shouldItemMap := make([]interface{}, 0)
- //指标来源
- if userId > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "term": map[string]interface{}{
- "UserId": userId,
- },
- })
- }
- // permissionChartClassifyIdList 有权限的分类
- if len(permissionChartClassifyIdList) > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "ChartClassifyId": permissionChartClassifyIdList,
- },
- })
- }
- if keywordStr != "" {
- mustMap = append(mustMap, map[string]interface{}{
- "match_phrase": map[string]interface{}{
- "ChartName": keywordStr,
- },
- })
- }
- if len(adminIdsSlice) > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "SysUserId": adminIdsSlice,
- },
- })
- }
- if len(collectClassifyIds) > 0 {
- for _, cid := range collectClassifyIds {
- shouldItemMap = append(shouldItemMap, map[string]interface{}{
- "match_phrase": map[string]interface{}{
- "CollectClassifyIds": cid,
- },
- })
- }
- }
- boolMap := map[string]interface{}{
- "must": mustMap,
- }
- if len(shouldItemMap) > 0 {
- boolMap["should"] = shouldItemMap
- boolMap["minimum_should_match"] = 1
- }
- queryMap := map[string]interface{}{
- "query": map[string]interface{}{
- "bool": boolMap,
- },
- }
- jsonBytes, _ := json.Marshal(queryMap)
- fmt.Println(string(jsonBytes))
- //根据条件数量统计
- requestTotalHits := client.Count(indexName).BodyJson(queryMap)
- total, err = requestTotalHits.Do(context.Background())
- if err != nil {
- return
- }
- // 分页查询
- queryMap["from"] = from
- queryMap["size"] = size
- queryMap["sort"] = []map[string]interface{}{
- map[string]interface{}{
- "CollectTime": map[string]interface{}{
- "order": "desc",
- },
- },
- }
- request := client.Search(indexName).Source(queryMap) // sets the JSON request
- searchMap := make(map[string]string)
- searchResp, err := request.Do(context.Background())
- if err != nil {
- return
- }
- fmt.Println(searchResp)
- fmt.Println(searchResp.Status)
- if searchResp.Status != 0 {
- return
- }
- if searchResp.Hits != nil {
- for _, v := range searchResp.Hits.Hits {
- if _, ok := searchMap[v.Id]; !ok {
- itemJson, tmpErr := v.Source.MarshalJSON()
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("movieJson err:", err)
- return
- }
- chartInfoItem := new(chart_collect.ChartCollectEsItem)
- tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
- if err != nil {
- fmt.Println("json.Unmarshal chartInfoJson err:", err)
- err = tmpErr
- return
- }
- if len(v.Highlight["ChartName"]) > 0 {
- chartInfoItem.ChartName = v.Highlight["ChartName"][0]
- }
- list = append(list, chartInfoItem)
- searchMap[v.Id] = v.Id
- }
- }
- }
- //for _, v := range result {
- // fmt.Println(v)
- //}
- return
- }
|