elasticSim.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. es "github.com/olivere/elastic/v7"
  8. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  9. "log"
  10. "runtime"
  11. "strings"
  12. "sync"
  13. "time"
  14. )
  15. type Elastic struct {
  16. S_esurl string
  17. I_size int
  18. Addrs []string
  19. Pool chan *es.Client
  20. lastTime int64
  21. lastTimeLock sync.Mutex
  22. ntimeout int
  23. Username string
  24. Password string
  25. }
  26. func (e *Elastic) InitElasticSize() {
  27. e.Pool = make(chan *es.Client, e.I_size)
  28. for _, s := range strings.Split(e.S_esurl, ",") {
  29. e.Addrs = append(e.Addrs, s)
  30. }
  31. for i := 0; i < e.I_size; i++ {
  32. client, _ := es.NewClient(es.SetURL(e.Addrs...), es.SetBasicAuth(e.Username, e.Password), es.SetMaxRetries(2), es.SetSniff(false))
  33. e.Pool <- client
  34. }
  35. }
  36. // 关闭连接
  37. func (e *Elastic) DestoryEsConn(client *es.Client) {
  38. select {
  39. case e.Pool <- client:
  40. break
  41. case <-time.After(time.Second * 1):
  42. if client != nil {
  43. client.Stop()
  44. }
  45. client = nil
  46. }
  47. }
  48. func (e *Elastic) GetEsConn() *es.Client {
  49. select {
  50. case c := <-e.Pool:
  51. if c == nil || !c.IsRunning() {
  52. log.Println("new esclient.", len(e.Pool))
  53. client, err := es.NewClient(es.SetURL(e.Addrs...), es.SetBasicAuth(e.Username, e.Password),
  54. es.SetMaxRetries(2), es.SetSniff(false))
  55. if err == nil && client.IsRunning() {
  56. return client
  57. }
  58. }
  59. return c
  60. case <-time.After(time.Second * 4):
  61. //超时
  62. e.ntimeout++
  63. e.lastTimeLock.Lock()
  64. defer e.lastTimeLock.Unlock()
  65. //12秒后允许创建链接
  66. c := time.Now().Unix() - e.lastTime
  67. if c > 12 {
  68. e.lastTime = time.Now().Unix()
  69. log.Println("add client..", len(e.Pool))
  70. c, _ := es.NewClient(es.SetURL(e.Addrs...), es.SetBasicAuth(e.Username, e.Password), es.SetMaxRetries(2), es.SetSniff(false))
  71. go func() {
  72. for i := 0; i < 2; i++ {
  73. client, _ := es.NewClient(es.SetURL(e.Addrs...), es.SetBasicAuth(e.Username, e.Password), es.SetMaxRetries(2), es.SetSniff(false))
  74. e.Pool <- client
  75. }
  76. }()
  77. return c
  78. }
  79. return nil
  80. }
  81. }
  82. func (e *Elastic) Get(index, query string) *[]map[string]interface{} {
  83. client := e.GetEsConn()
  84. defer func() {
  85. go e.DestoryEsConn(client)
  86. }()
  87. var res []map[string]interface{}
  88. if client != nil {
  89. defer func() {
  90. if r := recover(); r != nil {
  91. log.Println("[E]", r)
  92. for skip := 1; ; skip++ {
  93. _, file, line, ok := runtime.Caller(skip)
  94. if !ok {
  95. break
  96. }
  97. go log.Printf("%v,%v\n", file, line)
  98. }
  99. }
  100. }()
  101. searchResult, err := client.Search().Index(index).Source(query).Do(context.Background())
  102. if err != nil {
  103. log.Println("从ES查询出错", err.Error())
  104. return nil
  105. }
  106. if searchResult.Hits != nil {
  107. resNum := len(searchResult.Hits.Hits)
  108. if resNum < 5000 {
  109. res = make([]map[string]interface{}, resNum)
  110. for i, hit := range searchResult.Hits.Hits {
  111. parseErr := json.Unmarshal(hit.Source, &res[i])
  112. if parseErr == nil && hit.Highlight != nil && res[i] != nil {
  113. res[i]["highlight"] = map[string][]string(hit.Highlight)
  114. }
  115. }
  116. } else {
  117. log.Println("查询结果太多,查询到:", resNum, "条")
  118. }
  119. }
  120. }
  121. return &res
  122. }
  123. // 关闭elastic
  124. func (e *Elastic) Close() {
  125. for i := 0; i < e.I_size; i++ {
  126. cli := <-e.Pool
  127. cli.Stop()
  128. cli = nil
  129. }
  130. e.Pool = nil
  131. e = nil
  132. }
  133. //获取连接
  134. //func (e *Elastic) GetEsConn() (c *es.Client) {
  135. // defer util.Catch()
  136. // select {
  137. // case c = <-e.Pool:
  138. // if c == nil || !c.IsRunning() {
  139. // client, err := es.NewClient(es.SetURL(addrs...),
  140. // es.SetMaxRetries(2), es.SetSniff(false))
  141. // if err == nil && client.IsRunning() {
  142. // return client
  143. // }
  144. // return nil
  145. // }
  146. // return
  147. // case <-time.After(time.Second * 7):
  148. // //超时
  149. // ntimeout++
  150. // log.Println("timeout times:", ntimeout)
  151. // return nil
  152. // }
  153. //}
  154. func (e *Elastic) BulkSave(index string, obj []map[string]interface{}) {
  155. client := e.GetEsConn()
  156. defer e.DestoryEsConn(client)
  157. if client != nil {
  158. req := client.Bulk()
  159. for _, v := range obj {
  160. //if isDelBefore {
  161. // req = req.Add(es.NewBulkDeleteRequest().Index(index).Id(fmt.Sprintf("%v", v["_id"])))
  162. //}
  163. id := util.ObjToString(v["_id"])
  164. delete(v, "_id")
  165. req = req.Add(es.NewBulkIndexRequest().Index(index).Id(id).Doc(v))
  166. }
  167. _, err := req.Do(context.Background())
  168. if err != nil {
  169. log.Println("批量保存到ES出错", err.Error())
  170. }
  171. }
  172. }
  173. // BulkSaveReturnFails 批量保存 返回保存失败的doc
  174. func (e *Elastic) BulkSaveReturnFails(index string, obj []map[string]interface{}) []map[string]interface{} {
  175. client := e.GetEsConn()
  176. defer e.DestoryEsConn(client)
  177. if client == nil {
  178. return obj
  179. }
  180. // 存储 ID 与原始数据的映射关系
  181. idToData := make(map[string]map[string]interface{})
  182. // 收集失败的文档
  183. var failedDocs []map[string]interface{}
  184. req := client.Bulk()
  185. for _, v := range obj {
  186. id := util.ObjToString(v["_id"])
  187. idToData[id] = v // 建立映射
  188. doc := make(map[string]interface{})
  189. for k, va := range v {
  190. doc[k] = va
  191. }
  192. delete(doc, "_id")
  193. req = req.Add(es.NewBulkIndexRequest().Index(index).Id(id).Doc(doc))
  194. }
  195. res, err := req.Do(context.Background())
  196. if err != nil {
  197. log.Println("批量保存到ES出错", err.Error())
  198. return obj
  199. }
  200. if res.Errors {
  201. for _, item := range res.Items {
  202. for _, result := range item {
  203. if result.Error != nil {
  204. // 通过 ID 找到原始数据并添加到失败列表
  205. if originalData, exists := idToData[result.Id]; exists {
  206. // 可以在失败数据中添加错误信息,方便排查
  207. originalData["_error_reason"] = result.Error.Reason
  208. failedDocs = append(failedDocs, originalData)
  209. }
  210. }
  211. }
  212. }
  213. // 返回失败的文档列表,无错误(因为请求已被处理)
  214. return failedDocs
  215. }
  216. // 全部成功,返回空列表
  217. return nil
  218. }
  219. // 根据id删除索引对象
  220. func (e *Elastic) DelById(index, id string) bool {
  221. client := e.GetEsConn()
  222. defer e.DestoryEsConn(client)
  223. b := false
  224. if client != nil {
  225. var err error
  226. _, err = client.Delete().Index(index).Id(id).Do(context.Background())
  227. if err != nil {
  228. log.Println("更新检索出错:", err.Error())
  229. } else {
  230. b = true
  231. }
  232. }
  233. return b
  234. }
  235. func (e *Elastic) GetNoLimit(index, query string) *[]map[string]interface{} {
  236. client := e.GetEsConn()
  237. defer e.DestoryEsConn(client)
  238. var res []map[string]interface{}
  239. if client != nil {
  240. defer func() {
  241. if r := recover(); r != nil {
  242. log.Println("[E]", r)
  243. for skip := 1; ; skip++ {
  244. _, file, line, ok := runtime.Caller(skip)
  245. if !ok {
  246. break
  247. }
  248. go log.Printf("%v,%v\n", file, line)
  249. }
  250. }
  251. }()
  252. searchResult, err := client.Search().Index(index).Source(query).Do(context.Background())
  253. if err != nil {
  254. log.Println("从ES查询出错", err.Error())
  255. return nil
  256. }
  257. if searchResult.Hits != nil {
  258. resNum := len(searchResult.Hits.Hits)
  259. res = make([]map[string]interface{}, resNum)
  260. for i, hit := range searchResult.Hits.Hits {
  261. json.Unmarshal(hit.Source, &res[i])
  262. }
  263. }
  264. }
  265. return &res
  266. }
  267. //func (e *Elastic) GetByIdField(index, itype, id, fields string) *map[string]interface{} {
  268. // client := e.GetEsConn()
  269. // defer e.DestoryEsConn(client)
  270. // if client != nil {
  271. // defer func() {
  272. // if r := recover(); r != nil {
  273. // log.Println("[E]", r)
  274. // for skip := 1; ; skip++ {
  275. // _, file, line, ok := runtime.Caller(skip)
  276. // if !ok {
  277. // break
  278. // }
  279. // go log.Printf("%v,%v\n", file, line)
  280. // }
  281. // }
  282. // }()
  283. // query := `{"query":{"term":{"_id":"` + id + `"}}`
  284. // if len(fields) > 0 {
  285. // query = query + `,"_source":[` + fields + `]`
  286. // }
  287. // query = query + "}"
  288. // searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
  289. // if err != nil {
  290. // log.Println("从ES查询出错", err.Error())
  291. // return nil
  292. // }
  293. // var res map[string]interface{}
  294. // if searchResult.Hits != nil {
  295. // resNum := len(searchResult.Hits.Hits)
  296. // if resNum == 1 {
  297. // res = make(map[string]interface{})
  298. // for _, hit := range searchResult.Hits.Hits {
  299. // json.Unmarshal(*hit.Source., &res)
  300. // }
  301. // return &res
  302. // }
  303. // }
  304. // }
  305. // return nil
  306. //}
  307. func (e *Elastic) Count(index string, query interface{}) int64 {
  308. client := e.GetEsConn()
  309. defer e.DestoryEsConn(client)
  310. if client != nil {
  311. defer func() {
  312. if r := recover(); r != nil {
  313. log.Println("[E]", r)
  314. for skip := 1; ; skip++ {
  315. _, file, line, ok := runtime.Caller(skip)
  316. if !ok {
  317. break
  318. }
  319. go log.Printf("%v,%v\n", file, line)
  320. }
  321. }
  322. }()
  323. var qq es.Query
  324. if qi, ok2 := query.(es.Query); ok2 {
  325. qq = qi
  326. }
  327. n, err := client.Count(index).Query(qq).Do(context.Background())
  328. if err != nil {
  329. log.Println("统计出错", err.Error())
  330. }
  331. return n
  332. }
  333. return 0
  334. }
  335. //更新一个字段
  336. //func (e *Elastic) BulkUpdateArr(index, itype string, update []map[string]string) {
  337. // client := e.GetEsConn()
  338. // defer e.DestoryEsConn(client)
  339. // if client != nil {
  340. // defer func() {
  341. // if r := recover(); r != nil {
  342. // log.Println("[E]", r)
  343. // for skip := 1; ; skip++ {
  344. // _, file, line, ok := runtime.Caller(skip)
  345. // if !ok {
  346. // break
  347. // }
  348. // go log.Printf("%v,%v\n", file, line)
  349. // }
  350. // }
  351. // }()
  352. // for _, data := range update {
  353. // id := data["id"]
  354. // updateStr := data["updateStr"]
  355. // if id != "" && updateStr != "" {
  356. // _, err := client.Update().Index(index).Type(itype).Id(id).Script(updateStr).ScriptLang("groovy").Do()
  357. // if err != nil {
  358. // log.Println("更新检索出错:", err.Error())
  359. // }
  360. // } else {
  361. // log.Println("数据错误")
  362. // }
  363. // }
  364. // }
  365. //}
  366. //更新多个字段
  367. //func (e *Elastic) BulkUpdateMultipleFields(index, itype string, arrs [][]map[string]interface{}) {
  368. // client := e.GetEsConn()
  369. // defer e.DestoryEsConn(client)
  370. // if client != nil {
  371. // defer func() {
  372. // if r := recover(); r != nil {
  373. // log.Println("[E]", r)
  374. // for skip := 1; ; skip++ {
  375. // _, file, line, ok := runtime.Caller(skip)
  376. // if !ok {
  377. // break
  378. // }
  379. // go log.Printf("%v,%v\n", file, line)
  380. // }
  381. // }
  382. // }()
  383. // for _, arr := range arrs {
  384. // id := arr[0]["id"].(string)
  385. // update := arr[1]["update"].([]string)
  386. // for _, str := range update {
  387. // _, err := client.Update().Index(index).Type(itype).Id(id).Script(str).ScriptLang("groovy").Do()
  388. // if err != nil {
  389. // log.Println("更新检索出错:", err.Error())
  390. // }
  391. // }
  392. // }
  393. // }
  394. //}
  395. // UpdateBulk 批量修改文档
  396. func (e *Elastic) UpdateBulk(index string, docs ...[]map[string]interface{}) {
  397. client := e.GetEsConn()
  398. defer e.DestoryEsConn(client)
  399. bulkService := client.Bulk().Index(index).Refresh("true")
  400. //bulkService.Type(itype)
  401. for _, d := range docs {
  402. id := d[0]["_id"].(string)
  403. doc := es.NewBulkUpdateRequest().Id(id).Doc(d[1])
  404. bulkService.Add(doc)
  405. }
  406. _, err := bulkService.Do(context.Background())
  407. if err != nil {
  408. fmt.Printf("UpdateBulk all success err is %v\n", err)
  409. }
  410. //if len(res.Failed()) > 0 {
  411. // fmt.Printf("UpdateBulk all success failed is %v\n", (res.Items[0]))
  412. //}
  413. }
  414. // UpsertBulk 批量修改文档(不存在则插入)
  415. func (e *Elastic) UpsertBulk(ctx context.Context, index string, ids []string, docs []interface{}) error {
  416. client := e.GetEsConn()
  417. defer e.DestoryEsConn(client)
  418. bulkService := client.Bulk().Index(index).Refresh("true")
  419. //bulkService.Type("bidding")
  420. for i := range ids {
  421. doc := es.NewBulkUpdateRequest().Id(ids[i]).Doc(docs[i]).Upsert(docs[i])
  422. bulkService.Add(doc)
  423. }
  424. res, err := bulkService.Do(context.Background())
  425. if err != nil {
  426. return err
  427. }
  428. if len(res.Failed()) > 0 {
  429. return errors.New(res.Failed()[0].Error.Reason)
  430. }
  431. return nil
  432. }
  433. // 批量删除
  434. func (e *Elastic) DeleteBulk(index string, ids []string) {
  435. client := e.GetEsConn()
  436. defer e.DestoryEsConn(client)
  437. bulkService := client.Bulk().Index(index).Refresh("true")
  438. //bulkService.Type("bidding")
  439. for i := range ids {
  440. req := es.NewBulkDeleteRequest().Id(ids[i])
  441. bulkService.Add(req)
  442. }
  443. res, err := bulkService.Do(context.Background())
  444. if err != nil {
  445. fmt.Printf("DeleteBulk success is %v\n", len(res.Succeeded()))
  446. }
  447. }