diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index a9e057aade..8d93412e1b 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -201,6 +201,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se } req.Query = query + var scopeRoot *provider.ResourceId if len(scope) > 0 { scopedID, err := storagespace.ParseID(scope) if err != nil { @@ -225,6 +226,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se return nil, err } + scopeRoot = statRes.GetInfo().GetId() req.Ref = &searchmsg.Reference{ ResourceId: &searchmsg.ResourceID{ StorageId: statRes.GetInfo().GetSpace().GetRoot().GetStorageId(), @@ -321,7 +323,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se for i := 0; i < numWorkers; i++ { errg.Go(func() error { for space := range work { - res, err := s.searchIndex(ctx, req, space, mountpointMap[space.Id.OpaqueId]) + res, err := s.searchIndex(ctx, req, space, mountpointMap[space.Id.OpaqueId], scopeRoot) if err != nil && err != errSkipSpace { return err } @@ -425,7 +427,7 @@ func engineFetchSize(from int32, pageSize *int32) int32 { return math.MaxInt32 } -func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, space *provider.StorageSpace, mountpointID string) (res *IndexResult, err error) { +func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, space *provider.StorageSpace, mountpointID string, scopeRoot *provider.ResourceId) (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 { @@ -529,8 +531,14 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, permissions = space.GetRootInfo().GetPermissionSet() } + query := ExpandQueryTemplate(s.queryTemplate, req.Query, space) + if scopeRoot != nil { + // the path filter of the engines matches the scope folder as well + query = fmt.Sprintf(`(%s) AND NOT id:"%s"`, query, storagespace.FormatResourceID(scopeRoot)) + } + searchRequest := &IndexQuery{ - Query: ExpandQueryTemplate(s.queryTemplate, req.Query, space), + Query: query, Aggregations: req.GetAggregations(), AggregationFilters: req.GetAggregationFilters(), Ref: &searchmsg.Reference{ @@ -574,10 +582,6 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, // oc:permissions string projects, in libregraph action notation. match.Entity.PermissionsActionsAllowedValues = unifiedrole.CS3ResourcePermissionsToLibregraphActions(permissions) - if req.Ref != nil && searchPathPrefix == "/"+match.Entity.Name { - continue - } - matches = append(matches, match) } diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 368caffd70..843c803cae 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -1033,6 +1033,7 @@ var _ = Describe("Searchprovider", func() { gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ Status: status.NewOK(context.Background()), Info: &sprovider.ResourceInfo{ + Id: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "personalspace", OpaqueId: "scopefolder"}, Space: &sprovider.StorageSpace{Root: &sprovider.ResourceId{ StorageId: "storageid", SpaceId: "personalspace", @@ -1045,8 +1046,26 @@ var _ = Describe("Searchprovider", func() { Path: "/path", }, nil) indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{ - TotalMatches: 1, + TotalMatches: 2, Matches: []*searchmsg.Match{ + { + Score: 2, + Entity: &searchmsg.Entity{ + Ref: &searchmsg.Reference{ + ResourceId: &searchmsg.ResourceID{ + StorageId: personalSpace.Root.StorageId, + SpaceId: personalSpace.Root.SpaceId, + OpaqueId: personalSpace.Root.OpaqueId, + }, + Path: "./path/to/path", + }, + Id: &searchmsg.ResourceID{ + StorageId: personalSpace.Root.StorageId, + OpaqueId: "namesake-id", + }, + Name: "path", + }, + }, { Score: 1, Entity: &searchmsg.Entity{ @@ -1083,13 +1102,18 @@ var _ = Describe("Searchprovider", func() { Expect(err).ToNot(HaveOccurred()) Expect(res).ToNot(BeNil()) - Expect(res.TotalMatches).To(Equal(int32(1))) - Expect(len(res.Matches)).To(Equal(1)) - match := res.Matches[0] + Expect(res.TotalMatches).To(Equal(int32(2))) + Expect(len(res.Matches)).To(Equal(2)) + Expect(res.Matches[0].Entity.Name).To(Equal("path"), "a match named like the scope folder stays") + match := res.Matches[1] Expect(match.Entity.Id.OpaqueId).To(Equal("foo-id")) Expect(match.Entity.Name).To(Equal("Foo.pdf")) Expect(match.Entity.Ref.ResourceId.OpaqueId).To(Equal(personalSpace.Root.OpaqueId)) Expect(match.Entity.Ref.Path).To(Equal("./path/to/Foo.pdf")) + + indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool { + return req.Query == `(foo) AND NOT id:"storageid$personalspace!scopefolder"` + })) }) })