Better search api allow searching with tags or categories only

This commit is contained in:
LittleSheep 2024-12-08 14:09:34 +08:00
parent e1aad5519c
commit d49c960be5
2 changed files with 17 additions and 8 deletions

View File

@ -40,11 +40,11 @@ func universalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) {
tx = tx.Where("publisher_id = ?", author.ID) tx = tx.Where("publisher_id = ?", author.ID)
} }
if len(c.Query("category")) > 0 { if len(c.Query("categories")) > 0 {
tx = services.FilterPostWithCategory(tx, c.Query("category")) tx = services.FilterPostWithCategory(tx, c.Query("categories"))
} }
if len(c.Query("tag")) > 0 { if len(c.Query("tags")) > 0 {
tx = services.FilterPostWithTag(tx, c.Query("tag")) tx = services.FilterPostWithTag(tx, c.Query("tags"))
} }
if len(c.Query("type")) > 0 { if len(c.Query("type")) > 0 {
@ -103,8 +103,8 @@ func searchPost(c *fiber.Ctx) error {
tx := database.C tx := database.C
probe := c.Query("probe") probe := c.Query("probe")
if len(probe) == 0 { if len(probe) == 0 && len(c.Query("tags")) == 0 && len(c.Query("categories")) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "probe is required") return fiber.NewError(fiber.StatusBadRequest, "search term (probe, tags or categories) is required")
} }
tx = services.FilterPostWithFuzzySearch(tx, probe) tx = services.FilterPostWithFuzzySearch(tx, probe)

View File

@ -9,6 +9,7 @@ import (
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"time" "time"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/database"
@ -59,13 +60,17 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB { func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB {
return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id"). return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id").
Joins("JOIN categories ON categories.id = post_categories.category_id"). Joins("JOIN categories ON categories.id = post_categories.category_id").
Where("categories.alias = ?", alias) Where("categories.alias IN ?", strings.Split(alias, ",")).
Distinct("posts.id")
} }
func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB { func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB {
aliases := strings.Split(alias, ",")
return tx.Joins("JOIN post_tags ON posts.id = post_tags.post_id"). return tx.Joins("JOIN post_tags ON posts.id = post_tags.post_id").
Joins("JOIN tags ON tags.id = post_tags.tag_id"). Joins("JOIN tags ON tags.id = post_tags.tag_id").
Where("tags.alias = ?", alias) Where("tags.alias IN ?", aliases).
Group("posts.id").
Having("COUNT(DISTINCT tags.id) = ?", len(aliases))
} }
func FilterPostWithType(tx *gorm.DB, t string) *gorm.DB { func FilterPostWithType(tx *gorm.DB, t string) *gorm.DB {
@ -103,6 +108,10 @@ func FilterPostDraft(tx *gorm.DB) *gorm.DB {
} }
func FilterPostWithFuzzySearch(tx *gorm.DB, probe string) *gorm.DB { func FilterPostWithFuzzySearch(tx *gorm.DB, probe string) *gorm.DB {
if len(probe) == 0 {
return tx
}
probe = "%" + probe + "%" probe = "%" + probe + "%"
return tx. return tx.
Where("? AND body->>'content' ILIKE ?", gorm.Expr("body ? 'content'"), probe). Where("? AND body->>'content' ILIKE ?", gorm.Expr("body ? 'content'"), probe).