Skip to content

Commit

Permalink
Merge pull request #2558 from traPtitech/feat/multiple-from-to
Browse files Browse the repository at this point in the history
feat(service/search): from/toの複数指定を可能に
  • Loading branch information
ramdos0207 authored Feb 2, 2025
2 parents a5118b6 + 26f069c commit b2bd86b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
12 changes: 8 additions & 4 deletions docs/v3-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ paths:
name: in
description: メッセージが投稿されたチャンネル
- schema:
type: string
format: uuid
type: array
items:
type: string
format: uuid
in: query
name: to
description: メンションされたユーザー
- schema:
type: string
format: uuid
type: array
items:
type: string
format: uuid
in: query
name: from
description: メッセージを投稿したユーザー
Expand Down
4 changes: 2 additions & 2 deletions service/search/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Query struct {
After optional.Of[time.Time] `query:"after"` // 以降(投稿日時) 2020-06-20T00:00:00Z
Before optional.Of[time.Time] `query:"before"` // 以前(投稿日時)
In optional.Of[uuid.UUID] `query:"in"` // 投稿チャンネル
To optional.Of[uuid.UUID] `query:"to"` // メンション先
From optional.Of[uuid.UUID] `query:"from"` // 投稿者
To []uuid.UUID `query:"to"` // メンション先
From []uuid.UUID `query:"from"` // 投稿者
Citation optional.Of[uuid.UUID] `query:"citation"` // 引用しているメッセージ
Bot optional.Of[bool] `query:"bot"` // 投稿者がBotか
HasURL optional.Of[bool] `query:"hasURL"` // URLの存在
Expand Down
58 changes: 39 additions & 19 deletions service/search/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,17 @@ func NewESEngine(mm message.Manager, cm channel.Manager, repo repository.Reposit
type searchQuery m

type searchBody struct {
Query *struct {
Bool *struct {
Musts []searchQuery `json:"must,omitempty"`
} `json:"bool,omitempty"`
} `json:"query,omitempty"`
Query searchQuery `json:"query,omitempty"`
}

func newSearchBody(sq []searchQuery) searchBody {
sb := searchBody{
Query: &struct {
Bool *struct {
Musts []searchQuery `json:"must,omitempty"`
} `json:"bool,omitempty"`
}{Bool: &struct {
Musts []searchQuery `json:"must,omitempty"`
}{Musts: sq}},
func newSearchBody(andQueries []searchQuery) searchBody {
return searchBody{
Query: searchQuery{
"bool": boolQuery{
Must: andQueries,
},
},
}
return sb
}

type simpleQueryString struct {
Expand All @@ -286,6 +279,11 @@ type simpleQueryString struct {
DefaultOperator string `json:"default_operator"`
}

type boolQuery struct {
Must []searchQuery `json:"must,omitempty"`
Should []searchQuery `json:"should,omitempty"`
}

type rangeQuery map[string]rangeParameters

type rangeParameters struct {
Expand Down Expand Up @@ -338,12 +336,34 @@ func (e *esEngine) Do(q *Query) (Result, error) {
musts = append(musts, searchQuery{"term": termQuery{"isPublic": termQueryParameter{Value: true}}})
}

if q.To.Valid {
musts = append(musts, searchQuery{"term": termQuery{"to": termQueryParameter{Value: q.To}}})
if len(q.To) > 0 {
orQueries := make([]searchQuery, 0, len(q.To))
for _, toID := range q.To {
orQueries = append(orQueries, searchQuery{"term": termQuery{"to": termQueryParameter{Value: toID}}})
}

sq := searchQuery{"bool": boolQuery{Should: orQueries}}
if len(q.To) == 1 {
// OR検索が不要
sq = orQueries[0]
}

musts = append(musts, sq)
}

if q.From.Valid {
musts = append(musts, searchQuery{"term": termQuery{"userId": termQueryParameter{Value: q.From}}})
if len(q.From) > 0 {
orQueries := make([]searchQuery, 0, len(q.From))
for _, fromID := range q.From {
orQueries = append(orQueries, searchQuery{"term": termQuery{"userId": termQueryParameter{Value: fromID}}})
}

sq := searchQuery{"bool": boolQuery{Should: orQueries}}
if len(q.From) == 1 {
// OR検索が不要
sq = orQueries[0]
}

musts = append(musts, sq)
}

if q.Citation.Valid {
Expand Down

0 comments on commit b2bd86b

Please sign in to comment.