diff --git a/services/search/pkg/engine/bleve.go b/services/search/pkg/engine/bleve.go index 4f25dfa02c..af4bf1b7ec 100644 --- a/services/search/pkg/engine/bleve.go +++ b/services/search/pkg/engine/bleve.go @@ -171,9 +171,11 @@ func (b *Bleve) Search(_ context.Context, sir *searchService.SearchIndexRequest) totalMatches := res.Total for _, hit := range res.Hits { if sir.Ref != nil { - path := strings.TrimSuffix(getFieldValue[string](hit.Fields, "Path"), "/") - relRefPath := utils.MakeRelativePath(sir.Ref.Path) - if relRefPath != "." && !strings.HasPrefix(path, relRefPath+"/") { + hitPath := strings.TrimSuffix(getFieldValue[string](hit.Fields, "Path"), "/") + requestedPath := utils.MakeRelativePath(sir.Ref.Path) + isRoot := hitPath == requestedPath + + if !isRoot && requestedPath != "." && !strings.HasPrefix(hitPath, requestedPath+"/") { totalMatches-- continue } diff --git a/services/search/pkg/engine/bleve_test.go b/services/search/pkg/engine/bleve_test.go index 85a623d3a6..c1ec06f0e9 100644 --- a/services/search/pkg/engine/bleve_test.go +++ b/services/search/pkg/engine/bleve_test.go @@ -341,7 +341,7 @@ var _ = Describe("Bleve", func() { It("search *doc* in a subfolder", func() { res, err := doSearch(rootResource.ID, "Name:*doc*", "./doc") Expect(err).ToNot(HaveOccurred()) - Expect(res.TotalMatches).To(Equal(int32(1))) + Expect(res.TotalMatches).To(Equal(int32(2))) }) It("search *file* in a root", func() { res, err := doSearch(rootResource.ID, "Name:*file*", "") diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index 1ce6a9c32a..3bddd8b5ff 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -19,14 +19,15 @@ import ( "github.com/cs3org/reva/v2/pkg/storage/utils/walker" "github.com/cs3org/reva/v2/pkg/storagespace" "github.com/cs3org/reva/v2/pkg/utils" + "golang.org/x/sync/errgroup" + "google.golang.org/protobuf/types/known/fieldmaskpb" + "github.com/owncloud/ocis/v2/ocis-pkg/log" searchmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/search/v0" searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0" "github.com/owncloud/ocis/v2/services/search/pkg/config" "github.com/owncloud/ocis/v2/services/search/pkg/content" "github.com/owncloud/ocis/v2/services/search/pkg/engine" - "golang.org/x/sync/errgroup" - "google.golang.org/protobuf/types/known/fieldmaskpb" ) //go:generate mockery --name=Searcher @@ -327,9 +328,6 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, } rootName = space.GetRootInfo().GetPath() permissions = space.GetRootInfo().GetPermissionSet() - if req.Ref == nil && utils.MakeRelativePath(searchPathPrefix) == utils.MakeRelativePath(rootName) { - searchPathPrefix = "." - } s.logger.Debug().Interface("grantSpace", space).Interface("mountpointRootId", mountpointRootID).Msg("searching a grant") case _spaceTypePersonal, _spaceTypeProject: permissions = space.GetRootInfo().GetPermissionSet() @@ -356,6 +354,8 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, s.logger.Debug().Interface("searchRequest", searchRequest).Str("duration", fmt.Sprint(duration)).Str("space", space.Id.OpaqueId).Int("hits", len(res.Matches)).Msg("space search done") } + var matches []*searchmsg.Match + for _, match := range res.Matches { if mountpointPrefix != "" { match.Entity.Ref.Path = utils.MakeRelativePath(strings.TrimPrefix(match.Entity.Ref.Path, mountpointPrefix)) @@ -369,7 +369,16 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, isMountpoint := isShared && match.GetEntity().GetRef().GetPath() == "." isDir := match.GetEntity().GetMimeType() == "httpd/unix-directory" match.Entity.Permissions = convertToWebDAVPermissions(isShared, isMountpoint, isDir, permissions) + + if req.Ref != nil && searchPathPrefix == "/"+match.Entity.Name { + continue + } + + matches = append(matches, match) } + + res.Matches = matches + return res, nil }