Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package hotsearchmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var db *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
db = mongos.Shop.Coll(TN.HotSearch)
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: FN.HotSearch.Keyword, Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.HotSearch.Count, Value: -1},
|
||||
{Key: FN.HotSearch.UpdatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(hotsearchmod:Init) Create HotSearch Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// NormalizeKeyword 去掉首尾空白、压缩中间空格,并限制长度。
|
||||
func NormalizeKeyword(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
fields := strings.FieldsFunc(raw, unicode.IsSpace)
|
||||
raw = strings.Join(fields, " ")
|
||||
if utf8.RuneCountInString(raw) > MaxKeywordRunes {
|
||||
raw = string([]rune(raw)[:MaxKeywordRunes])
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// IncrByKeyword 按搜索词累加次数,不存在则插入。
|
||||
func IncrByKeyword(ctx context.Context, keyword string) *srvdef.SrvErr {
|
||||
keyword = NormalizeKeyword(keyword)
|
||||
if keyword == "" {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
filter := bson.M{FN.HotSearch.Keyword: keyword}
|
||||
update := bson.M{
|
||||
"$inc": bson.M{FN.HotSearch.Count: 1},
|
||||
"$set": bson.M{FN.HotSearch.UpdatedAt: now},
|
||||
"$setOnInsert": bson.M{
|
||||
FN.HotSearch.Id: mongos.Lid.HotSearch.Get(),
|
||||
FN.HotSearch.Keyword: keyword,
|
||||
FN.HotSearch.CreatedAt: now,
|
||||
},
|
||||
}
|
||||
if _, err := db.UpsertOne(ctx, filter, update); err != nil {
|
||||
if _, retryErr := db.UpdateOne(ctx, filter, bson.M{
|
||||
"$inc": bson.M{FN.HotSearch.Count: 1},
|
||||
"$set": bson.M{FN.HotSearch.UpdatedAt: now},
|
||||
}); retryErr != nil {
|
||||
log.Warn("hotsearchmod:IncrByKeyword failed", log.E(err), log.E(retryErr), log.Any("keyword", keyword))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindTopHotSearch(ctx context.Context, limit int64) (HotSearchList, *srvdef.SrvErr) {
|
||||
if limit <= 0 {
|
||||
limit = TopLimit
|
||||
}
|
||||
opts := options.Find().
|
||||
SetSort(bson.D{
|
||||
{Key: FN.HotSearch.Count, Value: -1},
|
||||
{Key: FN.HotSearch.UpdatedAt, Value: -1},
|
||||
{Key: FN.HotSearch.Id, Value: -1},
|
||||
}).
|
||||
SetLimit(limit).
|
||||
SetProjection(bson.D{
|
||||
{Key: FN.HotSearch.Id, Value: 1},
|
||||
{Key: FN.HotSearch.Keyword, Value: 1},
|
||||
{Key: FN.HotSearch.Count, Value: 1},
|
||||
})
|
||||
data := make(HotSearchList, 0)
|
||||
if err := db.Find(ctx, &data, bson.M{}, opts); err != nil {
|
||||
log.Warn("hotsearchmod:FindTopHotSearch db.Find failed", log.E(err))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package hotsearchmod
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeKeyword(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want string
|
||||
}{
|
||||
{name: "空串", raw: " ", want: ""},
|
||||
{name: "压缩空白", raw: " 霸道 总裁\t ", want: "霸道 总裁"},
|
||||
{name: "截断长度", raw: stringsRepeat("测", 60), want: stringsRepeat("测", MaxKeywordRunes)},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NormalizeKeyword(tt.raw); got != tt.want {
|
||||
t.Fatalf("NormalizeKeyword() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func stringsRepeat(s string, n int) string {
|
||||
out := make([]rune, 0, n)
|
||||
r := []rune(s)[0]
|
||||
for i := 0; i < n; i++ {
|
||||
out = append(out, r)
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package hotsearchmod
|
||||
|
||||
type TagHotSearch struct {
|
||||
Id string
|
||||
Keyword string
|
||||
Count string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
HotSearch TagHotSearch
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
HotSearch string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.HotSearch.Id = "_id"
|
||||
FN.HotSearch.Keyword = "keyword"
|
||||
FN.HotSearch.Count = "count"
|
||||
FN.HotSearch.CreatedAt = "createdAt"
|
||||
FN.HotSearch.UpdatedAt = "updatedAt"
|
||||
TN.HotSearch = "hot_search"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package hotsearchmod
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
MaxKeywordRunes = 50
|
||||
TopLimit = 10
|
||||
)
|
||||
|
||||
// HotSearch 热搜词
|
||||
// @table:hot_search
|
||||
type HotSearch struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"`
|
||||
Keyword string `json:"keyword" bson:"keyword"` // 搜索词
|
||||
Count int64 `json:"count" bson:"count"` // 搜索次数
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 首次记录时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 最近搜索时间
|
||||
}
|
||||
|
||||
type HotSearchList []*HotSearch
|
||||
Reference in New Issue
Block a user