Skip to content

Commit

Permalink
feat: support meta tags filter (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Oct 14, 2024
1 parent 46b11a7 commit be0fe62
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions canal/canal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/bangumi/server/internal/pkg/sys"
"github.com/bangumi/server/internal/search"
"github.com/bangumi/server/internal/subject"
"github.com/bangumi/server/internal/tag"
"github.com/bangumi/server/web/session"
)

Expand Down Expand Up @@ -64,6 +65,8 @@ func Main() error {
subject.NewMysqlRepo, search.New, session.NewMysqlRepo, session.New,
driver.NewS3,

tag.NewMysqlRepo,

newEventHandler,
),

Expand Down
4 changes: 4 additions & 0 deletions canal/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/logger/log"
"github.com/bangumi/server/internal/search"
"github.com/bangumi/server/internal/tag"
"github.com/bangumi/server/web/session"
)

Expand All @@ -37,6 +38,7 @@ func newEventHandler(
session session.Manager,
redis rueidis.Client,
stream Stream,
tag tag.Repo,
search search.Client,
s3 *s3.Client,
) *eventHandler {
Expand All @@ -48,6 +50,7 @@ func newEventHandler(
s3: s3,
stream: stream,
log: log.Named("eventHandler"),
tag: tag,
}
}

Expand All @@ -60,6 +63,7 @@ type eventHandler struct {
stream Stream
s3 *s3.Client // optional, check nil before use
redis rueidis.Client
tag tag.Repo
}

func (e *eventHandler) start() error {
Expand Down
1 change: 1 addition & 0 deletions internal/model/subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Subject struct {
Image string
Summary string
Name string
MetaTags string
Date string // first release date
NameCN string
Infobox string
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (c redisCache) Get(ctx context.Context, key string, value any) (bool, error
func (c redisCache) MGet(ctx context.Context, keys []string, result any) error {
results, err := c.ru.Do(ctx, c.ru.B().Mget().Key(keys...).Build()).ToArray()
if err != nil {
//nolint:errorlint
if err == rueidis.Nil {
return nil
}
Expand Down
15 changes: 10 additions & 5 deletions internal/search/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ type Req struct {
}

type ReqFilter struct { //nolint:musttag
Type []model.SubjectType `json:"type"` // or
Tag []string `json:"tag"` // and
AirDate []string `json:"air_date"` // and
Score []string `json:"rating"` // and
Rank []string `json:"rank"` // and
Type []model.SubjectType `json:"type"` // or
Tag []string `json:"tag"` // and
AirDate []string `json:"air_date"` // and
Score []string `json:"rating"` // and
Rank []string `json:"rank"` // and
MetaTags []string `json:"meta_tags"` // and

// if NSFW subject is enabled
NSFW null.Bool `json:"nsfw"`
Expand Down Expand Up @@ -224,6 +225,10 @@ func filterToMeiliFilter(req ReqFilter) [][]string {
filter = append(filter, []string{"nsfw = false"})
}

for _, tag := range req.MetaTags {
filter = append(filter, []string{"meta_tag = " + strconv.Quote(tag)})
}

// AND

for _, tag := range req.Tag {
Expand Down
3 changes: 3 additions & 0 deletions internal/search/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package search

import (
"strconv"
"strings"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/pkg/wiki"
Expand All @@ -28,6 +29,7 @@ import (
type subjectIndex struct {
ID model.SubjectID `json:"id"`
Tag []string `json:"tag,omitempty" filterable:"true"`
MetaTags []string `json:"meta_tag" filterable:"true"`
Name string `json:"name" searchable:"true"`
Aliases []string `json:"aliases,omitempty" searchable:"true"`
Date int `json:"date,omitempty" filterable:"true" sortable:"true"`
Expand Down Expand Up @@ -74,6 +76,7 @@ func extractSubject(s *model.Subject) subjectIndex {
ID: s.ID,
Name: s.Name,
Aliases: extractAliases(s, w),
MetaTags: strings.Split(s.MetaTags, " "),
Tag: tagNames,
NSFW: s.NSFW,
Type: s.TypeID,
Expand Down
2 changes: 1 addition & 1 deletion internal/search/index_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestIndexFilter(t *testing.T) {
t.Parallel()

actual := *(getAttributes("filterable"))
expected := []string{"date", "score", "rank", "type", "nsfw", "tag"}
expected := []string{"date", "meta_tag", "score", "rank", "type", "nsfw", "tag"}

sort.Strings(expected)
sort.Strings(actual)
Expand Down
1 change: 1 addition & 0 deletions internal/subject/mysql_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func ConvertDao(s *dao.Subject) (model.Subject, error) {
ID: s.ID,
Name: string(s.Name),
NameCN: string(s.NameCN),
MetaTags: s.FieldMetaTags,
TypeID: s.TypeID,
Image: s.Image,
PlatformID: s.Platform,
Expand Down
8 changes: 8 additions & 0 deletions openapi/v0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ paths:
items:
$ref: "#/components/schemas/SubjectType"
description: 条目类型,参照 `SubjectType` enum,多值之间为 `或` 的关系。
meta_tags:
type: array
items:
type: string
example:
- 童年
- 原创
description: 公共标签。多个值之间为 `且` 关系。可以用 `-` 排除标签。比如 `-科幻` 可以排除科幻标签。
tag:
type: array
items:
Expand Down

0 comments on commit be0fe62

Please sign in to comment.