Skip to content

Commit a0e0a30

Browse files
authored
Make SearchMode have default value and add comments (#33863)
* Make `SearchMode` have default value if it is empty * Add some comments for the "match" queries * Fix a copy-paste mistake in `buildMatchQuery` (`db.go`) * Add missing `q.Analyzer = repoIndexerAnalyzer`, it is in old code, although I do not see real difference ....
1 parent 45c4139 commit a0e0a30

File tree

8 files changed

+34
-19
lines changed

8 files changed

+34
-19
lines changed

modules/indexer/code/bleve/bleve.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/timeutil"
2727
"code.gitea.io/gitea/modules/typesniffer"
28+
"code.gitea.io/gitea/modules/util"
2829

2930
"github.com/blevesearch/bleve/v2"
3031
analyzer_custom "github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
@@ -272,14 +273,18 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
272273
pathQuery.FieldVal = "Filename"
273274
pathQuery.SetBoost(10)
274275

275-
if opts.SearchMode == indexer.SearchModeExact {
276+
searchMode := util.IfZero(opts.SearchMode, b.SupportedSearchModes()[0].ModeValue)
277+
if searchMode == indexer.SearchModeExact {
278+
// 1.21 used NewPrefixQuery, but it seems not working well, and later releases changed to NewMatchPhraseQuery
276279
q := bleve.NewMatchPhraseQuery(opts.Keyword)
280+
q.Analyzer = repoIndexerAnalyzer
277281
q.FieldVal = "Content"
278282
contentQuery = q
279283
} else /* words */ {
280284
q := bleve.NewMatchQuery(opts.Keyword)
281285
q.FieldVal = "Content"
282-
if opts.SearchMode == indexer.SearchModeFuzzy {
286+
q.Analyzer = repoIndexerAnalyzer
287+
if searchMode == indexer.SearchModeFuzzy {
283288
// this logic doesn't seem right, it is only used to pass the test-case `Keyword: "dESCRIPTION"`, which doesn't seem to be a real-life use-case.
284289
q.Fuzziness = inner_bleve.GuessFuzzinessByKeyword(opts.Keyword)
285290
} else {

modules/indexer/code/elasticsearch/elasticsearch.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/timeutil"
2727
"code.gitea.io/gitea/modules/typesniffer"
28+
"code.gitea.io/gitea/modules/util"
2829

2930
"github.com/go-enry/go-enry/v2"
3031
"github.com/olivere/elastic/v7"
@@ -365,7 +366,9 @@ func extractAggs(searchResult *elastic.SearchResult) []*internal.SearchResultLan
365366
// Search searches for codes and language stats by given conditions.
366367
func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
367368
var contentQuery elastic.Query
368-
if opts.SearchMode == indexer.SearchModeExact {
369+
searchMode := util.IfZero(opts.SearchMode, b.SupportedSearchModes()[0].ModeValue)
370+
if searchMode == indexer.SearchModeExact {
371+
// 1.21 used NewMultiMatchQuery().Type(esMultiMatchTypePhrasePrefix), but later releases changed to NewMatchPhraseQuery
369372
contentQuery = elastic.NewMatchPhraseQuery("content", opts.Keyword)
370373
} else /* words */ {
371374
contentQuery = elastic.NewMultiMatchQuery("content", opts.Keyword).Type(esMultiMatchTypeBestFields).Operator("and")

modules/indexer/code/indexer_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/modules/indexer/code/internal"
1818
"code.gitea.io/gitea/modules/setting"
1919
"code.gitea.io/gitea/modules/test"
20+
"code.gitea.io/gitea/modules/util"
2021

2122
_ "code.gitea.io/gitea/models"
2223
_ "code.gitea.io/gitea/models/actions"
@@ -240,7 +241,7 @@ func testIndexer(name string, t *testing.T, indexer internal.Indexer) {
240241
total, res, langs, err := indexer.Search(t.Context(), &internal.SearchOptions{
241242
RepoIDs: kw.RepoIDs,
242243
Keyword: kw.Keyword,
243-
SearchMode: kw.SearchMode,
244+
SearchMode: util.IfZero(kw.SearchMode, indexer_module.SearchModeWords),
244245
Paginator: &db.ListOptions{
245246
Page: 1,
246247
PageSize: 10,

modules/indexer/issues/bleve/bleve.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
1111
inner_bleve "code.gitea.io/gitea/modules/indexer/internal/bleve"
1212
"code.gitea.io/gitea/modules/indexer/issues/internal"
13+
"code.gitea.io/gitea/modules/util"
1314

1415
"github.com/blevesearch/bleve/v2"
1516
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
@@ -162,9 +163,10 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
162163
var queries []query.Query
163164

164165
if options.Keyword != "" {
165-
if options.SearchMode == indexer.SearchModeWords || options.SearchMode == indexer.SearchModeFuzzy {
166+
searchMode := util.IfZero(options.SearchMode, b.SupportedSearchModes()[0].ModeValue)
167+
if searchMode == indexer.SearchModeWords || searchMode == indexer.SearchModeFuzzy {
166168
fuzziness := 0
167-
if options.SearchMode == indexer.SearchModeFuzzy {
169+
if searchMode == indexer.SearchModeFuzzy {
168170
fuzziness = inner_bleve.GuessFuzzinessByKeyword(options.Keyword)
169171
}
170172
queries = append(queries, bleve.NewDisjunctionQuery([]query.Query{

modules/indexer/issues/db/db.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
1414
inner_db "code.gitea.io/gitea/modules/indexer/internal/db"
1515
"code.gitea.io/gitea/modules/indexer/issues/internal"
16+
"code.gitea.io/gitea/modules/util"
1617

1718
"xorm.io/builder"
1819
)
@@ -46,7 +47,7 @@ func (i *Indexer) Delete(_ context.Context, _ ...int64) error {
4647

4748
func buildMatchQuery(mode indexer.SearchModeType, colName, keyword string) builder.Cond {
4849
if mode == indexer.SearchModeExact {
49-
return db.BuildCaseInsensitiveLike("issue.name", keyword)
50+
return db.BuildCaseInsensitiveLike(colName, keyword)
5051
}
5152

5253
// match words
@@ -84,16 +85,16 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
8485
repoCond = builder.Eq{"repo_id": options.RepoIDs[0]}
8586
}
8687
subQuery := builder.Select("id").From("issue").Where(repoCond)
87-
88+
searchMode := util.IfZero(options.SearchMode, i.SupportedSearchModes()[0].ModeValue)
8889
cond = builder.Or(
89-
buildMatchQuery(options.SearchMode, "issue.name", options.Keyword),
90-
buildMatchQuery(options.SearchMode, "issue.content", options.Keyword),
90+
buildMatchQuery(searchMode, "issue.name", options.Keyword),
91+
buildMatchQuery(searchMode, "issue.content", options.Keyword),
9192
builder.In("issue.id", builder.Select("issue_id").
9293
From("comment").
9394
Where(builder.And(
9495
builder.Eq{"type": issue_model.CommentTypeComment},
9596
builder.In("issue_id", subQuery),
96-
buildMatchQuery(options.SearchMode, "content", options.Keyword),
97+
buildMatchQuery(searchMode, "content", options.Keyword),
9798
)),
9899
),
99100
)

modules/indexer/issues/elasticsearch/elasticsearch.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
1515
inner_elasticsearch "code.gitea.io/gitea/modules/indexer/internal/elasticsearch"
1616
"code.gitea.io/gitea/modules/indexer/issues/internal"
17+
"code.gitea.io/gitea/modules/util"
1718

1819
"github.com/olivere/elastic/v7"
1920
)
@@ -152,7 +153,8 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
152153
query := elastic.NewBoolQuery()
153154

154155
if options.Keyword != "" {
155-
if options.SearchMode == indexer.SearchModeExact {
156+
searchMode := util.IfZero(options.SearchMode, b.SupportedSearchModes()[0].ModeValue)
157+
if searchMode == indexer.SearchModeExact {
156158
query.Must(elastic.NewMultiMatchQuery(options.Keyword, "title", "content", "comments").Type(esMultiMatchTypePhrasePrefix))
157159
} else /* words */ {
158160
query.Must(elastic.NewMultiMatchQuery(options.Keyword, "title", "content", "comments").Type(esMultiMatchTypeBestFields).Operator("and"))

modules/indexer/issues/indexer.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ const (
282282

283283
// SearchIssues search issues by options.
284284
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
285-
indexer := *globalIndexer.Load()
285+
ix := *globalIndexer.Load()
286286

287287
if opts.Keyword == "" || opts.IsKeywordNumeric() {
288288
// This is a conservative shortcut.
@@ -291,10 +291,9 @@ func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, err
291291
// So if the user creates an issue and list issues immediately, the issue may not be listed because the indexer needs time to index the issue.
292292
// Even worse, the external indexer like elastic search may not be available for a while,
293293
// and the user may not be able to list issues completely until it is available again.
294-
indexer = db.NewIndexer()
294+
ix = db.NewIndexer()
295295
}
296-
297-
result, err := indexer.Search(ctx, opts)
296+
result, err := ix.Search(ctx, opts)
298297
if err != nil {
299298
return nil, 0, err
300299
}

modules/indexer/issues/indexer_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ func searchIssueWithKeyword(t *testing.T) {
8282
}
8383

8484
for _, test := range tests {
85-
issueIDs, _, err := SearchIssues(t.Context(), &test.opts)
86-
require.NoError(t, err)
87-
assert.Equal(t, test.expectedIDs, issueIDs)
85+
t.Run(test.opts.Keyword, func(t *testing.T) {
86+
issueIDs, _, err := SearchIssues(t.Context(), &test.opts)
87+
require.NoError(t, err)
88+
assert.Equal(t, test.expectedIDs, issueIDs)
89+
})
8890
}
8991
}
9092

0 commit comments

Comments
 (0)