Skip to content

Commit

Permalink
Merge pull request #3851 from aduffeck/keep-index-in-sync
Browse files Browse the repository at this point in the history
Fix keeping the index in sync when directories contain special chars
  • Loading branch information
butonic authored May 23, 2022
2 parents cd87cae + 644386b commit 8130d4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-index-integrity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Fix search index getting out of sync

We fixed a problem where the search index got out of sync with child elements of a parent containing special characters.

https://github.com/owncloud/ocis/pull/3851
14 changes: 10 additions & 4 deletions extensions/search/pkg/search/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"math"
"path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -111,7 +112,7 @@ func (i *Index) markAsDeleted(id string, deleted bool) error {
if doc.Type == uint64(sprovider.ResourceType_RESOURCE_TYPE_CONTAINER) {
query := bleve.NewConjunctionQuery(
bleve.NewQueryStringQuery("RootID:"+doc.RootID),
bleve.NewQueryStringQuery("Path:"+doc.Path+"/*"),
bleve.NewQueryStringQuery("Path:"+queryEscape(doc.Path+"/*")),
)
bleveReq := bleve.NewSearchRequest(query)
bleveReq.Size = math.MaxInt
Expand Down Expand Up @@ -187,7 +188,7 @@ func (i *Index) Move(id *sprovider.ResourceId, fullPath string) error {
if doc.Type == uint64(sprovider.ResourceType_RESOURCE_TYPE_CONTAINER) {
query := bleve.NewConjunctionQuery(
bleve.NewQueryStringQuery("RootID:"+doc.RootID),
bleve.NewQueryStringQuery("Path:"+oldName+"/*"),
bleve.NewQueryStringQuery("Path:"+queryEscape(oldName+"/*")),
)
bleveReq := bleve.NewSearchRequest(query)
bleveReq.Size = math.MaxInt
Expand Down Expand Up @@ -217,8 +218,8 @@ func (i *Index) Search(ctx context.Context, req *searchsvc.SearchIndexRequest) (
query := bleve.NewConjunctionQuery(
bleve.NewQueryStringQuery(req.Query),
deletedQuery, // Skip documents that have been marked as deleted
bleve.NewQueryStringQuery("RootID:"+req.Ref.ResourceId.StorageId+"!"+req.Ref.ResourceId.OpaqueId), // Limit search to the space
bleve.NewQueryStringQuery("Path:"+utils.MakeRelativePath(path.Join(req.Ref.Path, "/"))+"*"), // Limit search to this directory in the space
bleve.NewQueryStringQuery("RootID:"+req.Ref.ResourceId.StorageId+"!"+req.Ref.ResourceId.OpaqueId), // Limit search to the space
bleve.NewQueryStringQuery("Path:"+queryEscape(utils.MakeRelativePath(path.Join(req.Ref.Path, "/"))+"*")), // Limit search to this directory in the space
)
bleveReq := bleve.NewSearchRequest(query)
bleveReq.Size = 200
Expand Down Expand Up @@ -340,3 +341,8 @@ func idToBleveId(id *sprovider.ResourceId) string {
}
return id.StorageId + "!" + id.OpaqueId
}

func queryEscape(s string) string {
re := regexp.MustCompile(`([` + regexp.QuoteMeta(`+=&|><!(){}[]^\"~*?:\/`) + `\-\s])`)
return re.ReplaceAllString(s, "\\$1")
}
20 changes: 10 additions & 10 deletions extensions/search/pkg/search/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ var _ = Describe("Index", func() {
ri *sprovider.ResourceInfo
parentRef = &sprovider.Reference{
ResourceId: rootId,
Path: "./my/sudbir",
Path: "./my/sub d!r",
}
parentRi = &sprovider.ResourceInfo{
Id: &sprovider.ResourceId{
StorageId: "storageid",
OpaqueId: "parentopaqueid",
},
Path: "subdir",
Path: "sub d!r",
Size: 12345,
Type: sprovider.ResourceType_RESOURCE_TYPE_CONTAINER,
Mtime: &typesv1beta1.Timestamp{Seconds: 4000},
}
childRef = &sprovider.Reference{
ResourceId: rootId,
Path: "./my/sudbir/child.pdf",
Path: "./my/sub d!r/child.pdf",
}
childRi = &sprovider.ResourceInfo{
Id: &sprovider.ResourceId{
Expand Down Expand Up @@ -298,12 +298,12 @@ var _ = Describe("Index", func() {
It("marks a resource as deleted", func() {
err := i.Add(parentRef, parentRi)
Expect(err).ToNot(HaveOccurred())
assertDocCount(rootId, "subdir", 1)
assertDocCount(rootId, `sub\ d!r`, 1)

err = i.Delete(parentRi.Id)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 0)
assertDocCount(rootId, `sub\ d!r`, 0)
})

It("also marks child resources as deleted", func() {
Expand All @@ -312,13 +312,13 @@ var _ = Describe("Index", func() {
err = i.Add(childRef, childRi)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 1)
assertDocCount(rootId, `sub\ d\!r`, 1)
assertDocCount(rootId, "child.pdf", 1)

err = i.Delete(parentRi.Id)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 0)
assertDocCount(rootId, `sub\ d\!r`, 0)
assertDocCount(rootId, "child.pdf", 0)
})
})
Expand All @@ -332,13 +332,13 @@ var _ = Describe("Index", func() {
err = i.Delete(parentRi.Id)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 0)
assertDocCount(rootId, `sub\ d!r`, 0)
assertDocCount(rootId, "child.pdf", 0)

err = i.Restore(parentRi.Id)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 1)
assertDocCount(rootId, `sub\ d!r`, 1)
assertDocCount(rootId, "child.pdf", 1)
})
})
Expand All @@ -354,7 +354,7 @@ var _ = Describe("Index", func() {
err = i.Move(parentRi.Id, "./somewhere/else/newname")
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 0)
assertDocCount(rootId, `sub\ d!r`, 0)

matches := assertDocCount(rootId, "Name:child.pdf", 1)
Expect(matches[0].Entity.Ref.Path).To(Equal("./somewhere/else/newname/child.pdf"))
Expand Down

0 comments on commit 8130d4e

Please sign in to comment.