diff --git a/changelog/unreleased/fix-search.md b/changelog/unreleased/fix-search.md new file mode 100644 index 000000000..0ce64c0b5 --- /dev/null +++ b/changelog/unreleased/fix-search.md @@ -0,0 +1,8 @@ +Bugfix: Fix the search + +We fixed the issue when search using the current folder option shows the file/folders outside the folder if search keyword is same as +current folder + + +https://github.com/owncloud/ocis/pull/6947 +https://github.com/owncloud/ocis/issues/6935 diff --git a/services/search/pkg/engine/bleve.go b/services/search/pkg/engine/bleve.go index 9fc31dd69..0d757b260 100644 --- a/services/search/pkg/engine/bleve.go +++ b/services/search/pkg/engine/bleve.go @@ -164,9 +164,13 @@ 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 + if sir.Ref != nil { + path := strings.TrimSuffix(getFieldValue[string](hit.Fields, "Path"), "/") + relRefPath := utils.MakeRelativePath(sir.Ref.Path) + if relRefPath != "." && !strings.HasPrefix(path, relRefPath+"/") { + totalMatches-- + continue + } } rootID, err := storagespace.ParseID(getFieldValue[string](hit.Fields, "RootID")) diff --git a/services/search/pkg/engine/bleve_test.go b/services/search/pkg/engine/bleve_test.go index 219167fff..6c54d5468 100644 --- a/services/search/pkg/engine/bleve_test.go +++ b/services/search/pkg/engine/bleve_test.go @@ -23,7 +23,7 @@ var _ = Describe("Bleve", func() { idx bleve.Index ctx context.Context - doSearch = func(id string, query string) (*searchsvc.SearchIndexResponse, error) { + doSearch = func(id string, query, path string) (*searchsvc.SearchIndexResponse, error) { rID, err := storagespace.ParseID(id) if err != nil { return nil, err @@ -37,12 +37,13 @@ var _ = Describe("Bleve", func() { SpaceId: rID.SpaceId, OpaqueId: rID.OpaqueId, }, + Path: path, }, }) } assertDocCount = func(id string, query string, expectedCount int) []*searchmsg.Match { - res, err := doSearch(id, query) + res, err := doSearch(id, query, "") ExpectWithOffset(1, err).ToNot(HaveOccurred()) ExpectWithOffset(1, len(res.Matches)).To(Equal(expectedCount), "query returned unexpected number of results: "+query) @@ -177,7 +178,7 @@ var _ = Describe("Bleve", func() { err := eng.Upsert(parentResource.ID, parentResource) Expect(err).ToNot(HaveOccurred()) - res, err := doSearch(rootResource.ID, "Name:bar*") + res, err := doSearch(rootResource.ID, "Name:bar*", "") Expect(err).ToNot(HaveOccurred()) Expect(res.TotalMatches).To(Equal(int32(1))) }) @@ -252,7 +253,7 @@ var _ = Describe("Bleve", func() { err := eng.Upsert(parentResource.ID, parentResource) Expect(err).ToNot(HaveOccurred()) - res, err := doSearch(rootResource.ID, "Name:baz*") + res, err := doSearch(rootResource.ID, "Name:baz*", "") Expect(err).ToNot(HaveOccurred()) Expect(res.TotalMatches).To(Equal(int32(1))) Expect(res.Matches[0].Entity.Highlights).To(Equal("")) @@ -264,13 +265,94 @@ var _ = Describe("Bleve", func() { err := eng.Upsert(parentResource.ID, parentResource) Expect(err).ToNot(HaveOccurred()) - res, err := doSearch(rootResource.ID, "Content:bar") + res, err := doSearch(rootResource.ID, "Content:bar", "") Expect(err).ToNot(HaveOccurred()) Expect(res.TotalMatches).To(Equal(int32(1))) Expect(res.Matches[0].Entity.Highlights).To(Equal("foo bar baz")) }) }) + + Context("with a file in the root of the space and folder with a file. all of them have the same name", func() { + BeforeEach(func() { + parentResource := engine.Resource{ + ID: "1$2!3", + ParentID: rootResource.ID, + RootID: rootResource.ID, + Path: "./doc", + Type: uint64(sprovider.ResourceType_RESOURCE_TYPE_CONTAINER), + Document: content.Document{Name: "doc"}, + } + + childResource := engine.Resource{ + ID: "1$2!4", + ParentID: parentResource.ID, + RootID: rootResource.ID, + Path: "./doc/doc.pdf", + Type: uint64(sprovider.ResourceType_RESOURCE_TYPE_FILE), + Document: content.Document{Name: "doc.pdf"}, + } + + childResource2 := engine.Resource{ + ID: "1$2!7", + ParentID: parentResource.ID, + RootID: rootResource.ID, + Path: "./doc/file.pdf", + Type: uint64(sprovider.ResourceType_RESOURCE_TYPE_FILE), + Document: content.Document{Name: "file.pdf"}, + } + + rootChildResource := engine.Resource{ + ID: "1$2!5", + ParentID: rootResource.ID, + RootID: rootResource.ID, + Path: "./doc.pdf", + Type: uint64(sprovider.ResourceType_RESOURCE_TYPE_FILE), + Document: content.Document{Name: "doc.pdf"}, + } + + rootChildResource2 := engine.Resource{ + ID: "1$2!6", + ParentID: rootResource.ID, + RootID: rootResource.ID, + Path: "./file.pdf", + Type: uint64(sprovider.ResourceType_RESOURCE_TYPE_FILE), + Document: content.Document{Name: "file.pdf"}, + } + err := eng.Upsert(parentResource.ID, parentResource) + Expect(err).ToNot(HaveOccurred()) + + err = eng.Upsert(rootChildResource.ID, rootChildResource) + Expect(err).ToNot(HaveOccurred()) + err = eng.Upsert(rootChildResource2.ID, rootChildResource2) + Expect(err).ToNot(HaveOccurred()) + + err = eng.Upsert(childResource.ID, childResource) + Expect(err).ToNot(HaveOccurred()) + err = eng.Upsert(childResource2.ID, childResource2) + Expect(err).ToNot(HaveOccurred()) + }) + It("search *doc* in a root", func() { + res, err := doSearch(rootResource.ID, "Name:*doc*", "") + Expect(err).ToNot(HaveOccurred()) + Expect(res.TotalMatches).To(Equal(int32(3))) + }) + 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))) + }) + It("search *file* in a root", func() { + res, err := doSearch(rootResource.ID, "Name:*file*", "") + Expect(err).ToNot(HaveOccurred()) + Expect(res.TotalMatches).To(Equal(int32(2))) + }) + It("search *file* in a subfolder", func() { + res, err := doSearch(rootResource.ID, "Name:*file*", "./doc") + Expect(err).ToNot(HaveOccurred()) + Expect(res.TotalMatches).To(Equal(int32(1))) + }) + }) }) Describe("Upsert", func() { diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index 1b4fe8fcd..43707a07f 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -332,6 +332,9 @@ 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()