Skip to content

Commit 9c464e2

Browse files
fix #4479: add issue/pull index to search
- Use backticks instead of double quotes Co-authored-by: wxiaoguang <[email protected]>
1 parent e82f3ca commit 9c464e2

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

modules/indexer/issues/db/db.go

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
7171
)),
7272
),
7373
)
74+
75+
if options.IsKeywordNumeric() {
76+
cond = cond.Or(
77+
builder.Eq{"`index`": options.Keyword},
78+
)
79+
}
7480
}
7581

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

modules/indexer/issues/indexer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ const (
283283
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
284284
indexer := *globalIndexer.Load()
285285

286-
if opts.Keyword == "" {
286+
if opts.Keyword == "" || opts.IsKeywordNumeric() {
287287
// This is a conservative shortcut.
288-
// If the keyword is empty, db has better (at least not worse) performance to filter issues.
288+
// If the keyword is empty or an integer, db has better (at least not worse) performance to filter issues.
289289
// When the keyword is empty, it tends to listing rather than searching issues.
290290
// 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.
291291
// Even worse, the external indexer like elastic search may not be available for a while,

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{17, 12, 7, 2},
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

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package internal
55

66
import (
7+
"strconv"
8+
79
"code.gitea.io/gitea/models/db"
810
"code.gitea.io/gitea/modules/optional"
911
"code.gitea.io/gitea/modules/timeutil"
@@ -124,6 +126,12 @@ func (o *SearchOptions) Copy(edit ...func(options *SearchOptions)) *SearchOption
124126
return &v
125127
}
126128

129+
// used for optimized issue index based search
130+
func (o *SearchOptions) IsKeywordNumeric() bool {
131+
_, err := strconv.Atoi(o.Keyword)
132+
return err == nil
133+
}
134+
127135
type SortBy string
128136

129137
const (

0 commit comments

Comments
 (0)