Skip to content

Commit

Permalink
fix go-gitea#4479: add issue/pull index to search
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jun 25, 2024
1 parent b5326a4 commit 604c69e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
7 changes: 6 additions & 1 deletion modules/indexer/issues/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
// The notification is defined in modules, but it's using lots of things should be in services.

cond := builder.NewCond()

if options.Keyword != "" {
repoCond := builder.In("repo_id", options.RepoIDs)
if len(options.RepoIDs) == 1 {
repoCond = builder.Eq{"repo_id": options.RepoIDs[0]}
}
subQuery := builder.Select("id").From("issue").Where(repoCond)

var issueCond builder.Cond
issueCond = nil
if options.Index.Has() {
issueCond = builder.Eq{"issue.index": options.Keyword}
}
cond = builder.Or(
db.BuildCaseInsensitiveLike("issue.name", options.Keyword),
db.BuildCaseInsensitiveLike("issue.content", options.Keyword),
Expand All @@ -70,6 +74,7 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
db.BuildCaseInsensitiveLike("content", options.Keyword),
)),
),
issueCond,
)
}

Expand Down
9 changes: 7 additions & 2 deletions modules/indexer/issues/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"runtime/pprof"
"strconv"
"sync/atomic"
"time"

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

if opts.Keyword == "" {
issueIndex, err := strconv.Atoi(opts.Keyword)
if err == nil {
opts.Index = optional.Option[int64]{int64(issueIndex)}
}
if opts.Keyword == "" || opts.Index.Has() {
// This is a conservative shortcut.
// If the keyword is empty, db has better (at least not worse) performance to filter issues.
// If the keyword is empty or an integer, db has better (at least not worse) performance to filter issues.
// When the keyword is empty, it tends to listing rather than searching issues.
// 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.
// Even worse, the external indexer like elastic search may not be available for a while,
Expand Down
38 changes: 38 additions & 0 deletions modules/indexer/issues/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestDBSearchIssues(t *testing.T) {
InitIssueIndexer(true)

t.Run("search issues with keyword", searchIssueWithKeyword)
t.Run("search issues by index", searchIssueByIndex)
t.Run("search issues in repo", searchIssueInRepo)
t.Run("search issues by ID", searchIssueByID)
t.Run("search issues is pr", searchIssueIsPull)
Expand Down Expand Up @@ -87,6 +88,43 @@ func searchIssueWithKeyword(t *testing.T) {
}
}

func searchIssueByIndex(t *testing.T) {
tests := []struct {
opts SearchOptions
expectedIDs []int64
}{
{
SearchOptions{
Keyword: "1000",
RepoIDs: []int64{},
},
[]int64{},
},
{
SearchOptions{
Keyword: "2",
RepoIDs: []int64{},
},
[]int64{2, 7, 12, 17},
},
{
SearchOptions{
Keyword: "1",
RepoIDs: []int64{58},
},
[]int64{19},
},
}

for _, test := range tests {
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, test.expectedIDs, issueIDs)
}
}

func searchIssueInRepo(t *testing.T) {
tests := []struct {
opts SearchOptions
Expand Down
2 changes: 2 additions & 0 deletions modules/indexer/issues/internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type SearchOptions struct {

MilestoneIDs []int64 // milestones the issues have

Index optional.Option[int64] // keyword as potential issue index

ProjectID optional.Option[int64] // project the issues belong to
ProjectColumnID optional.Option[int64] // project column the issues belong to

Expand Down

0 comments on commit 604c69e

Please sign in to comment.