search_service.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "net/url"
  6. "time"
  7. "errors"
  8. "gopkg.in/mgo.v2"
  9. "gopkg.in/mgo.v2/bson"
  10. "xiaoniaokuaiyan.com/xiaoniao/config"
  11. "xiaoniaokuaiyan.com/xiaoniao/search"
  12. )
  13. type SearchService struct{}
  14. var (
  15. mgoSess *mgo.Session
  16. )
  17. func getMgoDB() *mgo.Database {
  18. var (
  19. mgoURI = config.IniConf.Section("database").Key("mgo.uri").Value()
  20. mgoDatabase = config.IniConf.Section("database").Key("mgo.database").Value()
  21. mgoUser = config.IniConf.Section("database").Key("mgo.user").Value()
  22. mgoPassword = config.IniConf.Section("database").Key("mgo.password").Value()
  23. )
  24. mgoURI = fmt.Sprintf("%s:%s@%s/%s", mgoUser, url.QueryEscape(mgoPassword), mgoURI, mgoDatabase)
  25. var err error
  26. if mgoSess == nil {
  27. mgoSess, err = mgo.Dial(mgoURI)
  28. if err != nil {
  29. log.Println(err)
  30. return nil
  31. }
  32. }
  33. return mgoSess.Copy().DB(mgoDatabase)
  34. }
  35. func (srv *SearchService) Query(param *search.QueryParam, uid int) (interface{}, error) {
  36. go func() {
  37. db := getMgoDB()
  38. if db == nil {
  39. return
  40. }
  41. defer db.Session.Close()
  42. /*var targetObj = bson.M{}
  43. err := db.C("search_history").Find(bson.M{
  44. "user_id": uid,
  45. "text": text,
  46. }).One(targetObj)
  47. if err != nil && err != mgo.ErrNotFound {
  48. fmt.Println(err)
  49. } else {
  50. if num, ok := targetObj["num"]; !ok {
  51. db.C("search_product").Insert(bson.M{
  52. "_id": cityId,
  53. "text": text,
  54. "num": 1,
  55. })
  56. } else {
  57. db.C("search_product").Upsert(bson.M{
  58. "city_id": cityId,
  59. "text": text,
  60. }, bson.M{"$set": bson.M{"num": num.(int) + 1}})
  61. }
  62. }*/
  63. err := db.C("search_text_log").Insert(bson.M{
  64. "city_id": param.CityId,
  65. "text": param.Text,
  66. "user_id": uid,
  67. "created_at": time.Now().Format("2006-01-02 15:04:05"),
  68. })
  69. if err != nil {
  70. fmt.Println(err)
  71. return
  72. }
  73. }()
  74. return search.Query(param)
  75. }
  76. func (srv *SearchService) GetTip(text string, cityId int, isZFB bool) (interface{}, error) {
  77. return search.GetSearchTips(text, cityId, isZFB)
  78. }
  79. func (srv *SearchService) GetTopKeyword(cityId int, sn int) (interface{}, error) {
  80. if sn > 20 {
  81. sn = 20
  82. }
  83. if sn == 0 {
  84. sn = 10
  85. }
  86. var querySelector interface{} = bson.M{}
  87. if cityId > 0 {
  88. querySelector = bson.M{
  89. "city_id": cityId,
  90. }
  91. }
  92. return getTopKeyword(querySelector, sn)
  93. }
  94. func (srv *SearchService) GetUserKeyword(uid int, sn int) (interface{}, error) {
  95. if uid < 0 {
  96. return nil, errors.New("1::user id is required")
  97. }
  98. if sn > 20 {
  99. sn = 20
  100. }
  101. if sn == 0 {
  102. sn = 10
  103. }
  104. var querySelector = bson.M{
  105. "user_id": uid,
  106. "text": bson.M{"$ne": ""},
  107. }
  108. db := getMgoDB()
  109. defer db.Session.Close()
  110. var pipes = []bson.M{
  111. bson.M{
  112. "$match": querySelector,
  113. },
  114. bson.M{
  115. "$sort": bson.M{
  116. "created_at": -1,
  117. },
  118. },
  119. bson.M{
  120. "$group": bson.M{
  121. "_id": "$text",
  122. "created_at": bson.M{
  123. "$max": "$created_at",
  124. },
  125. },
  126. },
  127. bson.M{
  128. "$sort": bson.M{
  129. "created_at": -1,
  130. },
  131. },
  132. bson.M{
  133. "$project": bson.M{"_id": 0, "text": "$_id"},
  134. },
  135. bson.M{
  136. "$limit": sn,
  137. },
  138. }
  139. keywords := []bson.M{}
  140. err := db.C("search_text_log").Pipe(pipes).All(&keywords) //Find(querySelector).Select(bson.M{"text": 1, "_id": 0}).Sort("-created_at").Limit(sn).All(&keywords)
  141. return keywords, err
  142. }
  143. func getTopKeyword(querySelector interface{}, size int) ([]bson.M, error) {
  144. db := getMgoDB()
  145. defer db.Session.Close()
  146. keywords := []bson.M{}
  147. var pipes = []bson.M{
  148. bson.M{
  149. "$match": querySelector,
  150. },
  151. bson.M{
  152. "$group": bson.M{
  153. "_id": "$text",
  154. "count": bson.M{
  155. "$sum": 1,
  156. },
  157. },
  158. },
  159. bson.M{
  160. "$sort": bson.M{
  161. "count": -1,
  162. },
  163. },
  164. bson.M{
  165. "$project": bson.M{"_id": 0, "text": "$_id"},
  166. },
  167. bson.M{
  168. "$limit": size,
  169. },
  170. }
  171. err := db.C("search_text_log").Pipe(pipes).All(&keywords) //.Find(querySelector).Select(bson.M{"text": 1, "_id": 0}).Sort("-num").Limit(sn).All(&keywords)
  172. return keywords, err
  173. }