Skip to content

Commit

Permalink
fix #4479: add issue/pull id to search
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jun 24, 2024
1 parent 053e582 commit 7bfe539
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
10 changes: 8 additions & 2 deletions modules/indexer/issues/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func generateIssueIndexMapping() (mapping.IndexMapping, error) {
numericFieldMapping := bleve.NewNumericFieldMapping()
numericFieldMapping.Store = false
numericFieldMapping.IncludeInAll = false
docMapping.AddFieldMappingsAt("id", numericFieldMapping)
docMapping.AddFieldMappingsAt("repo_id", numericFieldMapping)

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

queries = append(queries, bleve.NewDisjunctionQuery([]query.Query{
innerQueries := []query.Query{
inner_bleve.MatchPhraseQuery(options.Keyword, "title", issueIndexerAnalyzer, fuzziness),
inner_bleve.MatchPhraseQuery(options.Keyword, "content", issueIndexerAnalyzer, fuzziness),
inner_bleve.MatchPhraseQuery(options.Keyword, "comments", issueIndexerAnalyzer, fuzziness),
}...))
}
if options.IssueID.Has() {
innerQueries = append(innerQueries, inner_bleve.NumericEqualityQuery(options.IssueID.Value(), "id"))
}

queries = append(queries, bleve.NewDisjunctionQuery(innerQueries...))
}

if len(options.RepoIDs) > 0 || options.AllPublic {
Expand Down
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 issueIDEq builder.Cond
issueIDEq = nil
if options.IssueID.Has() {
issueIDEq = builder.Eq{"issue.id": 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),
)),
),
issueIDEq,
)
}

Expand Down
2 changes: 1 addition & 1 deletion modules/indexer/issues/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
searchType = esMultiMatchTypeBestFields
}

query.Must(elastic.NewMultiMatchQuery(options.Keyword, "title", "content", "comments").Type(searchType))
query.Must(elastic.NewMultiMatchQuery(options.Keyword, "id", "title", "content", "comments").Type(searchType))
}

if len(options.RepoIDs) > 0 {
Expand Down
7 changes: 7 additions & 0 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,6 +284,7 @@ const (
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
indexer := *globalIndexer.Load()

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

result, err := indexer.Search(ctx, opts)
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

IssueID optional.Option[int64] // keyword as potential issue id

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

Expand Down
2 changes: 2 additions & 0 deletions modules/indexer/issues/meilisearch/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewIndexer(url, apiKey, indexerName string) *Indexer {
"words", "typo", "proximity", "attribute", "exactness"},

SearchableAttributes: []string{
"id",
"title",
"content",
"comments",
Expand All @@ -57,6 +58,7 @@ func NewIndexer(url, apiKey, indexerName string) *Indexer {
"comments",
},
FilterableAttributes: []string{
"id",
"repo_id",
"is_public",
"is_pull",
Expand Down

0 comments on commit 7bfe539

Please sign in to comment.