es.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package knowledge
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta_gn/eta_api/models/knowledge"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "strconv"
  9. )
  10. // SearchChartInfoData 查询es中的图表数据
  11. func SearchKnowledgeResourceByEs(resourceType int, keywordStr string, showSysIds, classifyIds []int, sourceList []string, tagIds []int, isIncludeFile bool, from, size int) (list []*knowledge.KnowledgeResource, total int64, err error) {
  12. indexName := utils.EsKnowledgeResourceIndexName
  13. list = make([]*knowledge.KnowledgeResource, 0)
  14. defer func() {
  15. if err != nil {
  16. fmt.Println("SearchKnowledgeResource Err:", err.Error())
  17. utils.FileLog.Info("SearchKnowledgeResource Err:", err.Error())
  18. }
  19. }()
  20. client := utils.EsClient
  21. mustMap := make([]interface{}, 0)
  22. mustMap = append(mustMap, map[string]interface{}{
  23. "term": map[string]interface{}{
  24. "ResourceType": resourceType,
  25. },
  26. })
  27. //指标来源
  28. if len(showSysIds) > 0 {
  29. mustMap = append(mustMap, map[string]interface{}{
  30. "terms": map[string]interface{}{
  31. "SysUserId": showSysIds,
  32. },
  33. })
  34. }
  35. if len(tagIds) > 0 {
  36. mustMap = append(mustMap, map[string]interface{}{
  37. "terms": map[string]interface{}{
  38. "TagId": tagIds,
  39. },
  40. })
  41. }
  42. if len(sourceList) > 0 {
  43. mustMap = append(mustMap, map[string]interface{}{
  44. "terms": map[string]interface{}{
  45. "SourceFrom": sourceList,
  46. },
  47. })
  48. }
  49. if len(classifyIds) > 0 {
  50. mustMap = append(mustMap, map[string]interface{}{
  51. "terms": map[string]interface{}{
  52. "ClassifyId": classifyIds,
  53. },
  54. })
  55. }
  56. mustMap = append(mustMap, map[string]interface{}{
  57. "term": map[string]interface{}{
  58. "IsDelete": 0,
  59. },
  60. })
  61. if !isIncludeFile {
  62. mustMap = append(mustMap, map[string]interface{}{
  63. "term": map[string]interface{}{
  64. "IsFile": 0,
  65. },
  66. })
  67. }
  68. shouldMap := map[string]interface{}{
  69. "should": []interface{}{
  70. map[string]interface{}{
  71. "match": map[string]interface{}{
  72. "Title": keywordStr,
  73. },
  74. },
  75. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  76. map[string]interface{}{
  77. "match": map[string]interface{}{
  78. "Title": map[string]interface{}{
  79. "query": keywordStr,
  80. "operator": "and",
  81. },
  82. },
  83. },
  84. },
  85. }
  86. mustMap = append(mustMap, map[string]interface{}{
  87. "bool": shouldMap,
  88. })
  89. queryMap := map[string]interface{}{
  90. "query": map[string]interface{}{
  91. "bool": map[string]interface{}{
  92. "must": mustMap,
  93. },
  94. },
  95. }
  96. //根据条件数量统计
  97. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  98. total, err = requestTotalHits.Do(context.Background())
  99. if err != nil {
  100. return
  101. }
  102. // 分页查询
  103. queryMap["from"] = from
  104. queryMap["size"] = size
  105. jsonBytes, _ := json.Marshal(queryMap)
  106. fmt.Println(string(jsonBytes))
  107. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  108. searchMap := make(map[string]string)
  109. searchResp, err := request.Do(context.Background())
  110. if err != nil {
  111. return
  112. }
  113. fmt.Println(searchResp)
  114. fmt.Println(searchResp.Status)
  115. if searchResp.Status != 0 {
  116. return
  117. }
  118. if searchResp.Hits != nil {
  119. for _, v := range searchResp.Hits.Hits {
  120. if _, ok := searchMap[v.Id]; !ok {
  121. itemJson, tmpErr := v.Source.MarshalJSON()
  122. if tmpErr != nil {
  123. err = tmpErr
  124. fmt.Println("movieJson err:", err)
  125. return
  126. }
  127. knowledgeItem := new(knowledge.KnowledgeResource)
  128. tmpErr = json.Unmarshal(itemJson, &knowledgeItem)
  129. if err != nil {
  130. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  131. err = tmpErr
  132. return
  133. }
  134. list = append(list, knowledgeItem)
  135. searchMap[v.Id] = v.Id
  136. }
  137. }
  138. }
  139. return
  140. }
  141. // EsAddOrEditKnowledgeResource 新增/修改es中的知识资源数据
  142. func EsAddOrEditKnowledgeResource(item *knowledge.KnowledgeResource) (err error) {
  143. defer func() {
  144. if err != nil {
  145. fmt.Println("EsAddOrEditData Err:", err.Error())
  146. utils.FileLog.Info("EsAddOrEditKnowledgeResource err:", err)
  147. }
  148. }()
  149. indexName := utils.EsKnowledgeResourceIndexName
  150. client := utils.EsClient
  151. itemJson, err := json.Marshal(item)
  152. if err != nil {
  153. fmt.Println("EsAddOrEditKnowledgeResource json.Marshal itemJson err:", err)
  154. return
  155. }
  156. request := client.Index().Index(indexName).Id(strconv.Itoa(item.KnowledgeResourceId)).BodyJson(itemJson)
  157. response, err := request.Do(context.Background())
  158. if err != nil {
  159. fmt.Println("add json:", string(itemJson))
  160. fmt.Println("EsAddOrEditKnowledgeResource err:", err)
  161. return
  162. }
  163. if response.Status == 0 {
  164. err = fmt.Errorf("add knowledge resource to es failed, response result is %s", response.Result)
  165. err = nil
  166. } else {
  167. fmt.Println("EsAddOrEditKnowledgeResource:", response.Status, response.Result)
  168. }
  169. return
  170. }