Fix keeping the index in sync when directories contain special chars

This commit is contained in:
André Duffeck
2022-05-23 15:24:39 +02:00
parent cd87caede9
commit 211ac39a61
2 changed files with 20 additions and 14 deletions

View File

@@ -23,6 +23,7 @@ import (
"errors"
"math"
"path"
"regexp"
"strings"
"time"
@@ -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
@@ -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
@@ -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
@@ -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")
}

View File

@@ -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{
@@ -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() {
@@ -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)
})
})
@@ -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)
})
})
@@ -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"))