@@ -0,0 +1,148 @@
|
||||
package searchlogmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/constant"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.SearchLog
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "realm", Value: 1}}, //不会新建 realm 索引
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "realm", Value: 1}, {Key: "keyword", Value: 1}}, //不会新建 realm 索引
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// FindHotSearch 根据搜索量获取热搜排行榜
|
||||
func FindHotSearch(realm constant.RealmType, page commod.Page) ([]SearchCount, error) {
|
||||
p := []M{
|
||||
{
|
||||
"$match": M{"realm": realm},
|
||||
},
|
||||
{
|
||||
"$group": M{"_id": "$keyword", "count": M{"$sum": 1}},
|
||||
},
|
||||
{
|
||||
"$match": M{"_id": M{"$ne": ""}},
|
||||
},
|
||||
{
|
||||
"$sort": bson.D{{Key: "count", Value: -1}},
|
||||
},
|
||||
{
|
||||
"$skip": (page.PageNumber - 1) * page.PageSize,
|
||||
},
|
||||
{
|
||||
"$limit": page.PageSize,
|
||||
},
|
||||
}
|
||||
data := make([]SearchCount, 0, page.PageSize)
|
||||
if err := coll(nil).Aggregate(&data, p); err != nil {
|
||||
log.ZapLog.Error(fmt.Sprintf("models record SearchLog FindSearchCountVid fail error:%+v:", err))
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// InsertMany InsertMany
|
||||
func InsertMany(uid uint64, realm constant.RealmType, keyWords []string) error {
|
||||
now := time.Now()
|
||||
searchLogDoc := make([]SearchLogDoc, 0, len(keyWords))
|
||||
for _, keyword := range keyWords {
|
||||
if keyword == "" {
|
||||
continue
|
||||
}
|
||||
searchLogDoc = append(searchLogDoc, SearchLogDoc{
|
||||
Realm: realm,
|
||||
UID: uid,
|
||||
Keyword: keyword,
|
||||
CreatedAt: now,
|
||||
})
|
||||
}
|
||||
if len(searchLogDoc) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := coll(nil).InsertMany(&searchLogDoc); err != nil {
|
||||
log.ZapLog.Error("SearchLog InsertMany error", log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetKeywordCountMapByTime(start time.Time, end time.Time, realm constant.RealmType) (map[string]int64, error) {
|
||||
pipeline := []M{
|
||||
{
|
||||
"$match": M{
|
||||
"createdAt": M{
|
||||
"$gte": start,
|
||||
"$lt": end,
|
||||
},
|
||||
"realm": realm,
|
||||
},
|
||||
},
|
||||
{
|
||||
"$sortByCount": "$keyword",
|
||||
},
|
||||
}
|
||||
list := []struct {
|
||||
Keyword string `bson:"_id"`
|
||||
Count int64 `bson:"count"`
|
||||
}{}
|
||||
if err := coll(nil).Aggregate(&list, pipeline); err != nil {
|
||||
return nil, fmt.Errorf("GetKeywordCountMapByTime err: %s", err.Error())
|
||||
}
|
||||
m := make(map[string]int64, len(list))
|
||||
for _, v := range list {
|
||||
m[v.Keyword] = v.Count
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// 根据时间段获取搜索日志
|
||||
func GetSearchLogByTimeRange(start time.Time, end time.Time) (data []SearchLog, err error) {
|
||||
var query = bson.M{
|
||||
"createdAt": bson.M{"$gte": start, "$lt": end},
|
||||
}
|
||||
if err = coll(nil).Find(&data, query); err != nil {
|
||||
log.Error("models SearchLog model GetSearchLogByTimeRange error", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteBeforeCreatedAt(t *db.MongoTool, tm time.Time) error {
|
||||
_, err := coll(t).DeleteMany(bson.M{"createdAt": bson.M{"$lt": tm}})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package searchlogmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/constant"
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type (
|
||||
SearchLogDoc = SearchLog
|
||||
|
||||
ObjectID = primitive.ObjectID
|
||||
|
||||
M = bson.M
|
||||
)
|
||||
|
||||
// SearchLog 搜索记录
|
||||
type SearchLog struct {
|
||||
ID ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Realm constant.RealmType `json:"realm" bson:"realm"` //搜索区域 综合:complex 视频:video 用户:user 话题:tag 地点:site
|
||||
UID uint64 `json:"uid" bson:"uid"` //搜索人ID
|
||||
Keyword string `json:"keyword" bson:"keyword"` //搜索的关键字
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //搜索的时间
|
||||
}
|
||||
|
||||
// SearchLog 搜索记录
|
||||
type ESSearchLogSource struct {
|
||||
ID ObjectID `json:"id"`
|
||||
Realm constant.RealmType `json:"realm"` //搜索区域 综合:complex 视频:video 用户:user 话题:tag 地点:site
|
||||
UID uint64 `json:"uid"` //搜索人ID
|
||||
Keyword string `json:"keyword"` //搜索的关键字
|
||||
CreatedAt time.Time `json:"createdAt"` //搜索的时间
|
||||
}
|
||||
|
||||
// SearchCount 热搜视频统计最高多搜索次数的视频
|
||||
type SearchCount struct {
|
||||
KeyWord string `json:"keyWord" bson:"_id"` //关键字
|
||||
Count int `json:"count" bson:"count"` //关键字搜索次数
|
||||
}
|
||||
|
||||
// SearchResp SearchResp
|
||||
type SearchResp struct {
|
||||
TagName string `json:"id" bson:"_id,omitempty"`
|
||||
Count int `json:"count" bson:"count,omitempty"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
Reference in New Issue
Block a user