Skip to content

Commit 2e7aa6c

Browse files
committed
fix go-gitea#4479: add issue/pull id to search
1 parent bda875b commit 2e7aa6c

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

modules/indexer/issues/bleve/bleve.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func generateIssueIndexMapping() (mapping.IndexMapping, error) {
5353
numericFieldMapping := bleve.NewNumericFieldMapping()
5454
numericFieldMapping.Store = false
5555
numericFieldMapping.IncludeInAll = false
56+
docMapping.AddFieldMappingsAt("id", numericFieldMapping)
5657
docMapping.AddFieldMappingsAt("repo_id", numericFieldMapping)
5758

5859
textFieldMapping := bleve.NewTextFieldMapping()
@@ -161,11 +162,16 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
161162
fuzziness = inner_bleve.GuessFuzzinessByKeyword(options.Keyword)
162163
}
163164

164-
queries = append(queries, bleve.NewDisjunctionQuery([]query.Query{
165+
innerQueries := []query.Query{
165166
inner_bleve.MatchPhraseQuery(options.Keyword, "title", issueIndexerAnalyzer, fuzziness),
166167
inner_bleve.MatchPhraseQuery(options.Keyword, "content", issueIndexerAnalyzer, fuzziness),
167168
inner_bleve.MatchPhraseQuery(options.Keyword, "comments", issueIndexerAnalyzer, fuzziness),
168-
}...))
169+
}
170+
if options.IssueID.Has() {
171+
innerQueries = append(innerQueries, inner_bleve.NumericEqualityQuery(options.IssueID.Value(), "id"))
172+
}
173+
174+
queries = append(queries, bleve.NewDisjunctionQuery(innerQueries...))
169175
}
170176

171177
if len(options.RepoIDs) > 0 || options.AllPublic {

modules/indexer/issues/db/db.go

+6-1
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 issueIDEq builder.Cond
62+
issueIDEq = nil
63+
if options.IssueID.Has() {
64+
issueIDEq = builder.Eq{"issue.id": 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+
issueIDEq,
7378
)
7479
}
7580

modules/indexer/issues/elasticsearch/elasticsearch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
150150
searchType = esMultiMatchTypeBestFields
151151
}
152152

153-
query.Must(elastic.NewMultiMatchQuery(options.Keyword, "title", "content", "comments").Type(searchType))
153+
query.Must(elastic.NewMultiMatchQuery(options.Keyword, "id", "title", "content", "comments").Type(searchType))
154154
}
155155

156156
if len(options.RepoIDs) > 0 {

modules/indexer/issues/indexer.go

+7
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,6 +284,7 @@ const (
283284
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
284285
indexer := *globalIndexer.Load()
285286

287+
log.Info("Indexer Keyword: " + opts.Keyword)
286288
if opts.Keyword == "" {
287289
// This is a conservative shortcut.
288290
// If the keyword is empty, db has better (at least not worse) performance to filter issues.
@@ -291,6 +293,11 @@ func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, err
291293
// Even worse, the external indexer like elastic search may not be available for a while,
292294
// and the user may not be able to list issues completely until it is available again.
293295
indexer = db.NewIndexer()
296+
} else {
297+
issueID, err := strconv.Atoi(opts.Keyword)
298+
if err == nil {
299+
opts.IssueID = optional.Option[int64]{int64(issueID)}
300+
}
294301
}
295302

296303
result, err := indexer.Search(ctx, opts)

modules/indexer/issues/internal/model.go

+2
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+
IssueID optional.Option[int64] // keyword as potential issue id
93+
9294
ProjectID optional.Option[int64] // project the issues belong to
9395
ProjectColumnID optional.Option[int64] // project column the issues belong to
9496

modules/indexer/issues/meilisearch/meilisearch.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func NewIndexer(url, apiKey, indexerName string) *Indexer {
4646
"words", "typo", "proximity", "attribute", "exactness"},
4747

4848
SearchableAttributes: []string{
49+
"id",
4950
"title",
5051
"content",
5152
"comments",
@@ -57,6 +58,7 @@ func NewIndexer(url, apiKey, indexerName string) *Indexer {
5758
"comments",
5859
},
5960
FilterableAttributes: []string{
61+
"id",
6062
"repo_id",
6163
"is_public",
6264
"is_pull",

0 commit comments

Comments
 (0)