fix(search): exclude the scope folder of a search by its id

A search scoped to a folder (scope:<id>) restricts the engine query to the path of that folder. The path filter matches the folder itself as well as everything below it, so the folder is a match of its own search whenever it fits the query.

Until now the service dropped it after the engine had answered, by comparing names: a match was removed when "/" + its name equalled the path of the scope. That had three effects:

- The total and the aggregation buckets still counted the folder, so they were one higher than the list of matches.
- It only worked for a scope directly below the space root. With a scope of /a/b the path is never "/" + "b", so the folder b stayed in the results of a search inside b.
- With a scope of /a every match named "a" was removed, also a file or folder of that name further down, which is a legitimate match.

The id of the scope folder is known from the Stat that resolves the scope. It now goes into the engine query as NOT id:"<id>", the same way the default query template already excludes the personal space root, and the name comparison is gone. Matches, total and buckets agree, on every depth, for the WebDAV report and for the graph search.

Example, searching "urlaub" inside /Urlaub, which holds urlaub-2024.jpg and a sub folder Urlaub: before, 1 match (the jpg) and a total of 3. Now, 2 matches (the sub folder and the jpg) and a total of 2.
This commit is contained in:
Dominik Schmidt committed 2026-11-27 15:52:16 +01:00
1 parent c40173a5c4
commit d1eda59b34
2 files changed
+39 -11

No files matched your search

+11 -7
View File
@@ -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)
}
+28 -4
View File
@@ -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"`
}))
})
})