Skip to content

Commit be7db18

Browse files
committed
fix #4479: add issue/pull index to search
1 parent b5326a4 commit be7db18

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

Diff for: modules/indexer/issues/db/db.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,32 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
5151
// The notification is defined in modules, but it's using lots of things should be in services.
5252

5353
cond := builder.NewCond()
54-
5554
if options.Keyword != "" {
5655
repoCond := builder.In("repo_id", options.RepoIDs)
5756
if len(options.RepoIDs) == 1 {
5857
repoCond = builder.Eq{"repo_id": options.RepoIDs[0]}
5958
}
60-
subQuery := builder.Select("id").From("issue").Where(repoCond)
61-
62-
cond = builder.Or(
63-
db.BuildCaseInsensitiveLike("issue.name", options.Keyword),
64-
db.BuildCaseInsensitiveLike("issue.content", options.Keyword),
65-
builder.In("issue.id", builder.Select("issue_id").
66-
From("comment").
67-
Where(builder.And(
68-
builder.Eq{"type": issue_model.CommentTypeComment},
69-
builder.In("issue_id", subQuery),
70-
db.BuildCaseInsensitiveLike("content", options.Keyword),
71-
)),
72-
),
73-
)
59+
60+
if options.Index.Has() {
61+
cond = builder.And(
62+
builder.Eq{"\"index\"": options.Keyword},
63+
repoCond,
64+
)
65+
} else {
66+
subQuery := builder.Select("id").From("issue").Where(repoCond)
67+
cond = builder.Or(
68+
db.BuildCaseInsensitiveLike("issue.name", options.Keyword),
69+
db.BuildCaseInsensitiveLike("issue.content", options.Keyword),
70+
builder.In("issue.id", builder.Select("issue_id").
71+
From("comment").
72+
Where(builder.And(
73+
builder.Eq{"type": issue_model.CommentTypeComment},
74+
builder.In("issue_id", subQuery),
75+
db.BuildCaseInsensitiveLike("content", options.Keyword),
76+
)),
77+
),
78+
)
79+
}
7480
}
7581

7682
opt, err := ToDBOptions(ctx, options)

Diff for: modules/indexer/issues/indexer.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"runtime/pprof"
11+
"strconv"
1112
"sync/atomic"
1213
"time"
1314

@@ -283,9 +284,13 @@ const (
283284
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
284285
indexer := *globalIndexer.Load()
285286

286-
if opts.Keyword == "" {
287+
issueIndex, err := strconv.Atoi(opts.Keyword)
288+
if err == nil {
289+
opts.Index = optional.Option[int64]{int64(issueIndex)}
290+
}
291+
if opts.Keyword == "" || opts.Index.Has() {
287292
// This is a conservative shortcut.
288-
// If the keyword is empty, db has better (at least not worse) performance to filter issues.
293+
// If the keyword is empty or an integer, db has better (at least not worse) performance to filter issues.
289294
// When the keyword is empty, it tends to listing rather than searching issues.
290295
// So if the user creates an issue and list issues immediately, the issue may not be listed because the indexer needs time to index the issue.
291296
// Even worse, the external indexer like elastic search may not be available for a while,

Diff for: modules/indexer/issues/indexer_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestDBSearchIssues(t *testing.T) {
3131
InitIssueIndexer(true)
3232

3333
t.Run("search issues with keyword", searchIssueWithKeyword)
34+
t.Run("search issues by index", searchIssueByIndex)
3435
t.Run("search issues in repo", searchIssueInRepo)
3536
t.Run("search issues by ID", searchIssueByID)
3637
t.Run("search issues is pr", searchIssueIsPull)
@@ -87,6 +88,43 @@ func searchIssueWithKeyword(t *testing.T) {
8788
}
8889
}
8990

91+
func searchIssueByIndex(t *testing.T) {
92+
tests := []struct {
93+
opts SearchOptions
94+
expectedIDs []int64
95+
}{
96+
{
97+
SearchOptions{
98+
Keyword: "1000",
99+
RepoIDs: []int64{1},
100+
},
101+
[]int64{},
102+
},
103+
{
104+
SearchOptions{
105+
Keyword: "2",
106+
RepoIDs: []int64{1, 2, 3, 32},
107+
},
108+
[]int64{2, 7, 12, 17},
109+
},
110+
{
111+
SearchOptions{
112+
Keyword: "1",
113+
RepoIDs: []int64{58},
114+
},
115+
[]int64{19},
116+
},
117+
}
118+
119+
for _, test := range tests {
120+
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
121+
if !assert.NoError(t, err) {
122+
return
123+
}
124+
assert.Equal(t, test.expectedIDs, issueIDs)
125+
}
126+
}
127+
90128
func searchIssueInRepo(t *testing.T) {
91129
tests := []struct {
92130
opts SearchOptions

Diff for: modules/indexer/issues/internal/model.go

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type SearchOptions struct {
8989

9090
MilestoneIDs []int64 // milestones the issues have
9191

92+
Index optional.Option[int64] // keyword as potential issue index
93+
9294
ProjectID optional.Option[int64] // project the issues belong to
9395
ProjectColumnID optional.Option[int64] // project column the issues belong to
9496

0 commit comments

Comments
 (0)