Skip to content

Commit 516af5c

Browse files
committed
fix
1 parent 403775e commit 516af5c

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

modules/indexer/code/bleve/bleve.go

Lines changed: 7 additions & 2 deletions
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

Lines changed: 4 additions & 1 deletion
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/issues/bleve/bleve.go

Lines changed: 4 additions & 2 deletions
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

Lines changed: 5 additions & 4 deletions
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
)
@@ -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

Lines changed: 3 additions & 1 deletion
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"))

0 commit comments

Comments
 (0)