season_es.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/job/openplatform/open-sug/model"
  7. "go-common/library/log"
  8. "gopkg.in/olivere/elastic.v5"
  9. )
  10. // SeasonData
  11. func (d *Dao) SeasonData(c context.Context, item *model.Item) (scoreSlice []model.Score, err error) {
  12. var searchResult *elastic.SearchResult
  13. query := elastic.NewMultiMatchQuery(item.Keywords, "title", "alias", "alias_search", "actors^1.25")
  14. searchResult, err = d.es.Search().
  15. Index(fmt.Sprintf("%s_%s", d.c.Env, d.c.ElasticSearch.Season.Index)).
  16. Type(d.c.ElasticSearch.Season.Type).
  17. Query(query).
  18. Timeout(d.c.ElasticSearch.Timeout).
  19. Do(c)
  20. if err != nil {
  21. return
  22. }
  23. if searchResult.TotalHits() > 0 {
  24. wishCount, _ := d.WishCount(c, item)
  25. commentCount, _ := d.CommentCount(c, item)
  26. salesCount, _ := d.SalesCount(c, item)
  27. for _, s := range searchResult.Hits.Hits {
  28. seasonJson, _ := s.Source.MarshalJSON()
  29. season := model.EsSeason{}
  30. json.Unmarshal(seasonJson, &season)
  31. scoreSlice = append(scoreSlice, model.Score{SeasonID: s.Id, Score: *s.Score, SeasonName: season.Title})
  32. switch {
  33. case wishCount > d.ItemWishMax[s.Id]:
  34. d.ItemWishMax[s.Id] = wishCount
  35. case wishCount != 0 && wishCount < d.ItemWishMin[s.Id]:
  36. d.ItemWishMin[s.Id] = wishCount
  37. case d.ItemWishMin[s.Id] == 0:
  38. d.ItemWishMin[s.Id] = wishCount
  39. }
  40. switch {
  41. case commentCount > d.ItemCommentMax[s.Id]:
  42. d.ItemCommentMax[s.Id] = commentCount
  43. case commentCount != 0 && commentCount < d.ItemCommentMin[s.Id]:
  44. d.ItemCommentMin[s.Id] = commentCount
  45. case d.ItemCommentMin[s.Id] == 0:
  46. d.ItemCommentMin[s.Id] = commentCount
  47. }
  48. switch {
  49. case salesCount > d.ItemSalesMax[s.Id]:
  50. d.ItemSalesMax[s.Id] = salesCount
  51. case salesCount != 0 && salesCount < d.ItemSalesMin[s.Id]:
  52. d.ItemSalesMin[s.Id] = salesCount
  53. case d.ItemSalesMin[s.Id] == 0:
  54. d.ItemSalesMin[s.Id] = salesCount
  55. }
  56. }
  57. }
  58. return
  59. }
  60. // Index ...
  61. func (d *Dao) Index(ctx context.Context, index, typ string, id string, data interface{}) {
  62. resp, err := d.es.Index().Index(index).Type(typ).BodyJson(data).Id(id).Do(ctx)
  63. if err != nil {
  64. log.Error("索引写入失败 index(%s) type(%s) error(%v)", index, typ, err)
  65. return
  66. }
  67. if resp.Result != "" {
  68. log.Info("index(%s) type(%s) 创建成功", resp.Index, resp.Type)
  69. } else {
  70. log.Info("index(%s) type(%s) 更新成功", resp.Index, resp.Type)
  71. }
  72. }
  73. // IndexExists ...
  74. func (d *Dao) IndexExists(ctx context.Context, index string) bool {
  75. e, err := d.es.IndexExists(index).Do(ctx)
  76. if err != nil {
  77. log.Error("检查索引是否存在出错 IndexExists(%s) error(%v)", index, err)
  78. }
  79. return e
  80. }
  81. // CreateIndex ...
  82. func (d *Dao) CreateIndex(ctx context.Context, name string, mapping string) bool {
  83. resp, err := d.es.CreateIndex(name).BodyString(mapping).Do(ctx)
  84. if err != nil {
  85. log.Error("创建索引出错 CreateIndex(%s) error(%v)", name, err)
  86. return false
  87. }
  88. if !resp.Acknowledged {
  89. log.Error("创建索引失败 index(%s)", name)
  90. }
  91. return resp.Acknowledged
  92. }