Skip to content

Commit fdb2dd0

Browse files
committed
Re-use results of a keyword search as list of matching IDs for an 'IN' db query.
1 parent ec91d65 commit fdb2dd0

File tree

8 files changed

+27
-11
lines changed

8 files changed

+27
-11
lines changed

modules/indexer/issues/bleve/bleve.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
286286
"updated_unix"))
287287
}
288288

289+
if len(options.IssueIDs) > 0 {
290+
var idQueries []query.Query
291+
for _, issueId := range options.IssueIDs {
292+
idQueries = append(idQueries, inner_bleve.NumericEqualityQuery(issueId, "id"))
293+
}
294+
queries = append(queries, bleve.NewDisjunctionQuery(idQueries...))
295+
}
296+
289297
var indexerQuery query.Query = bleve.NewConjunctionQuery(queries...)
290298
if len(queries) == 0 {
291299
indexerQuery = bleve.NewMatchAllQuery()

modules/indexer/issues/db/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_m
7373
ExcludedLabelNames: nil,
7474
IncludeMilestones: nil,
7575
SortType: sortType,
76-
IssueIDs: nil,
76+
IssueIDs: options.IssueIDs,
7777
UpdatedAfterUnix: options.UpdatedAfterUnix.Value(),
7878
UpdatedBeforeUnix: options.UpdatedBeforeUnix.Value(),
7979
PriorityRepoID: 0,

modules/indexer/issues/dboptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOp
2020
IsPull: opts.IsPull,
2121
IsClosed: opts.IsClosed,
2222
IsArchived: opts.IsArchived,
23+
IssueIDs: opts.IssueIDs,
2324
}
2425

2526
if len(opts.LabelIDs) == 1 && opts.LabelIDs[0] == 0 {

modules/indexer/issues/elasticsearch/elasticsearch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
201201
}
202202
}
203203

204+
if len(options.IssueIDs) > 0 {
205+
query.Must(elastic.NewTermsQuery("id", toAnySlice(options.IssueIDs)...))
206+
}
207+
204208
if len(options.MilestoneIDs) > 0 {
205209
query.Must(elastic.NewTermsQuery("milestone_id", toAnySlice(options.MilestoneIDs)...))
206210
}

modules/indexer/issues/indexer.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
"fmt"
99
"os"
1010
"runtime/pprof"
11-
"strings"
1211
"sync/atomic"
1312
"time"
1413

1514
db_model "code.gitea.io/gitea/models/db"
16-
issue_model "code.gitea.io/gitea/models/issues"
1715
repo_model "code.gitea.io/gitea/models/repo"
1816
"code.gitea.io/gitea/modules/graceful"
1917
"code.gitea.io/gitea/modules/indexer"
@@ -296,11 +294,6 @@ func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, err
296294
ix = db.NewIndexer()
297295
}
298296

299-
if strings.HasPrefix(string(opts.SortBy), issue_model.ScopeSortPrefix) {
300-
// Sorting by label scope is currently only supported by the DB indexer.
301-
ix = db.NewIndexer()
302-
}
303-
304297
result, err := ix.Search(ctx, opts)
305298
if err != nil {
306299
return nil, 0, err

modules/indexer/issues/internal/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ type SearchOptions struct {
9494

9595
MilestoneIDs []int64 // milestones the issues have
9696

97+
IssueIDs []int64 // restrict search to these issues
98+
9799
ProjectID optional.Option[int64] // project the issues belong to
98100
ProjectColumnID optional.Option[int64] // project column the issues belong to
99101

modules/indexer/issues/meilisearch/meilisearch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
176176
}
177177
}
178178

179+
if len(options.IssueIDs) > 0 {
180+
query.And(inner_meilisearch.NewFilterIn("id", options.IssueIDs...))
181+
}
182+
179183
if len(options.MilestoneIDs) > 0 {
180184
query.And(inner_meilisearch.NewFilterIn("milestone_id", options.MilestoneIDs...))
181185
}

routers/web/repo/issue_list.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
560560
return
561561
}
562562

563+
var keywordMatchedIssueIds []int64
563564
var issueStats *issues_model.IssueStats
564565
statsOpts := &issues_model.IssuesOptions{
565566
RepoIDs: []int64{repo.ID},
@@ -575,7 +576,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
575576
IssueIDs: nil,
576577
}
577578
if keyword != "" {
578-
allIssueIDs, err := issueIDsFromSearch(ctx, keyword, statsOpts)
579+
keywordMatchedIssueIds, err = issueIDsFromSearch(ctx, keyword, statsOpts)
579580
if err != nil {
580581
if issue_indexer.IsAvailable(ctx) {
581582
ctx.ServerError("issueIDsFromSearch", err)
@@ -584,7 +585,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
584585
ctx.Data["IssueIndexerUnavailable"] = true
585586
return
586587
}
587-
statsOpts.IssueIDs = allIssueIDs
588+
statsOpts.IssueIDs = keywordMatchedIssueIds
588589
}
589590
if keyword != "" && len(statsOpts.IssueIDs) == 0 {
590591
// So it did search with the keyword, but no issue found.
@@ -641,7 +642,9 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
641642

642643
var issues issues_model.IssueList
643644
{
644-
ids, err := issueIDsFromSearch(ctx, keyword, &issues_model.IssuesOptions{
645+
// Do not repeat the keyword search, since if we had any keyword matches we should
646+
// already have their IDs available in allMatchingIssueIDs.
647+
ids, err := issueIDsFromSearch(ctx, "", &issues_model.IssuesOptions{
645648
Paginator: &db.ListOptions{
646649
Page: pager.Paginater.Current(),
647650
PageSize: setting.UI.IssuePagingNum,
@@ -658,6 +661,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
658661
IsPull: isPullOption,
659662
LabelIDs: labelIDs,
660663
SortType: sortType,
664+
IssueIDs: keywordMatchedIssueIds,
661665
})
662666
if err != nil {
663667
if issue_indexer.IsAvailable(ctx) {

0 commit comments

Comments
 (0)