fix (unified-storage): search returning empty results when query includes small term (#111140)

* fix search not returning anything when a word in the query has less than 3 characters

* fix test name

* fmt

* remove trimspace
This commit is contained in:
Will Assis
2025-09-16 08:55:23 -04:00
committed by GitHub
parent 81fe57478f
commit 6b2fb782c8
3 changed files with 36 additions and 2 deletions
+18 -1
View File
@@ -1092,7 +1092,7 @@ func (b *bleveIndex) toBleveSearchRequest(ctx context.Context, req *resourcepb.R
queryPhrase.Analyzer = standard.Name
// Query 3: Match query with standard analyzer
queryAnalyzed := bleve.NewMatchQuery(req.Query)
queryAnalyzed := bleve.NewMatchQuery(removeSmallTerms(req.Query))
queryAnalyzed.Analyzer = standard.Name
queryAnalyzed.Operator = query.MatchQueryOperatorAnd // Make sure all terms from the query are matched
@@ -1178,6 +1178,23 @@ func (b *bleveIndex) toBleveSearchRequest(ctx context.Context, req *resourcepb.R
return searchrequest, nil
}
func removeSmallTerms(query string) string {
words := strings.Fields(query)
validWords := make([]string, 0, len(words))
for _, word := range words {
if len(word) >= EDGE_NGRAM_MIN_TOKEN {
validWords = append(validWords, word)
}
}
if len(validWords) == 0 {
return query
}
return strings.Join(validWords, " ")
}
func (b *bleveIndex) stopUpdaterAndCloseIndex() error {
// Signal updater to stop. We do this by 1) setting updaterShuttingDown + sending signal, and by 2) calling cancel.
b.updaterMu.Lock()
@@ -191,6 +191,22 @@ func TestCanSearchByTitle(t *testing.T) {
checkSearchQuery(t, index, newQueryByTitle(fmt.Sprintf(`foo%d%s`, i, v)), []string{name})
}
})
t.Run("title search will ignore terms < 3 characters", func(t *testing.T) {
index := newTestDashboardsIndex(t, threshold, 2, 2, noop)
indexDocumentsWithTitles(t, index, key, map[string]string{
"name1": "new dashboard",
"name2": "new dash",
"name3": "new",
})
// matches everything
checkSearchQuery(t, index, newTestQuery("new"), []string{"name3", "name2", "name1"})
// ignore terms shorter than 3 chars
checkSearchQuery(t, index, newTestQuery("new d"), []string{"name3", "name2", "name1"})
// include terms shorter that are exactly 3 chars
checkSearchQuery(t, index, newTestQuery("new das"), []string{"name2", "name1"})
})
}
func newTestQuery(query string) *resourcepb.ResourceSearchRequest {
@@ -10,6 +10,7 @@ import (
)
const TITLE_ANALYZER = "title_analyzer"
const EDGE_NGRAM_MIN_TOKEN = 3.0
func RegisterCustomAnalyzers(mapper *mapping.IndexMappingImpl) error {
return registerTitleAnalyzer(mapper)
@@ -22,7 +23,7 @@ func registerTitleAnalyzer(mapper *mapping.IndexMappingImpl) error {
// Define an N-Gram tokenizer (for substring search)
edgeNgramTokenFilter := map[string]interface{}{
"type": edgengram.Name,
"min": 3.0,
"min": EDGE_NGRAM_MIN_TOKEN,
"max": 10.0,
"back": edgengram.FRONT,
}