elastic.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_forum_hub/models"
  6. "eta/eta_forum_hub/utils"
  7. "fmt"
  8. "github.com/olivere/elastic/v7"
  9. )
  10. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  11. func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) {
  12. defer func() {
  13. if err != nil {
  14. fmt.Println("EsAddOrEditData Err:", err.Error())
  15. }
  16. }()
  17. client := utils.EsClient
  18. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  19. if err != nil {
  20. fmt.Println("新增失败:", err.Error())
  21. return err
  22. }
  23. fmt.Println(resp)
  24. if resp.Status == 0 {
  25. fmt.Println("新增成功", resp.Result)
  26. err = nil
  27. } else {
  28. fmt.Println("AddData", resp.Status, resp.Result)
  29. }
  30. return
  31. }
  32. // EsDeleteEdbInfoData 删除es中的指标数据
  33. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  34. defer func() {
  35. if err != nil {
  36. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  37. }
  38. }()
  39. client := utils.EsClient
  40. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  41. fmt.Println(resp)
  42. if err != nil {
  43. return
  44. }
  45. if resp.Status == 0 {
  46. fmt.Println("删除成功")
  47. } else {
  48. fmt.Println("AddData", resp.Status, resp.Result)
  49. }
  50. return
  51. }
  52. // EsAddOrEditChartInfoData 新增/修改es中的图表数据
  53. func EsAddOrEditChartInfoData(indexName, docId string, item *models.ChartInfo) (err error) {
  54. defer func() {
  55. if err != nil {
  56. fmt.Println("EsAddOrEditData Err:", err.Error())
  57. }
  58. }()
  59. client := utils.EsClient
  60. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  61. if err != nil {
  62. fmt.Println("新增失败:", err.Error())
  63. return err
  64. }
  65. fmt.Println(resp)
  66. if resp.Status == 0 {
  67. fmt.Println("新增成功", resp.Result)
  68. err = nil
  69. } else {
  70. fmt.Println("AddData", resp.Status, resp.Result)
  71. }
  72. return
  73. }
  74. // EsDeleteDataV2 删除es中的数据
  75. func EsDeleteDataV2(indexName, docId string) (err error) {
  76. defer func() {
  77. if err != nil {
  78. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  79. }
  80. }()
  81. client := utils.EsClient
  82. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  83. fmt.Println(resp)
  84. if err != nil {
  85. return
  86. }
  87. if resp.Status == 0 {
  88. fmt.Println("删除成功")
  89. } else {
  90. fmt.Println("AddData", resp.Status, resp.Result)
  91. }
  92. return
  93. }
  94. // SearchChartInfoData 查询es中的图表数据
  95. func SearchChartInfoData(indexName, keywordStr string, showSysId []int, sourceList []int, permissionClassifyIdList []int, from, size int) (list []*models.ChartInfo, total int64, err error) {
  96. list = make([]*models.ChartInfo, 0)
  97. defer func() {
  98. if err != nil {
  99. fmt.Println("EsAddOrEditData Err:", err.Error())
  100. }
  101. }()
  102. client := utils.EsClient
  103. //queryString := elastic.NewQueryStringQuery(keywordStr)
  104. //boolQueryJson, err := json.Marshal(queryString)
  105. //if err != nil {
  106. // fmt.Println("boolQueryJson err:", err)
  107. //} else {
  108. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  109. //}
  110. highlight := elastic.NewHighlight()
  111. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  112. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  113. mustMap := make([]interface{}, 0)
  114. mustNotMap := make([]interface{}, 0)
  115. //指标来源
  116. if len(showSysId) > 0 {
  117. mustMap = append(mustMap, map[string]interface{}{
  118. "terms": map[string]interface{}{
  119. "SysUserId": showSysId,
  120. //"Frequency.keyword": "月度",
  121. },
  122. })
  123. }
  124. mustMap = append(mustMap, map[string]interface{}{
  125. "terms": map[string]interface{}{
  126. "Source": sourceList,
  127. },
  128. })
  129. // permissionClassifyIdList 只在特定的分类ID下的图表
  130. if len(permissionClassifyIdList) > 0 {
  131. mustMap = append(mustMap, map[string]interface{}{
  132. "terms": map[string]interface{}{
  133. "ChartClassifyId": permissionClassifyIdList,
  134. //"Frequency.keyword": "月度",
  135. },
  136. })
  137. }
  138. //关键字匹配
  139. //shouldMap := map[string]interface{}{
  140. // "should": []interface{}{
  141. // map[string]interface{}{
  142. // "match": map[string]interface{}{
  143. // "ChartName": keywordStr,
  144. // //"Frequency.keyword": "月度",
  145. // },
  146. // },
  147. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  148. // map[string]interface{}{
  149. // "match": map[string]interface{}{
  150. // "ChartName": map[string]interface{}{
  151. // "query": keywordStr,
  152. // "operator": "and",
  153. // },
  154. // //"Frequency.keyword": "月度",
  155. // },
  156. // },
  157. // map[string]interface{}{
  158. // "match": map[string]interface{}{
  159. // "ChartNameEn": keywordStr,
  160. // //"Frequency.keyword": "月度",
  161. // },
  162. // },
  163. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  164. // map[string]interface{}{
  165. // "match": map[string]interface{}{
  166. // "ChartNameEn": map[string]interface{}{
  167. // "query": keywordStr,
  168. // "operator": "and",
  169. // },
  170. // //"Frequency.keyword": "月度",
  171. // },
  172. // },
  173. // },
  174. //}
  175. // 默认使用中文名字字段去匹配
  176. keywordNameKey := `ChartName`
  177. // 如果没有中文,则使用英文名称字段去匹配
  178. /*if !utils.ContainsChinese(keywordStr) {
  179. keywordNameKey = `ChartNameEn`
  180. }*/
  181. shouldMap := map[string]interface{}{
  182. "should": []interface{}{
  183. /*map[string]interface{}{
  184. "match": map[string]interface{}{
  185. keywordNameKey: keywordStr,
  186. //"Frequency.keyword": "月度",
  187. },
  188. },*/
  189. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  190. map[string]interface{}{
  191. "match": map[string]interface{}{
  192. keywordNameKey: map[string]interface{}{
  193. "query": keywordStr,
  194. "operator": "and",
  195. },
  196. //"Frequency.keyword": "月度",
  197. },
  198. },
  199. },
  200. }
  201. mustMap = append(mustMap, map[string]interface{}{
  202. "bool": shouldMap,
  203. })
  204. queryMap := map[string]interface{}{
  205. "query": map[string]interface{}{
  206. "bool": map[string]interface{}{
  207. "must": mustMap,
  208. "must_not": mustNotMap,
  209. //"should": shouldMap,
  210. },
  211. },
  212. }
  213. //根据条件数量统计
  214. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  215. total, err = requestTotalHits.Do(context.Background())
  216. if err != nil {
  217. return
  218. }
  219. // 分页查询
  220. queryMap["from"] = from
  221. queryMap["size"] = size
  222. jsonBytes, _ := json.Marshal(queryMap)
  223. fmt.Println(string(jsonBytes))
  224. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  225. //requestJson, err := json.Marshal(request)
  226. //if err != nil {
  227. // fmt.Println("requestJson err:", err)
  228. //}
  229. //fmt.Println("requestJson ", string(requestJson))
  230. searchMap := make(map[string]string)
  231. searchResp, err := request.Do(context.Background())
  232. if err != nil {
  233. return
  234. }
  235. fmt.Println(searchResp)
  236. fmt.Println(searchResp.Status)
  237. if searchResp.Status != 0 {
  238. return
  239. }
  240. if searchResp.Hits != nil {
  241. for _, v := range searchResp.Hits.Hits {
  242. if _, ok := searchMap[v.Id]; !ok {
  243. itemJson, tmpErr := v.Source.MarshalJSON()
  244. if tmpErr != nil {
  245. err = tmpErr
  246. fmt.Println("movieJson err:", err)
  247. return
  248. }
  249. chartInfoItem := new(models.ChartInfo)
  250. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  251. if err != nil {
  252. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  253. err = tmpErr
  254. return
  255. }
  256. if len(v.Highlight["ChartName"]) > 0 {
  257. chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  258. }
  259. list = append(list, chartInfoItem)
  260. searchMap[v.Id] = v.Id
  261. }
  262. }
  263. }
  264. //for _, v := range result {
  265. // fmt.Println(v)
  266. //}
  267. return
  268. }
  269. // EsAddOrEditDataInterface 新增/修改es中的数据
  270. func EsAddOrEditDataInterface(indexName, docId string, item interface{}) (err error) {
  271. defer func() {
  272. if err != nil {
  273. fmt.Println("EsAddOrEditData Err:", err.Error())
  274. }
  275. }()
  276. client := utils.EsClient
  277. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  278. if err != nil {
  279. fmt.Println("新增失败:", err.Error())
  280. return err
  281. }
  282. fmt.Println(resp)
  283. if resp.Status == 0 {
  284. fmt.Println("新增成功", resp.Result)
  285. err = nil
  286. } else {
  287. fmt.Println("AddData", resp.Status, resp.Result)
  288. }
  289. return
  290. }