Add etag, mtime and mimetype to the search index and response

This commit is contained in:
André Duffeck
2022-04-25 11:23:30 +02:00
parent 962840ec55
commit 51cf27d767
14 changed files with 196 additions and 81 deletions
+33 -12
View File
@@ -21,10 +21,12 @@ package index
import (
"context"
"strings"
"time"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
"github.com/blevesearch/bleve/v2/mapping"
"google.golang.org/protobuf/types/known/timestamppb"
sprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
searchmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/search/v0"
@@ -36,8 +38,11 @@ type indexDocument struct {
Path string
ID string
Name string
Size uint64
Name string
Etag string
Size uint64
Mtime string
MimeType string
}
// Index represents a bleve based search index
@@ -108,20 +113,28 @@ func BuildMapping() mapping.IndexMapping {
}
func toEntity(ref *sprovider.Reference, ri *sprovider.ResourceInfo) *indexDocument {
return &indexDocument{
RootID: idToBleveId(ref.ResourceId),
Path: ref.Path,
ID: idToBleveId(ri.Id),
Name: ri.Path,
Size: ri.Size,
doc := &indexDocument{
RootID: idToBleveId(ref.ResourceId),
Path: ref.Path,
ID: idToBleveId(ri.Id),
Name: ri.Path,
Etag: ri.Etag,
Size: ri.Size,
MimeType: ri.MimeType,
}
if ri.Mtime != nil {
doc.Mtime = time.Unix(int64(ri.Mtime.Seconds), int64(ri.Mtime.Nanos)).UTC().Format(time.RFC3339)
}
return doc
}
func fromFields(fields map[string]interface{}) (*searchmsg.Match, error) {
rootIDParts := strings.SplitN(fields["RootID"].(string), "!", 2)
IDParts := strings.SplitN(fields["ID"].(string), "!", 2)
return &searchmsg.Match{
match := &searchmsg.Match{
Entity: &searchmsg.Entity{
Ref: &searchmsg.Reference{
ResourceId: &searchmsg.ResourceID{
@@ -134,10 +147,18 @@ func fromFields(fields map[string]interface{}) (*searchmsg.Match, error) {
StorageId: IDParts[0],
OpaqueId: IDParts[1],
},
Name: fields["Name"].(string),
Size: uint64(fields["Size"].(float64)),
Name: fields["Name"].(string),
Size: uint64(fields["Size"].(float64)),
Etag: fields["Etag"].(string),
MimeType: fields["MimeType"].(string),
},
}, nil
}
if mtime, err := time.Parse(time.RFC3339, fields["Mtime"].(string)); err == nil {
match.Entity.LastModifiedTime = &timestamppb.Timestamp{Seconds: mtime.Unix(), Nanos: int32(mtime.Nanosecond())}
}
return match, nil
}
func idToBleveId(id *sprovider.ResourceId) string {
@@ -5,6 +5,7 @@ import (
"github.com/blevesearch/bleve/v2"
sprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/owncloud/ocis/extensions/search/pkg/search/index"
searchmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/search/v0"
searchsvc "github.com/owncloud/ocis/protogen/gen/ocis/services/search/v0"
@@ -43,8 +44,11 @@ var _ = Describe("Index", func() {
StorageId: "storageid",
OpaqueId: "opaqueid",
},
Path: "foo.pdf",
Size: 12345,
Path: "foo.pdf",
Size: 12345,
Etag: "abcde",
MimeType: "application/pdf",
Mtime: &typesv1beta1.Timestamp{Seconds: 4000},
}
})
@@ -101,6 +105,30 @@ var _ = Describe("Index", func() {
Expect(len(res.Matches)).To(Equal(0))
})
It("returns all desired fields", func() {
res, err := i.Search(ctx, &searchsvc.SearchIndexRequest{
Ref: &searchmsg.Reference{
ResourceId: &searchmsg.ResourceID{
StorageId: ref.ResourceId.StorageId,
OpaqueId: ref.ResourceId.OpaqueId,
},
},
Query: "foo.pdf",
})
Expect(err).ToNot(HaveOccurred())
Expect(res).ToNot(BeNil())
Expect(len(res.Matches)).To(Equal(1))
match := res.Matches[0]
Expect(match.Entity.Ref.ResourceId.OpaqueId).To(Equal(ref.ResourceId.OpaqueId))
Expect(match.Entity.Ref.Path).To(Equal(ref.Path))
Expect(match.Entity.Id.OpaqueId).To(Equal(ri.Id.OpaqueId))
Expect(match.Entity.Name).To(Equal(ri.Path))
Expect(match.Entity.Size).To(Equal(ri.Size))
Expect(match.Entity.Etag).To(Equal(ri.Etag))
Expect(match.Entity.MimeType).To(Equal(ri.MimeType))
Expect(uint64(match.Entity.LastModifiedTime.AsTime().Unix())).To(Equal(ri.Mtime.Seconds))
})
It("finds files by name, prefix or substring match", func() {
queries := []string{"foo.pdf", "foo*", "*oo.p*"}
for _, query := range queries {
@@ -165,11 +193,6 @@ var _ = Describe("Index", func() {
Expect(err).ToNot(HaveOccurred())
Expect(res).ToNot(BeNil())
Expect(len(res.Matches)).To(Equal(1), "query returned no result: "+query)
Expect(res.Matches[0].Entity.Ref.ResourceId.OpaqueId).To(Equal(nestedRef.ResourceId.OpaqueId))
Expect(res.Matches[0].Entity.Ref.Path).To(Equal(nestedRef.Path))
Expect(res.Matches[0].Entity.Id.OpaqueId).To(Equal(nestedRI.Id.OpaqueId))
Expect(res.Matches[0].Entity.Name).To(Equal(nestedRI.Path))
Expect(res.Matches[0].Entity.Size).To(Equal(nestedRI.Size))
}
})