Fix search for files with spaces

This commit is contained in:
André Duffeck
2022-05-12 09:47:30 +02:00
parent 22f5e471bd
commit 49da927f25
3 changed files with 71 additions and 21 deletions

View File

@@ -24,25 +24,9 @@ var _ = Describe("Index", func() {
StorageId: "storageid",
OpaqueId: "rootopaqueid",
}
ref = &sprovider.Reference{
ResourceId: rootId,
Path: "./Foo.pdf",
}
ri = &sprovider.ResourceInfo{
Id: &sprovider.ResourceId{
StorageId: "storageid",
OpaqueId: "opaqueid",
},
ParentId: &sprovider.ResourceId{
StorageId: "storageid",
OpaqueId: "someopaqueid",
},
Path: "Foo.pdf",
Size: 12345,
Type: sprovider.ResourceType_RESOURCE_TYPE_FILE,
MimeType: "application/pdf",
Mtime: &typesv1beta1.Timestamp{Seconds: 4000},
}
filename string
ref *sprovider.Reference
ri *sprovider.ResourceInfo
parentRef = &sprovider.Reference{
ResourceId: rootId,
Path: "./my/sudbir",
@@ -92,6 +76,8 @@ var _ = Describe("Index", func() {
)
BeforeEach(func() {
filename = "Foo.pdf"
mapping, err := index.BuildMapping()
Expect(err).ToNot(HaveOccurred())
@@ -102,6 +88,28 @@ var _ = Describe("Index", func() {
Expect(err).ToNot(HaveOccurred())
})
JustBeforeEach(func() {
ref = &sprovider.Reference{
ResourceId: rootId,
Path: "./" + filename,
}
ri = &sprovider.ResourceInfo{
Id: &sprovider.ResourceId{
StorageId: "storageid",
OpaqueId: "opaqueid",
},
ParentId: &sprovider.ResourceId{
StorageId: "storageid",
OpaqueId: "someopaqueid",
},
Path: filename,
Size: 12345,
Type: sprovider.ResourceType_RESOURCE_TYPE_FILE,
MimeType: "application/pdf",
Mtime: &typesv1beta1.Timestamp{Seconds: 4000},
}
})
Describe("New", func() {
It("returns a new index instance", func() {
i, err := index.New(bleveIndex)
@@ -119,8 +127,32 @@ var _ = Describe("Index", func() {
})
Describe("Search", func() {
Context("with a file in the root of the space", func() {
Context("with a filename with spaces", func() {
BeforeEach(func() {
filename = "Foo oo.pdf"
})
It("finds the file", func() {
err := i.Add(ref, ri)
Expect(err).ToNot(HaveOccurred())
res, err := i.Search(ctx, &searchsvc.SearchIndexRequest{
Ref: &searchmsg.Reference{
ResourceId: &searchmsg.ResourceID{
StorageId: ref.ResourceId.StorageId,
OpaqueId: ref.ResourceId.OpaqueId,
},
},
Query: `Name:foo\ o*`,
})
Expect(err).ToNot(HaveOccurred())
Expect(res).ToNot(BeNil())
Expect(len(res.Matches)).To(Equal(1))
})
})
Context("with a file in the root of the space", func() {
JustBeforeEach(func() {
err := i.Add(ref, ri)
Expect(err).ToNot(HaveOccurred())
})

View File

@@ -125,7 +125,7 @@ func (p *Provider) Search(ctx context.Context, req *searchsvc.SearchRequest) (*s
_, rootStorageID := storagespace.SplitStorageID(space.Root.StorageId)
res, err := p.indexClient.Search(ctx, &searchsvc.SearchIndexRequest{
Query: "Name:" + strings.ToLower(req.Query),
Query: "Name:" + strings.ReplaceAll(strings.ToLower(req.Query), " ", `\ `),
Ref: &searchmsg.Reference{
ResourceId: &searchmsg.ResourceID{
StorageId: space.Root.StorageId,

View File

@@ -146,6 +146,24 @@ var _ = Describe("Searchprovider", func() {
}, nil)
})
It("lowercases the filename", func() {
p.Search(ctx, &searchsvc.SearchRequest{
Query: "Foo.pdf",
})
indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool {
return req.Query == "Name:foo.pdf"
}))
})
It("escapes special characters", func() {
p.Search(ctx, &searchsvc.SearchRequest{
Query: "Foo oo.pdf",
})
indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool {
return req.Query == `Name:foo\ oo.pdf`
}))
})
It("searches the personal user space", func() {
res, err := p.Search(ctx, &searchsvc.SearchRequest{
Query: "foo",