search.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package academy
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/creative/model/academy"
  6. "go-common/library/database/elastic"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. // ArchivesWithES search archives by es.
  11. func (d *Dao) ArchivesWithES(c context.Context, aca *academy.EsParam) (res *academy.SearchResult, err error) {
  12. r := d.es.NewRequest("academy_archive").Index("academy_archive").Fields("oid", "tid", "business")
  13. if aca.Business > 0 {
  14. r.WhereEq("business", aca.Business)
  15. }
  16. r.WhereEq("state", 0).Pn(aca.Pn).Ps(aca.Ps) //state 创作学院稿件状态
  17. r.WhereEq("arc_state", 0) //arc_state 原始稿件状态(视频、专栏)
  18. r.WhereEq("deleted_time", 0) //过滤删除的专栏
  19. if aca.Seed > 0 {
  20. r.OrderRandomSeed(time.Unix(aca.Seed, 0).Format("2006-01-02 15:04:05")) //随机推荐
  21. }
  22. if aca.Keyword != "" {
  23. r.WhereLike([]string{"title", "tid_name"}, []string{aca.Keyword}, true, "low").Highlight(true)
  24. }
  25. if aca.Order != "" {
  26. r.Order(aca.Order, "desc").OrderScoreFirst(false) //order: click (最多点击数), fav(最多收藏数), pubtime(最新发布时间), hot(最热值)
  27. }
  28. if aca.Duration > 0 { //h5 时长筛选 1(1-10分钟 ) 2(10-30分钟) 3(30-60分钟) 4(60分钟+)
  29. switch aca.Duration {
  30. case 1:
  31. r.WhereRange("duration", 1*60, 10*60, elastic.RangeScopeLcRc)
  32. case 2:
  33. r.WhereRange("duration", 10*60, 30*60, elastic.RangeScopeLcRc)
  34. case 3:
  35. r.WhereRange("duration", 30*60, 60*60, elastic.RangeScopeLcRc)
  36. case 4:
  37. r.WhereRange("duration", 60*60, nil, elastic.RangeScopeLcRo)
  38. }
  39. if aca.Order == "" {
  40. r.Order("duration", "desc")
  41. }
  42. }
  43. if len(aca.TidsMap) > 0 {
  44. for _, v := range aca.TidsMap {
  45. cmb := &elastic.Combo{}
  46. tids := make([]interface{}, 0, len(v))
  47. for _, tid := range v {
  48. tids = append(tids, tid)
  49. }
  50. cmb.ComboIn([]map[string][]interface{}{
  51. {"tid": tids},
  52. }).MinIn(1).MinAll(1)
  53. r.WhereCombo(cmb)
  54. }
  55. }
  56. res = &academy.SearchResult{}
  57. log.Info("ArchivesWithES r.Scan params(%s)", r.Params())
  58. if err = r.Scan(c, res); err != nil {
  59. log.Error("ArchivesWithES r.Scan|error(%v)", err)
  60. err = ecode.CreativeSearchErr
  61. return
  62. }
  63. return
  64. }
  65. //Keywords get all keywords.
  66. func (d *Dao) Keywords(c context.Context) (res []*academy.SearchKeywords, err error) {
  67. _getKWSQL := "SELECT id, rank, parent_id, state, name, comment FROM academy_search_keywords WHERE state=0 ORDER BY rank ASC"
  68. rows, err := d.db.Query(c, _getKWSQL)
  69. if err != nil {
  70. log.Error("Keywords d.db.Query error(%v)", err)
  71. return
  72. }
  73. defer rows.Close()
  74. res = make([]*academy.SearchKeywords, 0)
  75. for rows.Next() {
  76. o := &academy.SearchKeywords{}
  77. if err = rows.Scan(&o.ID, &o.Rank, &o.ParentID, &o.State, &o.Name, &o.Comment); err != nil {
  78. log.Error("Keywords rows.Scan error(%v)", err)
  79. return
  80. }
  81. if o.Name == "" {
  82. continue
  83. }
  84. res = append(res, o)
  85. }
  86. return
  87. }