Skip to content

Commit

Permalink
simplify code per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
darren committed Feb 14, 2025
1 parent dcb58f0 commit e87e418
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 29 deletions.
3 changes: 2 additions & 1 deletion modules/indexer/code/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
pathQuery.FieldVal = "Filename"
pathQuery.SetBoost(10)

if opts.IsKeywordLiteral {
if strings.HasPrefix(opts.Keyword, "\"") && strings.HasSuffix(opts.Keyword, "\"") {
opts.Keyword = strings.Trim(opts.Keyword, "\"")
q := bleve.NewMatchPhraseQuery(opts.Keyword)
q.FieldVal = "Content"
contentQuery = q
Expand Down
3 changes: 1 addition & 2 deletions modules/indexer/code/internal/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type SearchOptions struct {
Keyword string
Language string

IsKeywordFuzzy bool
IsKeywordLiteral bool
IsKeywordFuzzy bool

db.Paginator
}
Expand Down
14 changes: 3 additions & 11 deletions routers/common/codesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
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
IsLiteral bool
Keyword string
Language string
IsFuzzy bool
},
) {
ret.Language = ctx.FormTrim("l")
Expand All @@ -38,10 +35,5 @@ 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: 4 additions & 5 deletions routers/web/explore/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ 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,
IsKeywordLiteral: prepareSearch.IsLiteral,
Language: prepareSearch.Language,
RepoIDs: repoIDs,
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
Language: prepareSearch.Language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
Expand Down
9 changes: 4 additions & 5 deletions routers/web/repo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ 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,
IsKeywordLiteral: prepareSearch.IsLiteral,
Language: prepareSearch.Language,
RepoIDs: []int64{ctx.Repo.Repository.ID},
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
Language: prepareSearch.Language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
Expand Down
9 changes: 4 additions & 5 deletions routers/web/user/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ 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,
IsKeywordLiteral: prepareSearch.IsLiteral,
Language: prepareSearch.Language,
RepoIDs: repoIDs,
Keyword: prepareSearch.Keyword,
IsKeywordFuzzy: prepareSearch.IsFuzzy,
Language: prepareSearch.Language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.UI.RepoSearchPagingNum,
Expand Down

0 comments on commit e87e418

Please sign in to comment.