@@ -17,6 +17,7 @@ import (
17
17
"code.gitea.io/gitea/modules/charset"
18
18
"code.gitea.io/gitea/modules/git"
19
19
"code.gitea.io/gitea/modules/gitrepo"
20
+ path_filter "code.gitea.io/gitea/modules/indexer/code/bleve/token/path"
20
21
"code.gitea.io/gitea/modules/indexer/code/internal"
21
22
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
22
23
inner_bleve "code.gitea.io/gitea/modules/indexer/internal/bleve"
@@ -53,6 +54,7 @@ type RepoIndexerData struct {
53
54
RepoID int64
54
55
CommitID string
55
56
Content string
57
+ Filename string
56
58
Language string
57
59
UpdatedAt time.Time
58
60
}
@@ -64,8 +66,10 @@ func (d *RepoIndexerData) Type() string {
64
66
65
67
const (
66
68
repoIndexerAnalyzer = "repoIndexerAnalyzer"
69
+ filenameIndexerAnalyzer = "filenameIndexerAnalyzer"
70
+ filenameIndexerTokenizer = "filenameIndexerTokenizer"
67
71
repoIndexerDocType = "repoIndexerDocType"
68
- repoIndexerLatestVersion = 6
72
+ repoIndexerLatestVersion = 7
69
73
)
70
74
71
75
// generateBleveIndexMapping generates a bleve index mapping for the repo indexer
@@ -79,6 +83,11 @@ func generateBleveIndexMapping() (mapping.IndexMapping, error) {
79
83
textFieldMapping .IncludeInAll = false
80
84
docMapping .AddFieldMappingsAt ("Content" , textFieldMapping )
81
85
86
+ fileNamedMapping := bleve .NewTextFieldMapping ()
87
+ fileNamedMapping .IncludeInAll = false
88
+ fileNamedMapping .Analyzer = filenameIndexerAnalyzer
89
+ docMapping .AddFieldMappingsAt ("Filename" , fileNamedMapping )
90
+
82
91
termFieldMapping := bleve .NewTextFieldMapping ()
83
92
termFieldMapping .IncludeInAll = false
84
93
termFieldMapping .Analyzer = analyzer_keyword .Name
@@ -90,6 +99,7 @@ func generateBleveIndexMapping() (mapping.IndexMapping, error) {
90
99
docMapping .AddFieldMappingsAt ("UpdatedAt" , timeFieldMapping )
91
100
92
101
mapping := bleve .NewIndexMapping ()
102
+
93
103
if err := addUnicodeNormalizeTokenFilter (mapping ); err != nil {
94
104
return nil , err
95
105
} else if err := mapping .AddCustomAnalyzer (repoIndexerAnalyzer , map [string ]any {
@@ -100,6 +110,16 @@ func generateBleveIndexMapping() (mapping.IndexMapping, error) {
100
110
}); err != nil {
101
111
return nil , err
102
112
}
113
+
114
+ if err := mapping .AddCustomAnalyzer (filenameIndexerAnalyzer , map [string ]any {
115
+ "type" : analyzer_custom .Name ,
116
+ "char_filters" : []string {},
117
+ "tokenizer" : unicode .Name ,
118
+ "token_filters" : []string {unicodeNormalizeName , path_filter .Name , lowercase .Name },
119
+ }); err != nil {
120
+ return nil , err
121
+ }
122
+
103
123
mapping .DefaultAnalyzer = repoIndexerAnalyzer
104
124
mapping .AddDocumentMapping (repoIndexerDocType , docMapping )
105
125
mapping .AddDocumentMapping ("_all" , bleve .NewDocumentDisabledMapping ())
@@ -174,6 +194,7 @@ func (b *Indexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserErro
174
194
return batch .Index (id , & RepoIndexerData {
175
195
RepoID : repo .ID ,
176
196
CommitID : commitSha ,
197
+ Filename : update .Filename ,
177
198
Content : string (charset .ToUTF8DropErrors (fileContents , charset.ConvertOpts {})),
178
199
Language : analyze .GetCodeLanguage (update .Filename , fileContents ),
179
200
UpdatedAt : time .Now ().UTC (),
@@ -240,14 +261,19 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
240
261
keywordQuery query.Query
241
262
)
242
263
243
- phraseQuery := bleve .NewMatchPhraseQuery (opts .Keyword )
244
- phraseQuery .FieldVal = "Content"
245
- phraseQuery .Analyzer = repoIndexerAnalyzer
246
- keywordQuery = phraseQuery
264
+ pathQuery := bleve .NewPrefixQuery (strings .ToLower (opts .Keyword ))
265
+ pathQuery .FieldVal = "Filename"
266
+ pathQuery .SetBoost (10 )
267
+
268
+ contentQuery := bleve .NewMatchQuery (opts .Keyword )
269
+ contentQuery .FieldVal = "Content"
270
+
247
271
if opts .IsKeywordFuzzy {
248
- phraseQuery .Fuzziness = inner_bleve .GuessFuzzinessByKeyword (opts .Keyword )
272
+ contentQuery .Fuzziness = inner_bleve .GuessFuzzinessByKeyword (opts .Keyword )
249
273
}
250
274
275
+ keywordQuery = bleve .NewDisjunctionQuery (contentQuery , pathQuery )
276
+
251
277
if len (opts .RepoIDs ) > 0 {
252
278
repoQueries := make ([]query.Query , 0 , len (opts .RepoIDs ))
253
279
for _ , repoID := range opts .RepoIDs {
@@ -277,7 +303,7 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
277
303
278
304
from , pageSize := opts .GetSkipTake ()
279
305
searchRequest := bleve .NewSearchRequestOptions (indexerQuery , pageSize , from , false )
280
- searchRequest .Fields = []string {"Content" , "RepoID" , "Language" , "CommitID" , "UpdatedAt" }
306
+ searchRequest .Fields = []string {"Content" , "Filename" , " RepoID" , "Language" , "CommitID" , "UpdatedAt" }
281
307
searchRequest .IncludeLocations = true
282
308
283
309
if len (opts .Language ) == 0 {
@@ -307,6 +333,10 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
307
333
endIndex = locationEnd
308
334
}
309
335
}
336
+ if len (hit .Locations ["Filename" ]) > 0 {
337
+ startIndex , endIndex = internal .FilenameMatchIndexPos (hit .Fields ["Content" ].(string ))
338
+ }
339
+
310
340
language := hit .Fields ["Language" ].(string )
311
341
var updatedUnix timeutil.TimeStamp
312
342
if t , err := time .Parse (time .RFC3339 , hit .Fields ["UpdatedAt" ].(string )); err == nil {
0 commit comments