Skip to content

Commit e3021fa

Browse files
authored
Use MatchPhraseQuery for bleve code search (#33628)
Fix regression from #32210 which unintentionally changed the search mode for bleve from MaatchPhraseQuery to MatchQuery. On the main branch, meanwhile with #33590 a "literal code search" mode (by using quotes) was implemented as workaround for this unexpected code search behavior. Maybe that feature needs some redesign as it turns out to have been caused by a regression. But this PR at least already fixes the regression for 1.23.x
1 parent 81126da commit e3021fa

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

modules/indexer/code/bleve/bleve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int
266266
pathQuery.FieldVal = "Filename"
267267
pathQuery.SetBoost(10)
268268

269-
contentQuery := bleve.NewMatchQuery(opts.Keyword)
269+
contentQuery := bleve.NewMatchPhraseQuery(opts.Keyword)
270270
contentQuery.FieldVal = "Content"
271271

272272
if opts.IsKeywordFuzzy {

modules/indexer/code/indexer_test.go

+41-29
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,6 @@ func testIndexer(name string, t *testing.T, indexer internal.Indexer) {
165165
},
166166
},
167167
},
168-
// Search for matches on the contents of files within the repo '62'.
169-
// This scenario yields two results (both are based on contents, the first one is an exact match where as the second is a 'fuzzy' one)
170-
{
171-
RepoIDs: []int64{62},
172-
Keyword: "This is not cheese",
173-
Langs: 1,
174-
Results: []codeSearchResult{
175-
{
176-
Filename: "potato/ham.md",
177-
Content: "This is not cheese",
178-
},
179-
{
180-
Filename: "ham.md",
181-
Content: "This is also not cheese",
182-
},
183-
},
184-
},
185-
// Search for matches on the contents of files regardless of case.
186-
{
187-
RepoIDs: nil,
188-
Keyword: "dESCRIPTION",
189-
Langs: 1,
190-
Results: []codeSearchResult{
191-
{
192-
Filename: "README.md",
193-
Content: "# repo1\n\nDescription for repo1",
194-
},
195-
},
196-
},
197168
// Search for an exact match on the filename within the repo '62' (case insenstive).
198169
// This scenario yields a single result (the file avocado.md on the repo '62')
199170
{
@@ -233,6 +204,47 @@ func testIndexer(name string, t *testing.T, indexer internal.Indexer) {
233204
},
234205
}
235206

207+
if name == "elastic_search" {
208+
// Additional scenarios for elastic_search only
209+
additional := []struct {
210+
RepoIDs []int64
211+
Keyword string
212+
Langs int
213+
Results []codeSearchResult
214+
}{
215+
// Search for matches on the contents of files within the repo '62'.
216+
// This scenario yields two results (both are based on contents, the first one is an exact match where as the second is a 'fuzzy' one)
217+
{
218+
RepoIDs: []int64{62},
219+
Keyword: "This is not cheese",
220+
Langs: 1,
221+
Results: []codeSearchResult{
222+
{
223+
Filename: "potato/ham.md",
224+
Content: "This is not cheese",
225+
},
226+
{
227+
Filename: "ham.md",
228+
Content: "This is also not cheese",
229+
},
230+
},
231+
},
232+
// Search for matches on the contents of files regardless of case.
233+
{
234+
RepoIDs: nil,
235+
Keyword: "dESCRIPTION",
236+
Langs: 1,
237+
Results: []codeSearchResult{
238+
{
239+
Filename: "README.md",
240+
Content: "# repo1\n\nDescription for repo1",
241+
},
242+
},
243+
},
244+
}
245+
keywords = append(keywords, additional...)
246+
}
247+
236248
for _, kw := range keywords {
237249
t.Run(kw.Keyword, func(t *testing.T) {
238250
total, res, langs, err := indexer.Search(context.TODO(), &internal.SearchOptions{

0 commit comments

Comments
 (0)