From c54496a3f9e6c418e6d718c8bf0e69104fe3b6b1 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Wed, 2 Apr 2025 15:43:08 +0300 Subject: [PATCH] Search: Avoid panic with missing title (#103257) --- .../dashboards/service/search/search.go | 21 ++++++++++++------- pkg/storage/unified/search/bleve.go | 11 +++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/pkg/services/dashboards/service/search/search.go b/pkg/services/dashboards/service/search/search.go index 0d356fe32cd..ed53dc73407 100644 --- a/pkg/services/dashboards/service/search/search.go +++ b/pkg/services/dashboards/service/search/search.go @@ -46,11 +46,11 @@ func ParseResults(result *resource.ResourceSearchResponse, offset int64) (v0alph return v0alpha1.SearchResults{}, nil } - titleIDX := 0 + titleIDX := -1 folderIDX := -1 tagsIDX := -1 - scoreIDX := 0 - explainIDX := 0 + scoreIDX := -1 + explainIDX := -1 for i, v := range result.Results.Columns { switch v.Name { @@ -101,19 +101,24 @@ func ParseResults(result *resource.ResourceSearchResponse, offset int64) (v0alph hit := &v0alpha1.DashboardHit{ Resource: row.Key.Resource, // folders | dashboards Name: row.Key.Name, // The Grafana UID - Title: string(row.Cells[titleIDX]), Field: fields, } - if folderIDX > 0 && row.Cells[folderIDX] != nil { + if titleIDX >= 0 && row.Cells[titleIDX] != nil { + hit.Title = string(row.Cells[titleIDX]) + } else { + hit.Title = "(no title)" + } + + if folderIDX >= 0 && row.Cells[folderIDX] != nil { hit.Folder = string(row.Cells[folderIDX]) } - if tagsIDX > 0 && row.Cells[tagsIDX] != nil { + if tagsIDX >= 0 && row.Cells[tagsIDX] != nil { _ = json.Unmarshal(row.Cells[tagsIDX], &hit.Tags) } - if explainIDX > 0 && row.Cells[explainIDX] != nil { + if explainIDX >= 0 && row.Cells[explainIDX] != nil { _ = json.Unmarshal(row.Cells[explainIDX], &hit.Explain) } - if scoreIDX > 0 && row.Cells[scoreIDX] != nil { + if scoreIDX >= 0 && row.Cells[scoreIDX] != nil { _, _ = binary.Decode(row.Cells[scoreIDX], binary.BigEndian, &hit.Score) } diff --git a/pkg/storage/unified/search/bleve.go b/pkg/storage/unified/search/bleve.go index 0203fe186de..f485ce72e32 100644 --- a/pkg/storage/unified/search/bleve.go +++ b/pkg/storage/unified/search/bleve.go @@ -538,7 +538,16 @@ func (b *bleveIndex) Search( if err != nil { return nil, err } - searchrequest.Fields = f + if len(f) > 0 { + searchrequest.Fields = f + } else { + searchrequest.Fields = []string{ + resource.SEARCH_FIELD_TITLE, + resource.SEARCH_FIELD_FOLDER, + resource.SEARCH_FIELD_SOURCE_PATH, + resource.SEARCH_FIELD_MANAGED_BY, + } + } } res, err := index.SearchInContext(ctx, searchrequest)