diff --git a/pkg/storage/unified/search/bleve.go b/pkg/storage/unified/search/bleve.go index 7e96a10da26..211bef376cc 100644 --- a/pkg/storage/unified/search/bleve.go +++ b/pkg/storage/unified/search/bleve.go @@ -1218,16 +1218,19 @@ func (b *bleveIndex) toBleveSearchRequest(ctx context.Context, req *resourcepb.R // Query 1: Match the exact query string queryExact := bleve.NewMatchQuery(req.Query) queryExact.SetBoost(10.0) + queryExact.SetField(resource.SEARCH_FIELD_TITLE) queryExact.Analyzer = keyword.Name // don't analyze the query input - treat it as a single token queryExact.Operator = query.MatchQueryOperatorAnd // This doesn't make a difference for keyword analyzer, we add it just to be explicit. // Query 2: Phrase query with standard analyzer queryPhrase := bleve.NewMatchPhraseQuery(req.Query) queryPhrase.SetBoost(5.0) + queryPhrase.SetField(resource.SEARCH_FIELD_TITLE) queryPhrase.Analyzer = standard.Name // Query 3: Match query with standard analyzer queryAnalyzed := bleve.NewMatchQuery(removeSmallTerms(req.Query)) + queryAnalyzed.SetField(resource.SEARCH_FIELD_TITLE) queryAnalyzed.Analyzer = standard.Name queryAnalyzed.Operator = query.MatchQueryOperatorAnd // Make sure all terms from the query are matched diff --git a/pkg/storage/unified/testing/search_and_storage.go b/pkg/storage/unified/testing/search_and_storage.go index 5cb4d13488c..fa2e438a377 100644 --- a/pkg/storage/unified/testing/search_and_storage.go +++ b/pkg/storage/unified/testing/search_and_storage.go @@ -226,6 +226,29 @@ func RunTestSearchAndStorage(t *testing.T, ctx context.Context, backend resource require.NoError(t, err) require.NotNil(t, searchResp) require.Nil(t, searchResp.Error) + // finding a document by its tag using the query field is not supported anymore, so should return nothing here + // https://github.com/grafana/grafana/pull/111842 + require.Equal(t, int64(0), searchResp.TotalHits) + + // this is the correct way of searching by tag + searchResp, err = server.Search(ctx, &resourcepb.ResourceSearchRequest{ + Options: &resourcepb.ListOptions{ + Key: &resourcepb.ResourceKey{ + Group: "test.grafana.app", + Resource: "testresources", + Namespace: nsPrefix, + }, + Fields: []*resourcepb.Requirement{{ + Key: "tags", + Operator: "=", + Values: []string{"hello"}, + }}, + }, + Limit: 10, + }) + require.NoError(t, err) + require.NotNil(t, searchResp) + require.Nil(t, searchResp.Error) require.Equal(t, int64(3), searchResp.TotalHits) }) @@ -244,6 +267,29 @@ func RunTestSearchAndStorage(t *testing.T, ctx context.Context, backend resource require.NoError(t, err) require.NotNil(t, searchResp) require.Nil(t, searchResp.Error) + // finding a document by its tag using the query field is not supported anymore, so should return nothing here + // https://github.com/grafana/grafana/pull/111842 + require.Equal(t, int64(0), searchResp.TotalHits) + + // this is the correct way of searching by tag + searchResp, err = server.Search(ctx, &resourcepb.ResourceSearchRequest{ + Options: &resourcepb.ListOptions{ + Key: &resourcepb.ResourceKey{ + Group: "test.grafana.app", + Resource: "testresources", + Namespace: nsPrefix, + }, + Fields: []*resourcepb.Requirement{{ + Key: "tags", + Operator: "=", + Values: []string{"tag1"}, + }}, + }, + Limit: 10, + }) + require.NoError(t, err) + require.NotNil(t, searchResp) + require.Nil(t, searchResp.Error) require.Equal(t, int64(1), searchResp.TotalHits) }) } diff --git a/pkg/storage/unified/testing/search_backend.go b/pkg/storage/unified/testing/search_backend.go index 301a3a202e4..ed1cffd5194 100644 --- a/pkg/storage/unified/testing/search_backend.go +++ b/pkg/storage/unified/testing/search_backend.go @@ -164,14 +164,18 @@ func runTestResourceIndex(t *testing.T, backend resource.SearchBackend, nsPrefix Group: ns.Group, Resource: ns.Resource, }, + Fields: []*resourcepb.Requirement{{ + Key: "tags", + Operator: "=", + Values: []string{"tag3"}, + }}, }, Fields: []string{"title", "folder", "tags"}, - Query: "tag3", Limit: 10, }, nil) require.NoError(t, err) require.NotNil(t, resp) - require.Equal(t, int64(1), resp.TotalHits) // Only doc3 should have tag3 now + require.Equal(t, int64(1), resp.TotalHits) // Only doc2 should have tag3 now // Search for Document resp, err = index.Search(ctx, nil, &resourcepb.ResourceSearchRequest{