Skip to content

Commit

Permalink
wip 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 26, 2024
1 parent b5326a4 commit d23482c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ security-check:
go run $(GOVULNCHECK_PACKAGE) ./...

$(EXECUTABLE): $(GO_SOURCES) $(TAGS_PREREQ)
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) build $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) build $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o $@

.PHONY: release
release: frontend generate release-windows release-linux release-darwin release-freebsd release-copy release-compress vendor release-sources release-docs release-check
Expand Down
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
2 changes: 1 addition & 1 deletion modules/indexer/internal/bleve/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ func GuessFuzzinessByKeyword(s string) int {
return 0
}
}
return min(2, len(s)/4)
return 1
}
6 changes: 5 additions & 1 deletion modules/indexer/issues/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,16 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (

if options.Keyword != "" {
fuzziness := 0
var titleQuery query.Query
if options.IsFuzzyKeyword {
fuzziness = inner_bleve.GuessFuzzinessByKeyword(options.Keyword)
titleQuery = inner_bleve.FuzzyQuery(options.Keyword, "title", fuzziness)
} else {
titleQuery = inner_bleve.MatchPhraseQuery(options.Keyword, "title", issueIndexerAnalyzer, fuzziness)
}

queries = append(queries, bleve.NewDisjunctionQuery([]query.Query{
inner_bleve.MatchPhraseQuery(options.Keyword, "title", issueIndexerAnalyzer, fuzziness),
titleQuery,
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 d23482c

Please sign in to comment.