elastic.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package elastic
  2. import (
  3. "context"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "strings"
  8. )
  9. func EsCreateIndex(indexName, mappingJson string) (err error) {
  10. client := utils.EsClient
  11. //定义表结构
  12. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  13. if err != nil {
  14. return
  15. }
  16. if !exists {
  17. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  18. //BodyJson(bodyJson).Do(context.Background())
  19. if err != nil {
  20. fmt.Println("CreateIndex Err:" + err.Error())
  21. return err
  22. }
  23. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  24. } else {
  25. fmt.Println(indexName + " 已存在")
  26. }
  27. return
  28. }
  29. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  30. func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) {
  31. defer func() {
  32. if err != nil {
  33. fmt.Println("EsAddOrEditData Err:", err.Error())
  34. }
  35. }()
  36. client := utils.EsClient
  37. if err != nil {
  38. return
  39. }
  40. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  41. if err != nil {
  42. fmt.Println("新增失败:", err.Error())
  43. return err
  44. }
  45. fmt.Println(resp)
  46. if resp.Status == 0 {
  47. fmt.Println("新增成功", resp.Result)
  48. err = nil
  49. } else {
  50. fmt.Println("AddData", resp.Status, resp.Result)
  51. }
  52. return
  53. }
  54. // EsAddOrEditBaseFromYongyiIndex 新增/修改es中的原始涌溢指标
  55. func EsAddOrEditBaseFromYongyiIndex(indexName, docId string, item *models.BaseFromYongyiIndexList) (err error) {
  56. defer func() {
  57. if err != nil {
  58. fmt.Println("EsAddOrEditData Err:", err.Error())
  59. }
  60. }()
  61. client := utils.EsClient
  62. if err != nil {
  63. return
  64. }
  65. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  66. if err != nil {
  67. if strings.Contains(err.Error(), "no such index") {
  68. //新增index
  69. err = CreateEsEditBaseFromYongyiIndex()
  70. if err != nil {
  71. return
  72. }
  73. resp, err = client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  74. if err != nil {
  75. return
  76. }
  77. } else {
  78. fmt.Println("新增失败:", err.Error())
  79. return err
  80. }
  81. }
  82. fmt.Println(resp)
  83. if resp.Status == 0 {
  84. fmt.Println("新增成功", resp.Result)
  85. err = nil
  86. } else {
  87. fmt.Println("AddData", resp.Status, resp.Result)
  88. }
  89. return
  90. }
  91. func CreateEsEditBaseFromYongyiIndex() (err error) {
  92. indexName := utils.ES_INDEX_BASE_FROM_YONGYI_INDEX
  93. mappingJson := `{
  94. "mappings": {
  95. "dynamic": true,
  96. "properties": {
  97. "YongyiIndexId": {
  98. "type" : "long"
  99. },
  100. "IndexName": {
  101. "type": "text",
  102. "fields" : {
  103. "keyword" : {
  104. "type" : "keyword",
  105. "ignore_above" : 256
  106. }
  107. }
  108. },
  109. "ClassifyId": {
  110. "type" : "long"
  111. },
  112. "Frequency": {
  113. "type": "text",
  114. "fields" : {
  115. "keyword" : {
  116. "type" : "keyword",
  117. "ignore_above" : 256
  118. }
  119. }
  120. },
  121. "IndexCode": {
  122. "type": "text"
  123. },
  124. "Unit": {
  125. "type": "text",
  126. "fields" : {
  127. "keyword" : {
  128. "type" : "keyword",
  129. "ignore_above" : 256
  130. }
  131. }
  132. },
  133. "CreateTime": {
  134. "type" : "text",
  135. "fields" : {
  136. "keyword" : {
  137. "type" : "keyword",
  138. "ignore_above" : 256
  139. }
  140. }
  141. },
  142. "ModifyTime": {
  143. "type" : "text",
  144. "fields" : {
  145. "keyword" : {
  146. "type" : "keyword",
  147. "ignore_above" : 256
  148. }
  149. }
  150. },
  151. "StartDate": {
  152. "type" : "text",
  153. "fields" : {
  154. "keyword" : {
  155. "type" : "keyword",
  156. "ignore_above" : 256
  157. }
  158. }
  159. },
  160. "EndDate": {
  161. "type" : "text",
  162. "fields" : {
  163. "keyword" : {
  164. "type" : "keyword",
  165. "ignore_above" : 256
  166. }
  167. }
  168. },
  169. "TerminalCode": {
  170. "type": "text"
  171. }
  172. }
  173. }
  174. }`
  175. err = EsCreateIndex(indexName, mappingJson)
  176. return
  177. }