Skip to content

Commit b2bd86b

Browse files
authored
Merge pull request #2558 from traPtitech/feat/multiple-from-to
feat(service/search): from/toの複数指定を可能に
2 parents a5118b6 + 26f069c commit b2bd86b

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

docs/v3-api.yaml

+8-4
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,18 @@ paths:
119119
name: in
120120
description: メッセージが投稿されたチャンネル
121121
- schema:
122-
type: string
123-
format: uuid
122+
type: array
123+
items:
124+
type: string
125+
format: uuid
124126
in: query
125127
name: to
126128
description: メンションされたユーザー
127129
- schema:
128-
type: string
129-
format: uuid
130+
type: array
131+
items:
132+
type: string
133+
format: uuid
130134
in: query
131135
name: from
132136
description: メッセージを投稿したユーザー

service/search/engine.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type Query struct {
3232
After optional.Of[time.Time] `query:"after"` // 以降(投稿日時) 2020-06-20T00:00:00Z
3333
Before optional.Of[time.Time] `query:"before"` // 以前(投稿日時)
3434
In optional.Of[uuid.UUID] `query:"in"` // 投稿チャンネル
35-
To optional.Of[uuid.UUID] `query:"to"` // メンション先
36-
From optional.Of[uuid.UUID] `query:"from"` // 投稿者
35+
To []uuid.UUID `query:"to"` // メンション先
36+
From []uuid.UUID `query:"from"` // 投稿者
3737
Citation optional.Of[uuid.UUID] `query:"citation"` // 引用しているメッセージ
3838
Bot optional.Of[bool] `query:"bot"` // 投稿者がBotか
3939
HasURL optional.Of[bool] `query:"hasURL"` // URLの存在

service/search/es.go

+39-19
Original file line numberDiff line numberDiff line change
@@ -260,24 +260,17 @@ func NewESEngine(mm message.Manager, cm channel.Manager, repo repository.Reposit
260260
type searchQuery m
261261

262262
type searchBody struct {
263-
Query *struct {
264-
Bool *struct {
265-
Musts []searchQuery `json:"must,omitempty"`
266-
} `json:"bool,omitempty"`
267-
} `json:"query,omitempty"`
263+
Query searchQuery `json:"query,omitempty"`
268264
}
269265

270-
func newSearchBody(sq []searchQuery) searchBody {
271-
sb := searchBody{
272-
Query: &struct {
273-
Bool *struct {
274-
Musts []searchQuery `json:"must,omitempty"`
275-
} `json:"bool,omitempty"`
276-
}{Bool: &struct {
277-
Musts []searchQuery `json:"must,omitempty"`
278-
}{Musts: sq}},
266+
func newSearchBody(andQueries []searchQuery) searchBody {
267+
return searchBody{
268+
Query: searchQuery{
269+
"bool": boolQuery{
270+
Must: andQueries,
271+
},
272+
},
279273
}
280-
return sb
281274
}
282275

283276
type simpleQueryString struct {
@@ -286,6 +279,11 @@ type simpleQueryString struct {
286279
DefaultOperator string `json:"default_operator"`
287280
}
288281

282+
type boolQuery struct {
283+
Must []searchQuery `json:"must,omitempty"`
284+
Should []searchQuery `json:"should,omitempty"`
285+
}
286+
289287
type rangeQuery map[string]rangeParameters
290288

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

341-
if q.To.Valid {
342-
musts = append(musts, searchQuery{"term": termQuery{"to": termQueryParameter{Value: q.To}}})
339+
if len(q.To) > 0 {
340+
orQueries := make([]searchQuery, 0, len(q.To))
341+
for _, toID := range q.To {
342+
orQueries = append(orQueries, searchQuery{"term": termQuery{"to": termQueryParameter{Value: toID}}})
343+
}
344+
345+
sq := searchQuery{"bool": boolQuery{Should: orQueries}}
346+
if len(q.To) == 1 {
347+
// OR検索が不要
348+
sq = orQueries[0]
349+
}
350+
351+
musts = append(musts, sq)
343352
}
344353

345-
if q.From.Valid {
346-
musts = append(musts, searchQuery{"term": termQuery{"userId": termQueryParameter{Value: q.From}}})
354+
if len(q.From) > 0 {
355+
orQueries := make([]searchQuery, 0, len(q.From))
356+
for _, fromID := range q.From {
357+
orQueries = append(orQueries, searchQuery{"term": termQuery{"userId": termQueryParameter{Value: fromID}}})
358+
}
359+
360+
sq := searchQuery{"bool": boolQuery{Should: orQueries}}
361+
if len(q.From) == 1 {
362+
// OR検索が不要
363+
sq = orQueries[0]
364+
}
365+
366+
musts = append(musts, sq)
347367
}
348368

349369
if q.Citation.Valid {

0 commit comments

Comments
 (0)