Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More fixes to searching in shares #3725

Merged
merged 8 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions extensions/search/pkg/search/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
"time"

"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
"github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
"github.com/blevesearch/bleve/v2/analysis/tokenizer/single"
"github.com/blevesearch/bleve/v2/mapping"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -57,7 +60,11 @@ type Index struct {

// NewPersisted returns a new instance of Index with the data being persisted in the given directory
func NewPersisted(path string) (*Index, error) {
bi, err := bleve.New(path, BuildMapping())
mapping, err := BuildMapping()
if err != nil {
return nil, err
}
bi, err := bleve.New(path, mapping)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,14 +164,14 @@ func (i *Index) Purge(id *sprovider.ResourceId) error {
return i.bleveIndex.Delete(idToBleveId(id))
}

// Purge removes an entity from the index
func (i *Index) Move(ri *sprovider.ResourceInfo) error {
// Move update the path of an entry and all its children
func (i *Index) Move(ri *sprovider.ResourceInfo, fullPath string) error {
doc, err := i.getEntity(idToBleveId(ri.Id))
if err != nil {
return err
}
oldName := doc.Path
newName := utils.MakeRelativePath(ri.Path)
newName := utils.MakeRelativePath(fullPath)

doc, err = i.updateEntity(idToBleveId(ri.Id), func(doc *indexDocument) {
doc.Path = newName
Expand Down Expand Up @@ -204,7 +211,7 @@ func (i *Index) Search(ctx context.Context, req *searchsvc.SearchIndexRequest) (
deletedQuery := bleve.NewBoolFieldQuery(false)
deletedQuery.SetField("Deleted")
query := bleve.NewConjunctionQuery(
bleve.NewQueryStringQuery("Name:"+req.Query),
bleve.NewQueryStringQuery("Name:"+strings.ToLower(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
Expand Down Expand Up @@ -232,10 +239,29 @@ func (i *Index) Search(ctx context.Context, req *searchsvc.SearchIndexRequest) (
}

// BuildMapping builds a bleve index mapping which can be used for indexing
func BuildMapping() mapping.IndexMapping {
func BuildMapping() (mapping.IndexMapping, error) {
nameMapping := bleve.NewTextFieldMapping()
nameMapping.Analyzer = "lowercaseKeyword"

docMapping := bleve.NewDocumentMapping()
docMapping.AddFieldMappingsAt("Name", nameMapping)

indexMapping := bleve.NewIndexMapping()
indexMapping.DefaultAnalyzer = keyword.Name
return indexMapping
indexMapping.DefaultMapping = docMapping
err := indexMapping.AddCustomAnalyzer("lowercaseKeyword",
map[string]interface{}{
"type": custom.Name,
"tokenizer": single.Name,
"token_filters": []string{
lowercase.Name,
},
})
if err != nil {
return nil, err
}

return indexMapping, nil
}

func toEntity(ref *sprovider.Reference, ri *sprovider.ResourceInfo) *indexDocument {
Expand Down
31 changes: 24 additions & 7 deletions extensions/search/pkg/search/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ var _ = Describe("Index", func() {
StorageId: "storageid",
OpaqueId: "someopaqueid",
},
Path: "foo.pdf",
Path: "Foo.pdf",
Size: 12345,
Type: sprovider.ResourceType_RESOURCE_TYPE_FILE,
MimeType: "application/pdf",
Mtime: &typesv1beta1.Timestamp{Seconds: 4000},
}
parentRef = &sprovider.Reference{
ResourceId: rootId,
Path: "./sudbir",
Path: "./my/sudbir",
}
parentRi = &sprovider.ResourceInfo{
Id: &sprovider.ResourceId{
Expand All @@ -59,7 +59,7 @@ var _ = Describe("Index", func() {
}
childRef = &sprovider.Reference{
ResourceId: rootId,
Path: "./sudbir/child.pdf",
Path: "./my/sudbir/child.pdf",
}
childRi = &sprovider.ResourceInfo{
Id: &sprovider.ResourceId{
Expand Down Expand Up @@ -92,8 +92,10 @@ var _ = Describe("Index", func() {
)

BeforeEach(func() {
var err error
bleveIndex, err = bleve.NewMemOnly(index.BuildMapping())
mapping, err := index.BuildMapping()
Expect(err).ToNot(HaveOccurred())

bleveIndex, err = bleve.NewMemOnly(mapping)
Expect(err).ToNot(HaveOccurred())

i, err = index.New(bleveIndex)
Expand Down Expand Up @@ -201,6 +203,21 @@ var _ = Describe("Index", func() {
}
})

It("is case-insensitive", func() {
res, err := i.Search(ctx, &searchsvc.SearchIndexRequest{
Ref: &searchmsg.Reference{
ResourceId: &searchmsg.ResourceID{
StorageId: ref.ResourceId.StorageId,
OpaqueId: ref.ResourceId.OpaqueId,
},
},
Query: "Foo*",
})
Expect(err).ToNot(HaveOccurred())
Expect(res).ToNot(BeNil())
Expect(len(res.Matches)).To(Equal(1))
})

Context("and an additional file in a subdirectory", func() {
var (
nestedRef *sprovider.Reference
Expand Down Expand Up @@ -349,7 +366,7 @@ var _ = Describe("Index", func() {
Expect(err).ToNot(HaveOccurred())

parentRi.Path = "newname"
err = i.Move(parentRi)
err = i.Move(parentRi, "./somewhere/else/newname")
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootId, "subdir", 0)
Expand All @@ -365,7 +382,7 @@ var _ = Describe("Index", func() {
})
Expect(err).ToNot(HaveOccurred())
Expect(len(res.Matches)).To(Equal(1))
Expect(res.Matches[0].Entity.Ref.Path).To(Equal("./newname/child.pdf"))
Expect(res.Matches[0].Entity.Ref.Path).To(Equal("./somewhere/else/newname/child.pdf"))
})
})
})
10 changes: 5 additions & 5 deletions extensions/search/pkg/search/mocks/IndexClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 44 additions & 14 deletions extensions/search/pkg/search/provider/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/events"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -59,17 +61,25 @@ func (p *Provider) handleEvent(ev interface{}) {
p.logger.Error().Err(err).Msg("failed to stat the changed resource")
return
}

switch statRes.Status.Code {
case rpc.Code_CODE_OK:
err = p.indexClient.Move(statRes.Info)
if err != nil {
p.logger.Error().Err(err).Msg("failed to restore the changed resource in the index")
}
default:
if statRes.Status.Code != rpc.Code_CODE_OK {
p.logger.Error().Interface("statRes", statRes).Msg("failed to stat the changed resource")
return
}

gpRes, err := p.getPath(statRes.Info.Id, owner)
if err != nil {
p.logger.Error().Err(err).Interface("ref", ref).Msg("failed to get path for moved resource")
return
}
if gpRes.Status.Code != rpcv1beta1.Code_CODE_OK {
p.logger.Error().Interface("status", gpRes.Status).Interface("ref", ref).Msg("failed to get path for moved resource")
return
}

err = p.indexClient.Move(statRes.Info, gpRes.Path)
if err != nil {
p.logger.Error().Err(err).Msg("failed to restore the changed resource in the index")
}
return
case events.ContainerCreated:
ref = e.Ref
Expand Down Expand Up @@ -111,18 +121,38 @@ func (p *Provider) handleEvent(ev interface{}) {
}

func (p *Provider) statResource(ref *provider.Reference, owner *user.User) (*provider.StatResponse, error) {
// Get auth
ownerCtx, err := p.getAuthContext(owner)
if err != nil {
return nil, err
}

// Stat changed resource resource
return p.gwClient.Stat(ownerCtx, &provider.StatRequest{Ref: ref})
}

func (p *Provider) getPath(id *provider.ResourceId, owner *user.User) (*provider.GetPathResponse, error) {
ownerCtx, err := p.getAuthContext(owner)
if err != nil {
return nil, err
}

// Stat changed resource resource
return p.gwClient.GetPath(ownerCtx, &provider.GetPathRequest{ResourceId: id})
}

func (p *Provider) getAuthContext(owner *user.User) (context.Context, error) {
ownerCtx := ctxpkg.ContextSetUser(context.Background(), owner)
authRes, err := p.gwClient.Authenticate(ownerCtx, &gateway.AuthenticateRequest{
Type: "machine",
ClientId: "userid:" + owner.Id.OpaqueId,
ClientSecret: p.machineAuthAPIKey,
})
if err != nil || authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
if err == nil && authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
err = errtypes.NewErrtypeFromStatus(authRes.Status)
}
if err != nil {
p.logger.Error().Err(err).Interface("authRes", authRes).Msg("error using machine auth")
aduffeck marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
ownerCtx = metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token)

// Stat changed resource resource
return p.gwClient.Stat(ownerCtx, &provider.StatRequest{Ref: ref})
return metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token), nil
}
Loading