Skip to content

Commit

Permalink
fix go-gitea#4479: add fuzzy keyword search to title
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jun 27, 2024
1 parent d655ff1 commit a390d75
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
8 changes: 8 additions & 0 deletions modules/indexer/internal/bleve/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func MatchPhraseQuery(matchPhrase, field, analyzer string, fuzziness int) *query
return q
}

// FuzzyQuery generates a fuzzy query for the given phrase, field, and fuzziness
func FuzzyQuery(matchPhrase, field string, fuzziness int) *query.FuzzyQuery {
q := bleve.NewFuzzyQuery(matchPhrase)
q.FieldVal = field
q.Fuzziness = fuzziness
return q
}

// BoolFieldQuery generates a bool field query for the given value and field
func BoolFieldQuery(value bool, field string) *query.BoolFieldQuery {
q := bleve.NewBoolFieldQuery(value)
Expand Down
10 changes: 6 additions & 4 deletions modules/indexer/internal/bleve/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ func openIndexer(path string, latestVersion int) (bleve.Index, int, error) {

func GuessFuzzinessByKeyword(s string) int {
// according to https://github.com/blevesearch/bleve/issues/1563, the supported max fuzziness is 2
// magic number 4 was chosen to determine the levenshtein distance per each character of a keyword
// BUT, when using CJK (eg: `갃갃갃` `啊啊啊`), it mismatches a lot.
// BUT, when using CJK (eg: `갃갃갃` `啊啊啊`), it mismatches a lot
// which we need to live with, as we need to support not just ASCII
// in case of code points >= 128 we will increase the fuzziness to 2
// the standard is 1
for _, r := range s {
if r >= 128 {
return 0
return 2
}
}
return min(2, len(s)/4)
return 1
}
2 changes: 1 addition & 1 deletion modules/indexer/issues/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
}

queries = append(queries, bleve.NewDisjunctionQuery([]query.Query{
inner_bleve.MatchPhraseQuery(options.Keyword, "title", issueIndexerAnalyzer, fuzziness),
inner_bleve.FuzzyQuery(options.Keyword, "title", fuzziness),
inner_bleve.MatchPhraseQuery(options.Keyword, "content", issueIndexerAnalyzer, fuzziness),
inner_bleve.MatchPhraseQuery(options.Keyword, "comments", issueIndexerAnalyzer, fuzziness),
}...))
Expand Down
1 change: 1 addition & 0 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,7 @@ func SearchIssues(ctx *context.Context) {
MilestoneIDs: includedMilestones,
ProjectID: projectID,
SortBy: issue_indexer.SortByCreatedDesc,
IsFuzzyKeyword: true,
}

if since != 0 {
Expand Down

0 comments on commit a390d75

Please sign in to comment.