From 99b8cdc7d6781aa5320f78b3f6526a440ee78698 Mon Sep 17 00:00:00 2001 From: Roman Perekhod Date: Wed, 12 Jul 2023 12:00:04 +0200 Subject: [PATCH] fix the total count --- services/search/pkg/engine/bleve.go | 4 +- services/search/pkg/search/service.go | 24 +++--- services/search/pkg/search/service_test.go | 93 ++++++++++++++++++++-- 3 files changed, 102 insertions(+), 19 deletions(-) diff --git a/services/search/pkg/engine/bleve.go b/services/search/pkg/engine/bleve.go index 3831b42e7e..9fc31dd69d 100644 --- a/services/search/pkg/engine/bleve.go +++ b/services/search/pkg/engine/bleve.go @@ -162,8 +162,10 @@ func (b *Bleve) Search(_ context.Context, sir *searchService.SearchIndexRequest) } matches := make([]*searchMessage.Match, 0, len(res.Hits)) + totalMatches := res.Total for _, hit := range res.Hits { if sir.Ref != nil && !strings.HasPrefix(getFieldValue[string](hit.Fields, "Path"), utils.MakeRelativePath(path.Join(sir.Ref.Path, "/"))) { + totalMatches-- continue } @@ -206,7 +208,7 @@ func (b *Bleve) Search(_ context.Context, sir *searchService.SearchIndexRequest) return &searchService.SearchIndexResponse{ Matches: matches, - TotalMatches: int32(res.Total), + TotalMatches: int32(totalMatches), }, nil } diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index bc5648bc5b..1b4fe8fcde 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -32,8 +32,12 @@ import ( //go:generate mockery --name=Searcher const ( - _spaceStateTrashed = "trashed" - _slowQueryDuration = 500 * time.Millisecond + _spaceStateTrashed = "trashed" + _spaceTypeMountpoint = "mountpoint" + _spaceTypePersonal = "personal" + _spaceTypeProject = "project" + _spaceTypeGrant = "grant" + _slowQueryDuration = 500 * time.Millisecond ) // Searcher is the interface to the SearchService @@ -105,7 +109,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se } // GetPath the scope to get the full path in the space gpRes, err := gatewayClient.GetPath(ctx, &provider.GetPathRequest{ - ResourceId: statRes.Info.Id, + ResourceId: statRes.GetInfo().GetId(), }) if err != nil { return nil, err @@ -113,9 +117,9 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se req.Ref = &searchmsg.Reference{ ResourceId: &searchmsg.ResourceID{ - StorageId: statRes.Info.Space.Root.StorageId, - SpaceId: statRes.Info.Space.Root.SpaceId, - OpaqueId: statRes.Info.Space.Root.OpaqueId, + StorageId: statRes.GetInfo().GetSpace().GetRoot().GetStorageId(), + SpaceId: statRes.GetInfo().GetSpace().GetRoot().GetSpaceId(), + OpaqueId: statRes.GetInfo().GetSpace().GetRoot().GetOpaqueId(), }, Path: gpRes.Path, } @@ -153,7 +157,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se mountpointMap := map[string]string{} for _, space := range spaces { - if space.SpaceType != "mountpoint" { + if space.SpaceType != _spaceTypeMountpoint { continue } opaqueMap := sdk.DecodeOpaqueMap(space.Opaque) @@ -272,9 +276,9 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, mountpointPrefix := "" searchPathPrefix := req.Ref.GetPath() switch space.SpaceType { - case "mountpoint": + case _spaceTypeMountpoint: return nil, errSkipSpace // mountpoint spaces are only "links" to the shared spaces. we have to search the shared "grant" space instead - case "grant": + case _spaceTypeGrant: // In case of grant spaces we search the root of the outer space and translate the paths to the according mountpoint searchRootID.OpaqueId = space.Root.SpaceId if mountpointID == "" { @@ -329,7 +333,7 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, rootName = space.GetRootInfo().GetPath() permissions = space.GetRootInfo().GetPermissionSet() s.logger.Debug().Interface("grantSpace", space).Interface("mountpointRootId", mountpointRootID).Msg("searching a grant") - case "personal", "project": + case _spaceTypePersonal, _spaceTypeProject: permissions = space.GetRootInfo().GetPermissionSet() } diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 3c340003f7..d54b8d5adb 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -53,9 +53,10 @@ var _ = Describe("Searchprovider", func() { }, }, }, - Id: &sprovider.StorageSpaceId{OpaqueId: "storageid$personalspace!personalspace"}, - Root: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "personalspace", OpaqueId: "personalspace"}, - Name: "personalspace", + Id: &sprovider.StorageSpaceId{OpaqueId: "storageid$personalspace!personalspace"}, + Root: &sprovider.ResourceId{StorageId: "storageid", SpaceId: "personalspace", OpaqueId: "personalspace"}, + Name: "personalspace", + SpaceType: "personal", } ri = &sprovider.ResourceInfo{ @@ -94,10 +95,6 @@ var _ = Describe("Searchprovider", func() { Status: status.NewOK(ctx), Token: "authtoken", }, nil) - gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ - Status: status.NewOK(context.Background()), - Info: ri, - }, nil) gatewayClient.On("GetPath", mock.Anything, mock.MatchedBy(func(req *sprovider.GetPathRequest) bool { return req.ResourceId.OpaqueId == ri.Id.OpaqueId })).Return(&sprovider.GetPathResponse{ @@ -123,7 +120,10 @@ var _ = Describe("Searchprovider", func() { extractor.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{}, nil) indexClient.On("Upsert", mock.Anything, mock.Anything).Return(nil) indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{}, nil) - + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: ri, + }, nil) err := s.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"}, user.Id) Expect(err).ShouldNot(HaveOccurred()) }) @@ -167,6 +167,10 @@ var _ = Describe("Searchprovider", func() { }, }, }, nil) + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: ri, + }, nil) }) It("does not mess with field-based searches", func() { @@ -195,6 +199,75 @@ var _ = Describe("Searchprovider", func() { }) }) + Context("with a personal space with a filter", func() { + BeforeEach(func() { + gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything).Return(&sprovider.ListStorageSpacesResponse{ + Status: status.NewOK(ctx), + StorageSpaces: []*sprovider.StorageSpace{personalSpace}, + }, nil) + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: &sprovider.ResourceInfo{ + Space: &sprovider.StorageSpace{Root: &sprovider.ResourceId{ + StorageId: "storageid", + SpaceId: "personalspace", + OpaqueId: "personalspace", + }}, + }, + }, nil) + gatewayClient.On("GetPath", mock.Anything, mock.Anything).Return(&sprovider.GetPathResponse{ + Status: status.NewOK(ctx), + Path: "/path", + }, nil) + indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{ + TotalMatches: 1, + Matches: []*searchmsg.Match{ + { + Score: 1, + Entity: &searchmsg.Entity{ + Ref: &searchmsg.Reference{ + ResourceId: &searchmsg.ResourceID{ + StorageId: personalSpace.Root.StorageId, + SpaceId: personalSpace.Root.SpaceId, + OpaqueId: personalSpace.Root.OpaqueId, + }, + Path: "./path/to/Foo.pdf", + }, + Id: &searchmsg.ResourceID{ + StorageId: personalSpace.Root.StorageId, + OpaqueId: "foo-id", + }, + Name: "Foo.pdf", + }, + }, + }, + }, nil) + }) + + It("searches the personal user space", func() { + res, err := s.Search(ctx, &searchsvc.SearchRequest{ + Query: "foo scope:storageid$personalspace!personalspace/path", + Ref: &searchmsg.Reference{ + ResourceId: &searchmsg.ResourceID{ + StorageId: "storageid", + SpaceId: "personalspace", + OpaqueId: "personalspace", + }, + }, + }) + + 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(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")) + }) + }) + Context("with received shares", func() { var ( grantSpace *sprovider.StorageSpace @@ -223,6 +296,10 @@ var _ = Describe("Searchprovider", func() { }, }, } + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: ri, + }, nil) gatewayClient.On("GetPath", mock.Anything, mock.Anything).Return(&sprovider.GetPathResponse{ Status: status.NewOK(ctx), Path: "/grant/path",