es.go 5.7 KB

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