biddingdeletebyextract.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "go.mongodb.org/mongo-driver/bson"
  4. "log"
  5. util "utils"
  6. "utils/elastic"
  7. "utils/mongodb"
  8. )
  9. //根据抽取表repeat=1,删除es中重复数据
  10. func biddingDelByExtract(data []byte, mapInfo map[string]interface{}) {
  11. defer util.Catch()
  12. q, _ := mapInfo["query"].(map[string]interface{})
  13. if q == nil {
  14. q = map[string]interface{}{
  15. "_id": bson.M{
  16. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  17. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  18. // "$gte": mongodb.StringTOBsonId("5eb18d86511b1203376bd742"),
  19. // "$lte": mongodb.StringTOBsonId("5ebb43c5f2c1a7850bb1337c"),
  20. },
  21. "repeat": 1,
  22. }
  23. }
  24. extractc, _ := extract["collect"].(string)
  25. index, _ := bidding["index"].(string)
  26. itype, _ := bidding["type"].(string)
  27. //extract
  28. extractConn := extractMgo.GetMgoConn()
  29. defer extractMgo.DestoryMongoConn(extractConn)
  30. //查询数据
  31. count, _ := extractConn.DB(extractMgo.DbName).C(extractc).Find(&q).Count()
  32. log.Println("查询语句:", q, "删除同步总数:", count, "elastic库:", index)
  33. extractquery := extractConn.DB(extractMgo.DbName).C(extractc).Find(q).Select(
  34. bson.M{"_id": 1},
  35. ).Sort("_id").Iter()
  36. log.Println("开始迭代...")
  37. i := 0
  38. var n int
  39. var dnum int
  40. for tmp := make(map[string]interface{}); extractquery.Next(tmp); i = i + 1 {
  41. n++
  42. _id := mongodb.BsonIdToSId(tmp["_id"])
  43. if elastic.DelById(index, itype, _id) { //删除
  44. dnum++
  45. }
  46. //if other_index != "" && other_itype != "" {
  47. // bidding_other_es.DelById(other_index, other_itype, _id)
  48. //}
  49. if n%200 == 0 {
  50. log.Println("当前:", n)
  51. }
  52. tmp = make(map[string]interface{})
  53. }
  54. log.Println("共删除:", dnum)
  55. }