Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add config option to enable/disable default fuzzy search #29589

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,10 @@ LEVEL = Info
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; If the indexer type supports fuzzy search use it by default. Is used by issue- and repo-indexer alike.
;INDEXER_DEFAULT_FUZZY = true
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Issue Indexer settings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
Expand Down
1 change: 1 addition & 0 deletions docs/content/administration/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ relation to port exhaustion.

## Indexer (`indexer`)

- `INDEXER_DEFAULT_FUZZY`: **true**: If the indexer type supports fuzzy search use it by default. Is used by issue- and repo-indexer alike.
- `ISSUE_INDEXER_TYPE`: **bleve**: Issue indexer type, currently supported: `bleve`, `db`, `elasticsearch` or `meilisearch`.
- `ISSUE_INDEXER_CONN_STR`: ****: Issue indexer connection string, available when ISSUE_INDEXER_TYPE is elasticsearch (e.g. http://elastic:password@localhost:9200) or meilisearch (e.g. http://:apikey@localhost:7700)
- `ISSUE_INDEXER_NAME`: **gitea_issues**: Issue indexer name, available when ISSUE_INDEXER_TYPE is elasticsearch or meilisearch.
Expand Down
8 changes: 7 additions & 1 deletion modules/indexer/code/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,17 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
prefixQuery := bleve.NewPrefixQuery(keyword)
prefixQuery.FieldVal = "Content"
keywordQuery = prefixQuery
} else {
} else if !setting.Indexer.DefaultFuzzy {
phraseQuery := bleve.NewMatchPhraseQuery(keyword)
phraseQuery.FieldVal = "Content"
phraseQuery.Analyzer = repoIndexerAnalyzer
keywordQuery = phraseQuery
} else {
fuzzyQuery := bleve.NewFuzzyQuery(keyword)
fuzzyQuery.FieldVal = "Content"
fuzzyQuery.SetPrefix(len(keyword))
fuzzyQuery.Fuzziness = 2 //Levenshtein distance
keywordQuery = fuzzyQuery
}

if len(repoIDs) > 0 {
Expand Down
3 changes: 3 additions & 0 deletions modules/indexer/code/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
}

kwQuery := elastic.NewMultiMatchQuery(keyword, "content").Type(searchType)
if !setting.Indexer.DefaultFuzzy {
kwQuery = kwQuery.Fuzziness("0")
}
query := elastic.NewBoolQuery()
query = query.Must(kwQuery)
if len(repoIDs) > 0 {
Expand Down
1 change: 1 addition & 0 deletions modules/indexer/code/internal/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Indexer interface {
internal.Indexer
Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *RepoChanges) error
Delete(ctx context.Context, repoID int64) error
// TODO: use search option struct
Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error)
}

Expand Down
7 changes: 6 additions & 1 deletion modules/indexer/issues/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
inner_elasticsearch "code.gitea.io/gitea/modules/indexer/internal/elasticsearch"
"code.gitea.io/gitea/modules/indexer/issues/internal"
"code.gitea.io/gitea/modules/setting"

"github.com/olivere/elastic/v7"
)
Expand Down Expand Up @@ -141,7 +142,11 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
query := elastic.NewBoolQuery()

if options.Keyword != "" {
query.Must(elastic.NewMultiMatchQuery(options.Keyword, "title", "content", "comments"))
keywordQuery := elastic.NewMultiMatchQuery(options.Keyword, "title", "content", "comments")
if !setting.Indexer.DefaultFuzzy {
keywordQuery = keywordQuery.Fuzziness("0")
}
query.Must(keywordQuery)
}

if len(options.RepoIDs) > 0 {
Expand Down
7 changes: 7 additions & 0 deletions modules/setting/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

// Indexer settings
var Indexer = struct {
DefaultFuzzy bool

IssueType string
IssuePath string
IssueConnStr string
Expand All @@ -34,6 +36,8 @@ var Indexer = struct {
ExcludePatterns []glob.Glob
ExcludeVendored bool
}{
DefaultFuzzy: true,

IssueType: "bleve",
IssuePath: "indexers/issues.bleve",
IssueConnStr: "",
Expand All @@ -52,6 +56,9 @@ var Indexer = struct {

func loadIndexerFrom(rootCfg ConfigProvider) {
sec := rootCfg.Section("indexer")

Indexer.DefaultFuzzy = sec.Key("INDEXER_DEFAULT_FUZZY").MustBool(Indexer.DefaultFuzzy)

Indexer.IssueType = sec.Key("ISSUE_INDEXER_TYPE").MustString("bleve")
if Indexer.IssueType == "bleve" {
Indexer.IssuePath = filepath.ToSlash(sec.Key("ISSUE_INDEXER_PATH").MustString(filepath.ToSlash(filepath.Join(AppDataPath, "indexers/issues.bleve"))))
Expand Down
Loading