From 6b2fb782c8c7b8d184b66560bfd6236bdc43cf83 Mon Sep 17 00:00:00 2001 From: Will Assis <35489495+gassiss@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:55:23 -0400 Subject: [PATCH] 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 --- pkg/storage/unified/search/bleve.go | 19 ++++++++++++++++++- .../unified/search/bleve_search_test.go | 16 ++++++++++++++++ .../unified/search/custom_analyzers.go | 3 ++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/storage/unified/search/bleve.go b/pkg/storage/unified/search/bleve.go index 58b422176b4..002da763f67 100644 --- a/pkg/storage/unified/search/bleve.go +++ b/pkg/storage/unified/search/bleve.go @@ -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() diff --git a/pkg/storage/unified/search/bleve_search_test.go b/pkg/storage/unified/search/bleve_search_test.go index 6e54f1ded2a..3b65ba62316 100644 --- a/pkg/storage/unified/search/bleve_search_test.go +++ b/pkg/storage/unified/search/bleve_search_test.go @@ -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 { diff --git a/pkg/storage/unified/search/custom_analyzers.go b/pkg/storage/unified/search/custom_analyzers.go index 51e2176298b..851f2667d33 100644 --- a/pkg/storage/unified/search/custom_analyzers.go +++ b/pkg/storage/unified/search/custom_analyzers.go @@ -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, }