search.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/creative/model/academy"
  5. "go-common/library/database/elastic"
  6. "go-common/library/log"
  7. )
  8. // ArchivesWithES search archives by es.
  9. func (d *Dao) ArchivesWithES(c context.Context, aca *academy.EsParam) (res *academy.SearchResult, err error) {
  10. r := d.es.NewRequest("academy_archive").Fields("oid", "tid")
  11. r.Index("academy_archive").WhereEq("state", academy.StateNormal).WhereEq("business", aca.Business).Pn(aca.Pn).Ps(aca.Ps).Order("id", "desc")
  12. if aca.Business == academy.BusinessForArchvie && aca.State != academy.DefaultState { //arc_state 稿件原始状态 state 创作学院稿件状态
  13. r.WhereEq("arc_state", aca.State)
  14. }
  15. if aca.Business == academy.BusinessForArticle { //只筛选开放浏览的专栏
  16. r.WhereEq("arc_state", 0).WhereEq("deleted_time", 0)
  17. }
  18. if aca.Keyword != "" {
  19. r.WhereLike([]string{"title", "tid_name"}, []string{aca.Keyword}, true, "low")
  20. }
  21. if aca.Uname != "" {
  22. r.WhereLike([]string{"uname"}, []string{aca.Uname}, true, "low")
  23. }
  24. if aca.OID > 0 {
  25. r.WhereEq("oid", aca.OID)
  26. }
  27. if len(aca.TidsMap) > 0 {
  28. for _, v := range aca.TidsMap {
  29. cmb := &elastic.Combo{}
  30. tids := make([]interface{}, 0, len(v))
  31. for _, tid := range v {
  32. tids = append(tids, tid)
  33. }
  34. cmb.ComboIn([]map[string][]interface{}{
  35. {"tid": tids},
  36. }).MinIn(1).MinAll(1)
  37. r.WhereCombo(cmb)
  38. }
  39. }
  40. if aca.Business == academy.BusinessForArchvie {
  41. if aca.Copyright != 3 {
  42. r.WhereEq("copyright", aca.Copyright) //投稿类型
  43. } else {
  44. r.WhereIn("copyright", []int8{0, 1, 2})
  45. }
  46. }
  47. res = &academy.SearchResult{}
  48. if err = r.Scan(c, res); err != nil {
  49. log.Error("ArchivesWithES r.Scan params(%s)|error(%v)", r.Params(), err)
  50. }
  51. log.Info("ArchivesWithES params(%s)", r.Params())
  52. return
  53. }