Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service/search): from/toの複数指定を可能に #2558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading