es.go 6.0 KB

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