Skip to content

Commit dcb58f0

Browse files
committed
enable literal string for code search
Close: go-gitea#33588
1 parent d88b012 commit dcb58f0

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

modules/indexer/code/bleve/bleve.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,24 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
260260
var (
261261
indexerQuery query.Query
262262
keywordQuery query.Query
263+
contentQuery query.Query
263264
)
264265

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

269-
contentQuery := bleve.NewMatchQuery(opts.Keyword)
270-
contentQuery.FieldVal = "Content"
271-
272-
if opts.IsKeywordFuzzy {
273-
contentQuery.Fuzziness = inner_bleve.GuessFuzzinessByKeyword(opts.Keyword)
270+
if opts.IsKeywordLiteral {
271+
q := bleve.NewMatchPhraseQuery(opts.Keyword)
272+
q.FieldVal = "Content"
273+
contentQuery = q
274+
} else {
275+
q := bleve.NewMatchQuery(opts.Keyword)
276+
q.FieldVal = "Content"
277+
if opts.IsKeywordFuzzy {
278+
q.Fuzziness = inner_bleve.GuessFuzzinessByKeyword(opts.Keyword)
279+
}
280+
contentQuery = q
274281
}
275282

276283
keywordQuery = bleve.NewDisjunctionQuery(contentQuery, pathQuery)

modules/indexer/code/internal/indexer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ type SearchOptions struct {
2525
Keyword string
2626
Language string
2727

28-
IsKeywordFuzzy bool
28+
IsKeywordFuzzy bool
29+
IsKeywordLiteral bool
2930

3031
db.Paginator
3132
}

routers/common/codesearch.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
package common
55

66
import (
7+
"strings"
8+
79
"code.gitea.io/gitea/modules/setting"
810
"code.gitea.io/gitea/services/context"
911
)
1012

1113
func PrepareCodeSearch(ctx *context.Context) (ret struct {
12-
Keyword string
13-
Language string
14-
IsFuzzy bool
14+
Keyword string
15+
Language string
16+
IsFuzzy bool
17+
IsLiteral bool
1518
},
1619
) {
1720
ret.Language = ctx.FormTrim("l")
@@ -35,5 +38,10 @@ func PrepareCodeSearch(ctx *context.Context) (ret struct {
3538
ctx.Data["IsFuzzy"] = isFuzzy
3639

3740
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
41+
42+
if strings.HasPrefix(ret.Keyword, "\"") && strings.HasSuffix(ret.Keyword, "\"") {
43+
ret.Keyword = strings.Trim(ret.Keyword, "\"")
44+
ret.IsLiteral = true
45+
}
3846
return ret
3947
}

routers/web/explore/code.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ func Code(ctx *context.Context) {
7272

7373
if (len(repoIDs) > 0) || isAdmin {
7474
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
75-
RepoIDs: repoIDs,
76-
Keyword: prepareSearch.Keyword,
77-
IsKeywordFuzzy: prepareSearch.IsFuzzy,
78-
Language: prepareSearch.Language,
75+
RepoIDs: repoIDs,
76+
Keyword: prepareSearch.Keyword,
77+
IsKeywordFuzzy: prepareSearch.IsFuzzy,
78+
IsKeywordLiteral: prepareSearch.IsLiteral,
79+
Language: prepareSearch.Language,
7980
Paginator: &db.ListOptions{
8081
Page: page,
8182
PageSize: setting.UI.RepoSearchPagingNum,

routers/web/repo/search.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ func Search(ctx *context.Context) {
4848
if setting.Indexer.RepoIndexerEnabled {
4949
var err error
5050
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
51-
RepoIDs: []int64{ctx.Repo.Repository.ID},
52-
Keyword: prepareSearch.Keyword,
53-
IsKeywordFuzzy: prepareSearch.IsFuzzy,
54-
Language: prepareSearch.Language,
51+
RepoIDs: []int64{ctx.Repo.Repository.ID},
52+
Keyword: prepareSearch.Keyword,
53+
IsKeywordFuzzy: prepareSearch.IsFuzzy,
54+
IsKeywordLiteral: prepareSearch.IsLiteral,
55+
Language: prepareSearch.Language,
5556
Paginator: &db.ListOptions{
5657
Page: page,
5758
PageSize: setting.UI.RepoSearchPagingNum,

routers/web/user/code.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ func CodeSearch(ctx *context.Context) {
6868

6969
if len(repoIDs) > 0 {
7070
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
71-
RepoIDs: repoIDs,
72-
Keyword: prepareSearch.Keyword,
73-
IsKeywordFuzzy: prepareSearch.IsFuzzy,
74-
Language: prepareSearch.Language,
71+
RepoIDs: repoIDs,
72+
Keyword: prepareSearch.Keyword,
73+
IsKeywordFuzzy: prepareSearch.IsFuzzy,
74+
IsKeywordLiteral: prepareSearch.IsLiteral,
75+
Language: prepareSearch.Language,
7576
Paginator: &db.ListOptions{
7677
Page: page,
7778
PageSize: setting.UI.RepoSearchPagingNum,

0 commit comments

Comments
 (0)