Skip to content

Commit 64750fd

Browse files
committed
refactor numeric keyword detection
1 parent e546736 commit 64750fd

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

modules/indexer/issues/db/db.go

+15-18
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,29 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
4949
// But the two functions are used in modules/notification/indexer, that means we will import services/indexer in modules/notification/indexer.
5050
// So that's the root problem:
5151
// The notification is defined in modules, but it's using lots of things should be in services.
52-
5352
cond := builder.NewCond()
5453
if options.Keyword != "" {
5554
repoCond := builder.In("repo_id", options.RepoIDs)
5655
if len(options.RepoIDs) == 1 {
5756
repoCond = builder.Eq{"repo_id": options.RepoIDs[0]}
5857
}
5958

60-
if options.Index.Has() {
61-
cond = builder.And(
59+
subQuery := builder.Select("id").From("issue").Where(repoCond)
60+
cond = builder.Or(
61+
db.BuildCaseInsensitiveLike("issue.name", options.Keyword),
62+
db.BuildCaseInsensitiveLike("issue.content", options.Keyword),
63+
builder.In("issue.id", builder.Select("issue_id").
64+
From("comment").
65+
Where(builder.And(
66+
builder.Eq{"type": issue_model.CommentTypeComment},
67+
builder.In("issue_id", subQuery),
68+
db.BuildCaseInsensitiveLike("content", options.Keyword),
69+
)),
70+
),
71+
)
72+
if options.IsKeywordNumeric() {
73+
cond = cond.Or(
6274
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-
),
7875
)
7976
}
8077
}

modules/indexer/issues/indexer.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"os"
1010
"runtime/pprof"
11-
"strconv"
1211
"sync/atomic"
1312
"time"
1413

@@ -284,11 +283,7 @@ const (
284283
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
285284
indexer := *globalIndexer.Load()
286285

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() {
286+
if opts.Keyword == "" || opts.IsKeywordNumeric() {
292287
// This is a conservative shortcut.
293288
// If the keyword is empty or an integer, db has better (at least not worse) performance to filter issues.
294289
// When the keyword is empty, it tends to listing rather than searching issues.

modules/indexer/issues/internal/model.go

+8-2
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"
@@ -89,8 +91,6 @@ type SearchOptions struct {
8991

9092
MilestoneIDs []int64 // milestones the issues have
9193

92-
Index optional.Option[int64] // keyword as potential issue index
93-
9494
ProjectID optional.Option[int64] // project the issues belong to
9595
ProjectColumnID optional.Option[int64] // project column the issues belong to
9696

@@ -126,6 +126,12 @@ func (o *SearchOptions) Copy(edit ...func(options *SearchOptions)) *SearchOption
126126
return &v
127127
}
128128

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+
129135
type SortBy string
130136

131137
const (

0 commit comments

Comments
 (0)