es.go 7.2 KB

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