Skip to content

Commit

Permalink
enable literal string for code search
Browse files Browse the repository at this point in the history
Close: #33588
  • Loading branch information
darren committed Feb 14, 2025
1 parent d88b012 commit dcb58f0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
17 changes: 12 additions & 5 deletions modules/indexer/code/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,24 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
var (
indexerQuery query.Query
keywordQuery query.Query
contentQuery query.Query
)

pathQuery := bleve.NewPrefixQuery(strings.ToLower(opts.Keyword))
pathQuery.FieldVal = "Filename"
pathQuery.SetBoost(10)

contentQuery := bleve.NewMatchQuery(opts.Keyword)
contentQuery.FieldVal = "Content"

if opts.IsKeywordFuzzy {
contentQuery.Fuzziness = inner_bleve.GuessFuzzinessByKeyword(opts.Keyword)
if opts.IsKeywordLiteral {
q := bleve.NewMatchPhraseQuery(opts.Keyword)
q.FieldVal = "Content"
contentQuery = q
} else {
q := bleve.NewMatchQuery(opts.Keyword)
q.FieldVal = "Content"
if opts.IsKeywordFuzzy {
q.Fuzziness = inner_bleve.GuessFuzzinessByKeyword(opts.Keyword)
}
contentQuery = q
}

keywordQuery = bleve.NewDisjunctionQuery(contentQuery, pathQuery)
Expand Down
3 changes: 2 additions & 1 deletion modules/indexer/code/internal/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type SearchOptions struct {
Keyword string
Language string

IsKeywordFuzzy bool
IsKeywordFuzzy bool
IsKeywordLiteral bool

db.Paginator
}
Expand Down
14 changes: 11 additions & 3 deletions routers/common/codesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
package common

import (
"strings"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/context"
)

func PrepareCodeSearch(ctx *context.Context) (ret struct {
Keyword string
Language string
IsFuzzy bool
Keyword string
Language string
IsFuzzy bool
IsLiteral bool
},
) {
ret.Language = ctx.FormTrim("l")
Expand All @@ -35,5 +38,10 @@ func PrepareCodeSearch(ctx *context.Context) (ret struct {
ctx.Data["IsFuzzy"] = isFuzzy

ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled

if strings.HasPrefix(ret.Keyword, "\"") && strings.HasSuffix(ret.Keyword, "\"") {
ret.Keyword = strings.Trim(ret.Keyword, "\"")
ret.IsLiteral = true
}
return ret
}
9 changes: 5 additions & 4 deletions routers/web/explore/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ func Code(ctx *context.Context) {

if (len(repoIDs) > 0) || isAdmin {
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
RepoIDs: repoIDs,
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
Language: prepareSearch.Language,
RepoIDs: repoIDs,
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
IsKeywordLiteral: prepareSearch.IsLiteral,
Language: prepareSearch.Language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
Expand Down
9 changes: 5 additions & 4 deletions routers/web/repo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func Search(ctx *context.Context) {
if setting.Indexer.RepoIndexerEnabled {
var err error
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
RepoIDs: []int64{ctx.Repo.Repository.ID},
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
Language: prepareSearch.Language,
RepoIDs: []int64{ctx.Repo.Repository.ID},
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
IsKeywordLiteral: prepareSearch.IsLiteral,
Language: prepareSearch.Language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
Expand Down
9 changes: 5 additions & 4 deletions routers/web/user/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ func CodeSearch(ctx *context.Context) {

if len(repoIDs) > 0 {
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
RepoIDs: repoIDs,
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
Language: prepareSearch.Language,
RepoIDs: repoIDs,
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
IsKeywordLiteral: prepareSearch.IsLiteral,
Language: prepareSearch.Language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
Expand Down

0 comments on commit dcb58f0

Please sign in to comment.