Skip to content

Commit a390d75

Browse files
committed
fix #4479: add fuzzy keyword search to title
1 parent d655ff1 commit a390d75

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

modules/indexer/internal/bleve/query.go

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func MatchPhraseQuery(matchPhrase, field, analyzer string, fuzziness int) *query
2828
return q
2929
}
3030

31+
// FuzzyQuery generates a fuzzy query for the given phrase, field, and fuzziness
32+
func FuzzyQuery(matchPhrase, field string, fuzziness int) *query.FuzzyQuery {
33+
q := bleve.NewFuzzyQuery(matchPhrase)
34+
q.FieldVal = field
35+
q.Fuzziness = fuzziness
36+
return q
37+
}
38+
3139
// BoolFieldQuery generates a bool field query for the given value and field
3240
func BoolFieldQuery(value bool, field string) *query.BoolFieldQuery {
3341
q := bleve.NewBoolFieldQuery(value)

modules/indexer/internal/bleve/util.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ func openIndexer(path string, latestVersion int) (bleve.Index, int, error) {
5050

5151
func GuessFuzzinessByKeyword(s string) int {
5252
// according to https://github.com/blevesearch/bleve/issues/1563, the supported max fuzziness is 2
53-
// magic number 4 was chosen to determine the levenshtein distance per each character of a keyword
54-
// BUT, when using CJK (eg: `갃갃갃` `啊啊啊`), it mismatches a lot.
53+
// BUT, when using CJK (eg: `갃갃갃` `啊啊啊`), it mismatches a lot
54+
// which we need to live with, as we need to support not just ASCII
55+
// in case of code points >= 128 we will increase the fuzziness to 2
56+
// the standard is 1
5557
for _, r := range s {
5658
if r >= 128 {
57-
return 0
59+
return 2
5860
}
5961
}
60-
return min(2, len(s)/4)
62+
return 1
6163
}

modules/indexer/issues/bleve/bleve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
162162
}
163163

164164
queries = append(queries, bleve.NewDisjunctionQuery([]query.Query{
165-
inner_bleve.MatchPhraseQuery(options.Keyword, "title", issueIndexerAnalyzer, fuzziness),
165+
inner_bleve.FuzzyQuery(options.Keyword, "title", fuzziness),
166166
inner_bleve.MatchPhraseQuery(options.Keyword, "content", issueIndexerAnalyzer, fuzziness),
167167
inner_bleve.MatchPhraseQuery(options.Keyword, "comments", issueIndexerAnalyzer, fuzziness),
168168
}...))

routers/web/repo/issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,7 @@ func SearchIssues(ctx *context.Context) {
26772677
MilestoneIDs: includedMilestones,
26782678
ProjectID: projectID,
26792679
SortBy: issue_indexer.SortByCreatedDesc,
2680+
IsFuzzyKeyword: true,
26802681
}
26812682

26822683
if since != 0 {

0 commit comments

Comments
 (0)