season_es.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8. "go-common/app/admin/openplatform/sug/model"
  9. "go-common/library/log"
  10. "gopkg.in/olivere/elastic.v5"
  11. "io/ioutil"
  12. )
  13. const (
  14. _seasonIndex = "%s_sug_job_season"
  15. _seasonType = "sug_job_season"
  16. )
  17. // GetSeason get season from es.
  18. func (d *Dao) GetSeason(ctx context.Context, seasonID int64) (season model.Season, err error) {
  19. seasonTermQuery := elastic.NewTermQuery("id", seasonID)
  20. searchResult, err := d.es.Search().Index(fmt.Sprintf(_seasonIndex, d.c.Env)).Type(_seasonType).Query(seasonTermQuery).From(0).Size(1).Timeout("1s").Do(ctx)
  21. if err != nil {
  22. log.Error("es search error(%v)", err)
  23. return
  24. }
  25. if searchResult.Hits.TotalHits > 0 {
  26. for _, hit := range searchResult.Hits.Hits {
  27. err = json.Unmarshal(*hit.Source, &season)
  28. if err != nil {
  29. log.Error("json.Unmarshal err(%v)", err)
  30. return season, err
  31. }
  32. }
  33. }
  34. return
  35. }
  36. // SeasonList search season list.
  37. func (d *Dao) SeasonList(ctx context.Context, params *model.SourceSearch) (list []model.Season, err error) {
  38. query := elastic.NewBoolQuery()
  39. matchQuery := elastic.NewMatchQuery("title", params.Keyword).Fuzziness("40")
  40. sid, _ := strconv.Atoi(params.Keyword)
  41. termQuery := elastic.NewMatchQuery("id", sid).Boost(100)
  42. query.Should(matchQuery)
  43. query.Should(termQuery)
  44. searchResult, err := d.es.Search().Index(fmt.Sprintf(_seasonIndex, d.c.Env)).Type(_seasonType).Query(query).From(0).Size(10).Timeout("1s").Do(ctx)
  45. if err != nil {
  46. return
  47. }
  48. list = []model.Season{}
  49. if searchResult.Hits.TotalHits > 0 {
  50. var season model.Season
  51. for _, hit := range searchResult.Hits.Hits {
  52. err := json.Unmarshal(*hit.Source, &season)
  53. if err != nil {
  54. log.Error("json.Unmarshal error(%v)", err)
  55. continue
  56. }
  57. list = append(list, season)
  58. }
  59. }
  60. return
  61. }
  62. // ItemList mall items list from http.
  63. func (d *Dao) ItemList(ctx context.Context, params *model.SourceSearch) (itemsList []model.Items, err error) {
  64. query := make(map[string]interface{})
  65. query["pageNum"] = params.PageNum
  66. query["pageSize"] = params.PageSize
  67. query["shopId"] = 0
  68. query["keyword"] = params.Keyword
  69. jsonQuery, _ := json.Marshal(query)
  70. resp, err := d.client.Post(d.c.URL.ItemSearch, "application/json", bytes.NewReader(jsonQuery))
  71. if err != nil {
  72. log.Error("Request error(%v)", err)
  73. return
  74. }
  75. HTTPResponse := model.HTTPResponse{}
  76. bodyJSON, _ := ioutil.ReadAll(resp.Body)
  77. if err = json.Unmarshal(bodyJSON, &HTTPResponse); err != nil {
  78. log.Error("json.Unmarshal error(%v)", err)
  79. }
  80. if HTTPResponse.Code != 0 {
  81. log.Error("Request (%s) search error(%v)", d.c.URL.ItemSearch, err)
  82. return
  83. }
  84. itemsList = HTTPResponse.Data.List
  85. return
  86. }