diff --git a/services/search/pkg/bleve/backend.go b/services/search/pkg/bleve/backend.go index 73aa18fa59..c270078146 100644 --- a/services/search/pkg/bleve/backend.go +++ b/services/search/pkg/bleve/backend.go @@ -22,7 +22,6 @@ import ( "github.com/opencloud-eu/opencloud/services/search/pkg/search" searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" searchQuery "github.com/opencloud-eu/opencloud/services/search/pkg/query" ) @@ -65,8 +64,7 @@ func (i *serialIndex) Batch(b *bleve.Batch) error { } // Search executes a search request operation within the index. -// Returns a SearchIndexResponse object or an error. -func (b *Backend) Search(_ context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) { +func (b *Backend) Search(_ context.Context, sir *search.IndexQuery) (*search.IndexResult, error) { createdQuery, err := b.queryCreator.Create(sir.Query) if err != nil { if kql.IsValidationError(err) { @@ -172,7 +170,7 @@ func (b *Backend) Search(_ context.Context, sir *searchService.SearchIndexReques matches = append(matches, match) } - return &searchService.SearchIndexResponse{ + return &search.IndexResult{ Matches: matches, TotalMatches: int32(totalMatches), }, nil diff --git a/services/search/pkg/bleve/backend_test.go b/services/search/pkg/bleve/backend_test.go index 8cff372e2c..3aa6fecb35 100644 --- a/services/search/pkg/bleve/backend_test.go +++ b/services/search/pkg/bleve/backend_test.go @@ -11,7 +11,6 @@ import ( . "github.com/onsi/gomega" "github.com/opencloud-eu/opencloud/pkg/log" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/pkg/bleve" "github.com/opencloud-eu/opencloud/services/search/pkg/content" bleveQuery "github.com/opencloud-eu/opencloud/services/search/pkg/query/bleve" @@ -141,10 +140,10 @@ var _ = Describe("Bleve", func() { Describe("Update", func() { tagHits := func(tag string) []string { - res, err := eng.Search(context.Background(), &searchService.SearchIndexRequest{Query: `tag:("` + tag + `")`}) + res, err := eng.Search(context.Background(), &search.IndexQuery{Query: `tag:("` + tag + `")`}) Expect(err).ToNot(HaveOccurred()) var ids []string - for _, match := range res.GetMatches() { + for _, match := range res.Matches { ids = append(ids, match.GetEntity().GetId().GetOpaqueId()) } return ids diff --git a/services/search/pkg/opensearch/backend.go b/services/search/pkg/opensearch/backend.go index 1364eba59a..71a15b47b6 100644 --- a/services/search/pkg/opensearch/backend.go +++ b/services/search/pkg/opensearch/backend.go @@ -21,7 +21,6 @@ import ( "github.com/opencloud-eu/opencloud/pkg/kql" "github.com/opencloud-eu/opencloud/pkg/log" searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/pkg/mapping" "github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/convert" "github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/osu" @@ -81,7 +80,7 @@ func NewBackend(ctx context.Context, name string, client *opensearchgoAPI.Client return &Backend{index: index, client: client}, nil } -func (b *Backend) Search(ctx context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) { +func (b *Backend) Search(ctx context.Context, sir *search.IndexQuery) (*search.IndexResult, error) { boolQuery, err := convert.KQLToOpenSearchBoolQuery(sir.Query) switch { case kql.IsValidationError(err): @@ -173,7 +172,7 @@ func (b *Backend) Search(ctx context.Context, sir *searchService.SearchIndexRequ matches = append(matches, match) } - return &searchService.SearchIndexResponse{ + return &search.IndexResult{ Matches: matches, TotalMatches: int32(totalMatches), }, nil diff --git a/services/search/pkg/opensearch/backend_test.go b/services/search/pkg/opensearch/backend_test.go index abd74fd1e5..56fa1f44e5 100644 --- a/services/search/pkg/opensearch/backend_test.go +++ b/services/search/pkg/opensearch/backend_test.go @@ -11,7 +11,6 @@ import ( opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi" "github.com/opencloud-eu/opencloud/pkg/log" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/internal/opensearchtest" "github.com/opencloud-eu/opencloud/services/search/pkg/opensearch" "github.com/opencloud-eu/opencloud/services/search/pkg/search" @@ -147,9 +146,9 @@ var _ = Describe("Backend", func() { }) tagHits := func(tag string) int { - res, err := backend.Search(context.Background(), &searchService.SearchIndexRequest{Query: `tag:("` + tag + `")`}) + res, err := backend.Search(context.Background(), &search.IndexQuery{Query: `tag:("` + tag + `")`}) Expect(err).ToNot(HaveOccurred()) - return len(res.GetMatches()) + return len(res.Matches) } It("rewrites tags and favorites and keeps the extracted fields", func() { diff --git a/services/search/pkg/parity/engines_test.go b/services/search/pkg/parity/engines_test.go index bc1e044c72..70126605e6 100644 --- a/services/search/pkg/parity/engines_test.go +++ b/services/search/pkg/parity/engines_test.go @@ -15,7 +15,6 @@ import ( "github.com/opencloud-eu/opencloud/pkg/log" searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/internal/opensearchtest" bleveEngine "github.com/opencloud-eu/opencloud/services/search/pkg/bleve" "github.com/opencloud-eu/opencloud/services/search/pkg/opensearch" @@ -146,12 +145,12 @@ func badRequestAnswer(err error) []string { return []string{"bad request"} } -func reads(read func(*searchMessage.Match) string) func(*searchService.SearchIndexResponse) []string { +func reads(read func(*searchMessage.Match) string) func(*search.IndexResult) []string { return readsMany(func(m *searchMessage.Match) []string { return []string{read(m)} }) } -func readsMany(read func(*searchMessage.Match) []string) func(*searchService.SearchIndexResponse) []string { - return func(resp *searchService.SearchIndexResponse) []string { +func readsMany(read func(*searchMessage.Match) []string) func(*search.IndexResult) []string { + return func(resp *search.IndexResult) []string { if len(resp.Matches) != 1 { return []string{fmt.Sprintf("%d matches", len(resp.Matches))} } @@ -170,7 +169,7 @@ func resourceID(id *searchMessage.ResourceID) string { // ask runs a query and returns the matched names; an engine error is the // answer "error", so a query the engine rejects still shows in the matrix. -func ask(e search.Engine, request *searchService.SearchIndexRequest) ([]string, error) { +func ask(e search.Engine, request *search.IndexQuery) ([]string, error) { resp, err := e.Search(context.Background(), request) if err != nil { return []string{"error"}, err diff --git a/services/search/pkg/parity/parity_test.go b/services/search/pkg/parity/parity_test.go index 3c82ce043e..c4e2c84da4 100644 --- a/services/search/pkg/parity/parity_test.go +++ b/services/search/pkg/parity/parity_test.go @@ -12,7 +12,6 @@ import ( "github.com/opencloud-eu/reva/v3/pkg/errtypes" searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/pkg/search" ) @@ -124,7 +123,7 @@ type responseCase struct { after string query string reads string - read func(*searchService.SearchIndexResponse) []string + read func(*search.IndexResult) []string want []string engineOverrides map[string]override @@ -288,7 +287,7 @@ var _ = Describe("Queries", func() { Skip(e.unavailable) } - request := &searchService.SearchIndexRequest{Query: c.query, PageSize: c.limit, Ref: c.ref} + request := &search.IndexQuery{Query: c.query, PageSize: c.limit, Ref: c.ref} expected := override{want: c.want, wantCount: c.wantCount, wantBadRequest: c.wantBadRequest} _, overridden := c.engineOverrides[name] @@ -369,7 +368,7 @@ var _ = Describe("Operations", func() { answers := make([][]string, len(c.expect)) errs := make([]error, len(c.expect)) for i, expect := range c.expect { - answers[i], errs[i] = ask(e.backend, &searchService.SearchIndexRequest{Query: expect.query}) + answers[i], errs[i] = ask(e.backend, &search.IndexQuery{Query: expect.query}) recordAnswer(rows[i], name, answers[i]) } @@ -446,7 +445,7 @@ var _ = Describe("Response", func() { e.settle() } - resp, err := e.backend.Search(context.Background(), &searchService.SearchIndexRequest{Query: c.query}) + resp, err := e.backend.Search(context.Background(), &search.IndexQuery{Query: c.query}) if err != nil { recordAnswer(row, name, []string{"error"}) } diff --git a/services/search/pkg/parity/response_entity_test.go b/services/search/pkg/parity/response_entity_test.go index ad761de7ec..6fd478d6e8 100644 --- a/services/search/pkg/parity/response_entity_test.go +++ b/services/search/pkg/parity/response_entity_test.go @@ -4,7 +4,6 @@ import ( "fmt" searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/pkg/search" ) @@ -75,8 +74,8 @@ func entityGroup() responseGroup { }, { id: 11, query: `path:"./parent"`, reads: "TotalMatches", want: []string{"2"}, - read: func(resp *searchService.SearchIndexResponse) []string { - return []string{fmt.Sprint(resp.GetTotalMatches())} + read: func(resp *search.IndexResult) []string { + return []string{fmt.Sprint(resp.TotalMatches)} }, }, { diff --git a/services/search/pkg/search/mocks/engine.go b/services/search/pkg/search/mocks/engine.go index 110c8d07e6..1942be032e 100644 --- a/services/search/pkg/search/mocks/engine.go +++ b/services/search/pkg/search/mocks/engine.go @@ -7,7 +7,6 @@ package mocks import ( "context" - "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/pkg/search" mock "github.com/stretchr/testify/mock" ) @@ -547,26 +546,26 @@ func (_c *Engine_Restore_Call) RunAndReturn(run func(id string) error) *Engine_R } // Search provides a mock function for the type Engine -func (_mock *Engine) Search(ctx context.Context, req *v0.SearchIndexRequest) (*v0.SearchIndexResponse, error) { +func (_mock *Engine) Search(ctx context.Context, req *search.IndexQuery) (*search.IndexResult, error) { ret := _mock.Called(ctx, req) if len(ret) == 0 { panic("no return value specified for Search") } - var r0 *v0.SearchIndexResponse + var r0 *search.IndexResult var r1 error - if returnFunc, ok := ret.Get(0).(func(context.Context, *v0.SearchIndexRequest) (*v0.SearchIndexResponse, error)); ok { + if returnFunc, ok := ret.Get(0).(func(context.Context, *search.IndexQuery) (*search.IndexResult, error)); ok { return returnFunc(ctx, req) } - if returnFunc, ok := ret.Get(0).(func(context.Context, *v0.SearchIndexRequest) *v0.SearchIndexResponse); ok { + if returnFunc, ok := ret.Get(0).(func(context.Context, *search.IndexQuery) *search.IndexResult); ok { r0 = returnFunc(ctx, req) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*v0.SearchIndexResponse) + r0 = ret.Get(0).(*search.IndexResult) } } - if returnFunc, ok := ret.Get(1).(func(context.Context, *v0.SearchIndexRequest) error); ok { + if returnFunc, ok := ret.Get(1).(func(context.Context, *search.IndexQuery) error); ok { r1 = returnFunc(ctx, req) } else { r1 = ret.Error(1) @@ -581,20 +580,20 @@ type Engine_Search_Call struct { // Search is a helper method to define mock.On call // - ctx context.Context -// - req *v0.SearchIndexRequest +// - req *search.IndexQuery func (_e *Engine_Expecter) Search(ctx any, req any) *Engine_Search_Call { return &Engine_Search_Call{Call: _e.mock.On("Search", ctx, req)} } -func (_c *Engine_Search_Call) Run(run func(ctx context.Context, req *v0.SearchIndexRequest)) *Engine_Search_Call { +func (_c *Engine_Search_Call) Run(run func(ctx context.Context, req *search.IndexQuery)) *Engine_Search_Call { _c.Call.Run(func(args mock.Arguments) { var arg0 context.Context if args[0] != nil { arg0 = args[0].(context.Context) } - var arg1 *v0.SearchIndexRequest + var arg1 *search.IndexQuery if args[1] != nil { - arg1 = args[1].(*v0.SearchIndexRequest) + arg1 = args[1].(*search.IndexQuery) } run( arg0, @@ -604,12 +603,12 @@ func (_c *Engine_Search_Call) Run(run func(ctx context.Context, req *v0.SearchIn return _c } -func (_c *Engine_Search_Call) Return(searchIndexResponse *v0.SearchIndexResponse, err error) *Engine_Search_Call { - _c.Call.Return(searchIndexResponse, err) +func (_c *Engine_Search_Call) Return(indexResult *search.IndexResult, err error) *Engine_Search_Call { + _c.Call.Return(indexResult, err) return _c } -func (_c *Engine_Search_Call) RunAndReturn(run func(ctx context.Context, req *v0.SearchIndexRequest) (*v0.SearchIndexResponse, error)) *Engine_Search_Call { +func (_c *Engine_Search_Call) RunAndReturn(run func(ctx context.Context, req *search.IndexQuery) (*search.IndexResult, error)) *Engine_Search_Call { _c.Call.Return(run) return _c } diff --git a/services/search/pkg/search/search.go b/services/search/pkg/search/search.go index a897aabf25..24996378f8 100644 --- a/services/search/pkg/search/search.go +++ b/services/search/pkg/search/search.go @@ -22,7 +22,6 @@ import ( "github.com/opencloud-eu/opencloud/pkg/log" searchmsg "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" - searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" "github.com/opencloud-eu/opencloud/services/search/pkg/content" "github.com/opencloud-eu/opencloud/services/search/pkg/mapping" ) @@ -46,9 +45,23 @@ var ErrOptimizeUnsupported = errors.New("the search engine merges its index on i // ErrOptimizeInProgress is returned while another optimization of the index runs var ErrOptimizeInProgress = errors.New("the index is being optimized already") +// IndexQuery is one search against the index. +type IndexQuery struct { + Query string + Ref *searchmsg.Reference + PageSize int32 +} + +// IndexResult is what the engine answers: the matches of one space and how +// many there are in total. +type IndexResult struct { + Matches []*searchmsg.Match + TotalMatches int32 +} + // Engine is the interface to the search engine type Engine interface { - Search(ctx context.Context, req *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) + Search(ctx context.Context, req *IndexQuery) (*IndexResult, error) DocCount() (uint64, error) // Optimize merges the segments of the index, ErrOptimizeUnsupported if the // engine merges on its own diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index 1b5de5ea46..551e11ed14 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -287,7 +287,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se errg, ctx := errgroup.WithContext(ctx) work := make(chan *provider.StorageSpace, len(spaces)) - results := make(chan *searchsvc.SearchIndexResponse, len(spaces)) + results := make(chan *IndexResult, len(spaces)) // Distribute work errg.Go(func() error { @@ -331,7 +331,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se close(results) }() - responses := make([]*searchsvc.SearchIndexResponse, len(spaces)) + responses := make([]*IndexResult, len(spaces)) i := 0 for r := range results { responses[i] = r @@ -369,7 +369,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se }, nil } -func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, space *provider.StorageSpace, mountpointID string) (res *searchsvc.SearchIndexResponse, err error) { +func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, space *provider.StorageSpace, mountpointID string) (res *IndexResult, err error) { // the workers run in an errgroup, which does not recover, one failing space must not affect the others defer func() { if r := recover(); r != nil { @@ -473,7 +473,7 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, permissions = space.GetRootInfo().GetPermissionSet() } - searchRequest := &searchsvc.SearchIndexRequest{ + searchRequest := &IndexQuery{ Query: ExpandQueryTemplate(s.queryTemplate, req.Query, space), Ref: &searchmsg.Reference{ ResourceId: searchRootID, diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 9b5c3a66eb..064e649d86 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -785,7 +785,7 @@ var _ = Describe("Searchprovider", func() { Status: status.NewOK(ctx), StorageSpaces: []*sprovider.StorageSpace{personalSpace}, }, nil) - indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{ + indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{ TotalMatches: 1, Matches: []*searchmsg.Match{ { @@ -829,7 +829,7 @@ var _ = Describe("Searchprovider", func() { Query: "Size:<10", }) Expect(err).ToNot(HaveOccurred()) - indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool { + indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool { return req.Query == "Size:<10" })) }) @@ -840,7 +840,7 @@ var _ = Describe("Searchprovider", func() { }) _, err := s.Search(ctx, &searchsvc.SearchRequest{Query: "foo"}) Expect(err).ToNot(HaveOccurred()) - indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool { + indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool { return req.Query == `(foo) AND (NOT path:"./.space" AND NOT id:"storageid$personalspace!personalspace")` })) }) @@ -894,7 +894,7 @@ var _ = Describe("Searchprovider", func() { Status: status.NewOK(ctx), Path: "/path", }, nil) - indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{ + indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{ TotalMatches: 1, Matches: []*searchmsg.Match{ { @@ -993,7 +993,7 @@ var _ = Describe("Searchprovider", func() { Status: status.NewOK(ctx), StorageSpaces: []*sprovider.StorageSpace{grantSpace, mountpointSpace}, }, nil) - indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{ + indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{ TotalMatches: 1, Matches: []*searchmsg.Match{ { @@ -1040,10 +1040,10 @@ var _ = Describe("Searchprovider", func() { Status: status.NewOK(ctx), StorageSpaces: []*sprovider.StorageSpace{personalSpace, grantSpace, mountpointSpace}, }, nil) - indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool { + indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool { return req.Ref.ResourceId.OpaqueId == grantSpace.Root.SpaceId && req.Ref.ResourceId.SpaceId == grantSpace.Root.SpaceId - })).Return(&searchsvc.SearchIndexResponse{ + })).Return(&search.IndexResult{ TotalMatches: 2, Matches: []*searchmsg.Match{ { @@ -1084,10 +1084,10 @@ var _ = Describe("Searchprovider", func() { }, }, }, nil) - indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool { + indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool { return req.Ref.ResourceId.OpaqueId == personalSpace.Root.OpaqueId && req.Ref.ResourceId.SpaceId == personalSpace.Root.SpaceId - })).Return(&searchsvc.SearchIndexResponse{ + })).Return(&search.IndexResult{ TotalMatches: 1, Matches: []*searchmsg.Match{ {