Skip to content

Commit 604c69e

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

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

modules/indexer/issues/db/db.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ 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
}
6059
subQuery := builder.Select("id").From("issue").Where(repoCond)
6160

61+
var issueCond builder.Cond
62+
issueCond = nil
63+
if options.Index.Has() {
64+
issueCond = builder.Eq{"issue.index": options.Keyword}
65+
}
6266
cond = builder.Or(
6367
db.BuildCaseInsensitiveLike("issue.name", options.Keyword),
6468
db.BuildCaseInsensitiveLike("issue.content", options.Keyword),
@@ -70,6 +74,7 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
7074
db.BuildCaseInsensitiveLike("content", options.Keyword),
7175
)),
7276
),
77+
issueCond,
7378
)
7479
}
7580

modules/indexer/issues/indexer.go

Lines changed: 7 additions & 2 deletions
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,

modules/indexer/issues/indexer_test.go

Lines changed: 38 additions & 0 deletions
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{},
100+
},
101+
[]int64{},
102+
},
103+
{
104+
SearchOptions{
105+
Keyword: "2",
106+
RepoIDs: []int64{},
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

modules/indexer/issues/internal/model.go

Lines changed: 2 additions & 0 deletions
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)