es.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. "html"
  9. "strconv"
  10. "strings"
  11. "github.com/PuerkitoBio/goquery"
  12. )
  13. // SearchChartInfoData 查询es中的图表数据
  14. func SearchKnowledgeResourceByEs(resourceType int, keywordStr string, showSysIds []int, myId int, classifyIds []int, sourceList []string, tagIds []int, isIncludeFile bool, from, size int) (list []*knowledge.KnowledgeResource, total int64, err error) {
  15. indexName := utils.EsKnowledgeResourceIndexName
  16. list = make([]*knowledge.KnowledgeResource, 0)
  17. defer func() {
  18. if err != nil {
  19. fmt.Println("SearchKnowledgeResource Err:", err.Error())
  20. utils.FileLog.Info("SearchKnowledgeResource Err:", err.Error())
  21. }
  22. }()
  23. client := utils.EsClient
  24. mustMap := make([]interface{}, 0)
  25. mustMap = append(mustMap, map[string]interface{}{
  26. "term": map[string]interface{}{
  27. "ResourceType": resourceType,
  28. },
  29. })
  30. //指标来源
  31. if len(showSysIds) > 0 {
  32. mustMap = append(mustMap, map[string]interface{}{
  33. "terms": map[string]interface{}{
  34. "AdminId": showSysIds,
  35. },
  36. })
  37. }
  38. if myId > 0 {
  39. mustMap = append(mustMap, map[string]interface{}{
  40. "term": map[string]interface{}{
  41. "AdminId": myId,
  42. },
  43. })
  44. }
  45. if len(tagIds) > 0 {
  46. mustMap = append(mustMap, map[string]interface{}{
  47. "terms": map[string]interface{}{
  48. "TagId": tagIds,
  49. },
  50. })
  51. }
  52. if len(sourceList) > 0 {
  53. mustMap = append(mustMap, map[string]interface{}{
  54. "terms": map[string]interface{}{
  55. "SourceFrom": sourceList,
  56. },
  57. })
  58. }
  59. if len(classifyIds) > 0 {
  60. mustMap = append(mustMap, map[string]interface{}{
  61. "terms": map[string]interface{}{
  62. "ClassifyId": classifyIds,
  63. },
  64. })
  65. }
  66. mustMap = append(mustMap, map[string]interface{}{
  67. "term": map[string]interface{}{
  68. "IsDelete": 0,
  69. },
  70. })
  71. if !isIncludeFile {
  72. mustMap = append(mustMap, map[string]interface{}{
  73. "term": map[string]interface{}{
  74. "IsFile": 0,
  75. },
  76. })
  77. }
  78. if keywordStr != "" {
  79. shouldMap := map[string]interface{}{
  80. "should": []interface{}{
  81. map[string]interface{}{
  82. "match": map[string]interface{}{
  83. "Title": keywordStr,
  84. },
  85. },
  86. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  87. map[string]interface{}{
  88. "match": map[string]interface{}{
  89. "Title": map[string]interface{}{
  90. "query": keywordStr,
  91. "operator": "and",
  92. },
  93. },
  94. },
  95. },
  96. }
  97. mustMap = append(mustMap, map[string]interface{}{
  98. "bool": shouldMap,
  99. })
  100. }
  101. queryMap := map[string]interface{}{
  102. "query": map[string]interface{}{
  103. "bool": map[string]interface{}{
  104. "must": mustMap,
  105. },
  106. },
  107. }
  108. //根据条件数量统计
  109. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  110. total, err = requestTotalHits.Do(context.Background())
  111. if err != nil {
  112. return
  113. }
  114. // 分页查询
  115. queryMap["from"] = from
  116. queryMap["size"] = size
  117. if keywordStr == "" {
  118. sortMap := []map[string]interface{}{
  119. {
  120. "StartTime.keyword": map[string]interface{}{
  121. "order": "desc", // 按照开始时间倒序排列
  122. },
  123. },
  124. }
  125. queryMap["sort"] = sortMap
  126. }
  127. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  128. searchMap := make(map[string]string)
  129. searchResp, err := request.Do(context.Background())
  130. if err != nil {
  131. return
  132. }
  133. if searchResp.Status != 0 {
  134. jsonBytes, _ := json.Marshal(queryMap)
  135. utils.FileLog.Info("search json:%s,SearchKnowledgeResourceByEs Status:%d", string(jsonBytes), searchResp.Status)
  136. return
  137. }
  138. if searchResp.Hits != nil {
  139. for _, v := range searchResp.Hits.Hits {
  140. if _, ok := searchMap[v.Id]; !ok {
  141. itemJson, tmpErr := v.Source.MarshalJSON()
  142. if tmpErr != nil {
  143. err = tmpErr
  144. utils.FileLog.Info("search json:%s,SearchKnowledgeResourceByEs Hits Source err:%s", string(itemJson), tmpErr.Error())
  145. return
  146. }
  147. knowledgeItem := new(knowledge.KnowledgeResource)
  148. tmpErr = json.Unmarshal(itemJson, &knowledgeItem)
  149. if err != nil {
  150. utils.FileLog.Info("json.Unmarshal KnowledgeResource err:%s", err.Error())
  151. err = tmpErr
  152. return
  153. }
  154. list = append(list, knowledgeItem)
  155. searchMap[v.Id] = v.Id
  156. }
  157. }
  158. }
  159. return
  160. }
  161. func ExtractTextFromResourceContent(content string) (text string) {
  162. content = html.UnescapeString(content)
  163. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  164. if err != nil {
  165. return
  166. }
  167. text = doc.Text()
  168. text = strings.ReplaceAll(text, "\n", "")
  169. return
  170. }
  171. // EsAddOrEditKnowledgeResource 新增/修改es中的知识资源数据
  172. func EsAddOrEditKnowledgeResource(item *knowledge.KnowledgeResource) (err error) {
  173. defer func() {
  174. if err != nil {
  175. fmt.Println("EsAddOrEditData Err:", err.Error())
  176. utils.FileLog.Info("EsAddOrEditKnowledgeResource err:", err)
  177. }
  178. }()
  179. indexName := utils.EsKnowledgeResourceIndexName
  180. client := utils.EsClient
  181. if item.IsFile == 0 {
  182. content := ExtractTextFromResourceContent(item.Content)
  183. contentRunes := []rune(content)
  184. if len(contentRunes) > 60 {
  185. item.Content = string(contentRunes[:60])
  186. } else {
  187. item.Content = content
  188. }
  189. }
  190. request := client.Index().Index(indexName).Id(strconv.Itoa(item.KnowledgeResourceId)).BodyJson(item)
  191. response, err := request.Do(context.Background())
  192. if err != nil {
  193. jsonBytes, _ := json.Marshal(item)
  194. utils.FileLog.Info("add json:%s,EsAddOrEditKnowledgeResource err:%s", string(jsonBytes), err.Error())
  195. return
  196. }
  197. if response.Status == 0 {
  198. err = nil
  199. } else {
  200. fmt.Println("EsAddOrEditKnowledgeResource:", response.Status, response.Result)
  201. }
  202. return
  203. }