Skip to content

Commit

Permalink
Merge pull request #139 from OpenTreeHole/floor
Browse files Browse the repository at this point in the history
Load Floors Without Sensitive Mention
  • Loading branch information
JingYiJun authored Apr 7, 2024
2 parents 12ea068 + 1face70 commit af04c23
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
8 changes: 2 additions & 6 deletions apis/floor/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func ListFloorsInAHole(c *fiber.Ctx) error {
return err
}
result := querySet.Order(query.OrderBy + " " + query.Sort).
Preload("Mention").
Find(&floors)
if result.Error != nil {
return result.Error
Expand Down Expand Up @@ -97,7 +96,6 @@ func ListFloorsOld(c *fiber.Ctx) error {
}

result := querySet.
Preload("Mention").
Find(&floors)
if result.Error != nil {
return result.Error
Expand Down Expand Up @@ -128,7 +126,7 @@ func GetFloor(c *fiber.Ctx) error {
if err != nil {
return err
}
result := querySet.Preload("Mention").
result := querySet.
First(&floor, floorID)
if result.Error != nil {
return result.Error
Expand Down Expand Up @@ -663,10 +661,8 @@ func ListReplyFloors(c *fiber.Ctx) error {
return err
}
result := querySet.
Where("ranking <> ?", 0).
Order(query.OrderBy+" "+query.Sort).
Where("user_id = ?", userID).
Preload("Mention").
Where("user_id = ? and ranking <> ?", userID, 0).
Find(&floors)
if result.Error != nil {
return result.Error
Expand Down
4 changes: 2 additions & 2 deletions apis/floor/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func SearchFloors(c *fiber.Ctx) error {
return err
}

floors, err := Search(query.Search, query.Size, query.Offset, query.Accurate)
floors, err := Search(c, query.Search, query.Size, query.Offset, query.Accurate)
if err != nil {
return err
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func SearchFloorsOld(c *fiber.Ctx, query *ListOldModel) error {
return common.Forbidden("树洞流量激增,搜索功能暂缓开放")
}

floors, err := Search(query.Search, query.Size, query.Offset, false)
floors, err := Search(c, query.Search, query.Size, query.Offset, false)
if err != nil {
return err
}
Expand Down
23 changes: 15 additions & 8 deletions models/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
"strconv"
"time"

Expand Down Expand Up @@ -61,9 +62,9 @@ type FloorModel struct {
Content string `json:"content"`
}

func Search(keyword string, size, offset int, accurate bool) (Floors, error) {
func Search(c *fiber.Ctx, keyword string, size, offset int, accurate bool) (Floors, error) {
if ES == nil {
return SearchOld(keyword, size, offset)
return SearchOld(c, keyword, size, offset)
}

var query types.Query
Expand Down Expand Up @@ -133,22 +134,28 @@ func Search(keyword string, size, offset int, accurate bool) (Floors, error) {
}
log.Info().Ints("floor_ids", floorIDs).Msg("search response")

err = DB.Preload("Mention").Find(&floors, floorIDs).Error
querySet, err := MakeFloorQuerySet(c)
if err != nil {
return nil, err
}
err = querySet.Find(&floors, floorIDs).Error
if err != nil {
return nil, err
}

return utils.OrderInGivenOrder(floors, floorIDs), nil
}

func SearchOld(keyword string, size, offset int) (Floors, error) {
func SearchOld(c *fiber.Ctx, keyword string, size, offset int) (Floors, error) {
floors := Floors{}
result := DB.
querySet, err := floors.MakeQuerySet(nil, &offset, &size, c)
if err != nil {
return nil, err
}
result := querySet.
Where("content like ?", "%"+keyword+"%").
Where("hole_id in (?)", DB.Table("hole").Select("id").Where("hidden = false")).
Where("(is_sensitive = 0 AND is_actual_sensitive IS NULL) OR is_actual_sensitive = 0").
Offset(offset).Limit(size).Order("id desc").
Preload("Mention").Find(&floors)
Order("id desc").Find(&floors)
return floors, result.Error
}

Expand Down
5 changes: 3 additions & 2 deletions models/floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ func MakeFloorQuerySet(c *fiber.Ctx) (*gorm.DB, error) {
return nil, err
}
if user.IsAdmin {
return DB, nil
return DB.Preload("Mention"), nil
} else {
userID, err := common.GetUserID(c)
if err != nil {
return nil, err
}
return DB.Where("(is_sensitive = 0 AND is_actual_sensitive IS NULL) OR is_actual_sensitive = 0 OR user_id = ?", userID), nil
return DB.Where("(is_sensitive = 0 AND is_actual_sensitive IS NULL) OR is_actual_sensitive = 0 OR user_id = ?", userID).
Preload("Mention", "(is_sensitive = 0 AND is_actual_sensitive IS NULL) OR is_actual_sensitive = 0 OR user_id = ?", userID), nil
}
}

Expand Down
49 changes: 34 additions & 15 deletions models/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,40 @@ func loadFloors(holes Holes) error {
// load all floors with holeIDs and ranking < HoleFloorSize or the last floor
// sorted by hole_id asc first and ranking asc second
var floors Floors
err := DB.
Raw(
// using file sort
`SELECT * FROM (? UNION ?) f ORDER BY hole_id, ranking`,
// use index(idx_hole_ranking), type range, use MRR
DB.Model(&Floor{}).Where("hole_id in ? and ranking < ?", holeIDs, config.Config.HoleFloorSize),

// UNION, remove duplications
// use index(idx_hole_ranking), type eq_ref
DB.Model(&Floor{}).Where(
"(hole_id, ranking) in (?)",
// use index(PRIMARY), type range
DB.Model(&Hole{}).Select("id", "reply").Where("id in ?", holeIDs),
),
).Scan(&floors).Error
err := DB.Raw(`select * from (
SELECT id, content, anonyname, created_at, updated_at, deleted, fold, hole_id, user_id, special_tag,
reply_to, modified, ranking, dislike, is_sensitive, is_actual_sensitive, `+"`like`"+`FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY hole_id ORDER BY id) AS row_num
FROM floor
WHERE hole_id IN ? and ((is_sensitive = 0 AND is_actual_sensitive IS NULL) OR is_actual_sensitive = 0)
) AS ranked_floors
WHERE row_num <= ?
Union
SELECT id, content, anonyname, created_at, updated_at, deleted, fold, hole_id, user_id, special_tag,
reply_to, modified, ranking, dislike, is_sensitive, is_actual_sensitive, `+"`like`"+`FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY hole_id ORDER BY id desc) AS row_num
FROM floor
WHERE hole_id IN ? and ((is_sensitive = 0 AND is_actual_sensitive IS NULL) OR is_actual_sensitive = 0)
) AS ranked_floors
WHERE row_num = 1
) f order by hole_id, id`, holeIDs, config.Config.HoleFloorSize, holeIDs).Scan(&floors).Error
//err := DB.
// Raw(
// // using file sort
// `SELECT * FROM (? UNION ?) f ORDER BY hole_id, ranking`,
// // use index(idx_hole_ranking), type range, use MRR
// DB.Model(&Floor{}).Where("hole_id in ? and ranking < ?", holeIDs, config.Config.HoleFloorSize),
//
// // UNION, remove duplications
// // use index(idx_hole_ranking), type eq_ref
// DB.Model(&Floor{}).Where(
// "(hole_id, ranking) in (?)",
// // use index(PRIMARY), type range
// DB.Model(&Hole{}).Select("id", "reply").Where("id in ?", holeIDs),
// ),
// ).Scan(&floors).Error
if err != nil {
return err
}
Expand Down

0 comments on commit af04c23

Please sign in to comment.