unified-storage: restrict search to title of documents (#111842)
* restrict search to title of documents
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user