search.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strings"
  7. "go-common/app/admin/main/aegis/model"
  8. "go-common/library/database/elastic"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _upsertES = "/x/admin/search/upsert"
  14. )
  15. // ResourceES search archives by es.
  16. func (d *Dao) ResourceES(c context.Context, arg *model.SearchParams) (sres *model.SearchRes, err error) {
  17. r := d.es.NewRequest("aegis_resource").Index("aegis_resource").Fields(
  18. "id",
  19. "business_id",
  20. "flow_id",
  21. "oid",
  22. "mid",
  23. "content",
  24. "extra1",
  25. "extra2",
  26. "extra3",
  27. "extra4",
  28. "extra5",
  29. "extra6",
  30. "extra1s",
  31. "extra2s",
  32. "extra3s",
  33. "extra4s",
  34. "extratime1",
  35. "octime",
  36. "ptime",
  37. "metadata",
  38. "note",
  39. "reject_reason",
  40. "reason_id",
  41. "state",
  42. "ctime",
  43. ).OrderScoreFirst(false)
  44. escm := model.EsCommon{
  45. Ps: arg.Ps,
  46. Pn: arg.Pn,
  47. Order: "ctime",
  48. Sort: strings.ToLower(arg.CtimeOrder),
  49. }
  50. if escm.Sort != "asc" && escm.Sort != "desc" {
  51. escm.Sort = "desc"
  52. }
  53. setESParams(r, arg, escm)
  54. if arg.KeyWord != "" { //描述
  55. arg.KeyWord = strings.Replace(arg.KeyWord, ",", ",", -1)
  56. r.WhereLike([]string{"content"}, strings.Split(arg.KeyWord, ","), true, elastic.LikeLevelHigh)
  57. }
  58. log.Info("ResourceES params(%s)", r.Params())
  59. sres = &model.SearchRes{}
  60. if err = r.Scan(c, sres); err != nil {
  61. log.Error("ResourceES r.Scan params(%s)|error(%v)", r.Params(), err)
  62. return
  63. }
  64. arg.Pn = sres.Page.Num
  65. arg.Ps = sres.Page.Size
  66. arg.Total = sres.Page.Total
  67. return
  68. }
  69. //UpsertES 更新搜索
  70. func (d *Dao) UpsertES(c context.Context, rsc []*model.UpsertItem) (err error) {
  71. if len(rsc) == 0 {
  72. return
  73. }
  74. items := []*model.UpsertItem{}
  75. for _, item := range rsc {
  76. if item == nil || item.ID <= 0 {
  77. continue
  78. }
  79. items = append(items, item)
  80. }
  81. data := map[string][]*model.UpsertItem{
  82. "aegis_resource": items,
  83. }
  84. datab, err := json.Marshal(data)
  85. if err != nil {
  86. log.Error("UpsertES json.Marshal error(%v) resource(%+v)", err, rsc)
  87. return err
  88. }
  89. res := new(struct {
  90. Code int `json:"code"`
  91. Message string `json:"message"`
  92. })
  93. params := url.Values{}
  94. params.Set("business", "aegis_resource")
  95. params.Set("insert", "false")
  96. params.Set("data", string(datab))
  97. if err = d.clientW.Post(c, d.c.Host.Manager+_upsertES, "", params, res); err != nil {
  98. log.Error("UpsertES d.clientW.Post error(%v) params(%+v)", err, params)
  99. return
  100. }
  101. if res.Code != ecode.OK.Code() {
  102. log.Error("UpsertES d.clientW.Post failed, response(%+v) params(%+v)", res, params)
  103. return
  104. }
  105. log.Info("response(%+v) url=%s%s?%s", res, d.c.Host.Manager, _upsertES, params.Encode())
  106. return
  107. }