Skip to content

Commit

Permalink
Provide Search filter for locations
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Jul 4, 2023
1 parent 941ef59 commit 81b884d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Provide Search filter for locations

The search result REPORT response now contains a content preview which highlights the search term.
The feature is only available if content extraction (e.g. apache tika) is configured

https://github.com/owncloud/ocis/pull/6713
OCIS-3705
29 changes: 29 additions & 0 deletions services/search/pkg/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
searchmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/search/v0"
Expand Down Expand Up @@ -155,3 +156,31 @@ func convertToWebDAVPermissions(isShared, isMountpoint, isDir bool, p *provider.
}
return b.String()
}

func extractScope(path string) (*searchmsg.Reference, error) {
ref, err := storagespace.ParseReference(path)
if err != nil {
return nil, err
}
return &searchmsg.Reference{
ResourceId: &searchmsg.ResourceID{
StorageId: ref.ResourceId.StorageId,
SpaceId: ref.ResourceId.SpaceId,
OpaqueId: ref.ResourceId.OpaqueId,
},
Path: ref.GetPath(),
}, nil
}

// ParseScope extract a scope value from the query string
func ParseScope(query string) (search, scope string) {
match := scopeRegex.FindStringSubmatch(query)
if len(match) >= 2 {
cut := match[0]
scope = strings.TrimSpace(match[1])
search = strings.TrimSpace(strings.ReplaceAll(query, cut, ""))
return
}
search = query
return
}
48 changes: 34 additions & 14 deletions services/search/pkg/search/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -56,6 +57,7 @@ type Service struct {
}

var errSkipSpace error
var scopeRegex = regexp.MustCompile(`scope:\s*([^" "\n\r]*)`)

// NewService creates a new Provider instance.
func NewService(gatewaySelector pool.Selectable[gateway.GatewayAPIClient], eng engine.Engine, extractor content.Extractor, logger log.Logger, cfg *config.Config) *Service {
Expand All @@ -72,10 +74,29 @@ func NewService(gatewaySelector pool.Selectable[gateway.GatewayAPIClient], eng e

// Search processes a search request and passes it down to the engine.
func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*searchsvc.SearchResponse, error) {
if req.Query == "" {
s.logger.Debug().Str("query", req.Query).Msg("performing a search")
query, path := ParseScope(req.Query)
if query == "" {
return nil, errtypes.BadRequest("empty query provided")
}
s.logger.Debug().Str("query", req.Query).Msg("performing a search")
req.Query = query
var filters []*provider.ListStorageSpacesRequest_Filter
if len(path) > 0 {
ref, err := extractScope(path)
if err != nil {
return nil, err
}
if req.Ref == nil {
req.Ref = ref
}
req.Ref.Path = ref.GetPath()
filters = []*provider.ListStorageSpacesRequest_Filter{
{
Type: provider.ListStorageSpacesRequest_Filter_TYPE_ID,
Term: &provider.ListStorageSpacesRequest_Filter_Id{Id: &provider.StorageSpaceId{OpaqueId: ref.GetResourceId().OpaqueId}},
},
}
}

gatewayClient, err := s.gatewaySelector.Next()
if err != nil {
Expand All @@ -84,18 +105,17 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se

currentUser := revactx.ContextMustGetUser(ctx)

listSpacesRes, err := gatewayClient.ListStorageSpaces(ctx, &provider.ListStorageSpacesRequest{
Filters: []*provider.ListStorageSpacesRequest_Filter{
{
Type: provider.ListStorageSpacesRequest_Filter_TYPE_USER,
Term: &provider.ListStorageSpacesRequest_Filter_User{User: currentUser.GetId()},
},
{
Type: provider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE,
Term: &provider.ListStorageSpacesRequest_Filter_SpaceType{SpaceType: "+grant"},
},
filters = append(filters, []*provider.ListStorageSpacesRequest_Filter{
{
Type: provider.ListStorageSpacesRequest_Filter_TYPE_USER,
Term: &provider.ListStorageSpacesRequest_Filter_User{User: currentUser.GetId()},
},
})
{
Type: provider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE,
Term: &provider.ListStorageSpacesRequest_Filter_SpaceType{SpaceType: "+grant"},
},
}...)
listSpacesRes, err := gatewayClient.ListStorageSpaces(ctx, &provider.ListStorageSpacesRequest{Filters: filters})
if err != nil {
s.logger.Error().Err(err).Msg("failed to list the user's storage spaces")
return nil, err
Expand Down Expand Up @@ -229,7 +249,7 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest,
rootName string
permissions *provider.ResourcePermissions
)
mountpointPrefix := ""
mountpointPrefix := req.GetRef().GetPath()
switch space.SpaceType {
case "mountpoint":
return nil, errSkipSpace // mountpoint spaces are only "links" to the shared spaces. we have to search the shared "grant" space instead
Expand Down
48 changes: 48 additions & 0 deletions services/search/pkg/search/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,51 @@ var _ = Describe("Searchprovider", func() {
})
})
})

var _ = DescribeTable("Parse Scope",
func(pattern, wantSearch, wantScope string) {
gotSearch, gotScope := search.ParseScope(pattern)
Expect(gotSearch).To(Equal(wantSearch))
Expect(gotScope).To(Equal(wantScope))
},
Entry("When scope is at the end of the line",
`+Name:*file* +Tags:&quot;foo&quot; scope:<uuid>/folder/subfolder`,
`+Name:*file* +Tags:&quot;foo&quot;`,
`<uuid>/folder/subfolder`,
),
Entry("When scope is at the end of the line 2",
`+Name:*file* +Tags:&quot;foo&quot; scope:<uuid>/folder`,
`+Name:*file* +Tags:&quot;foo&quot;`,
`<uuid>/folder`,
),
Entry("When scope is at the end of the line 3",
`file scope:<uuid>/folder/subfolder`,
`file`,
`<uuid>/folder/subfolder`,
),
Entry("When scope is at the end of the line with a space",
`+Name:*file* +Tags:&quot;foo&quot; scope: <uuid>/folder/subfolder`,
`+Name:*file* +Tags:&quot;foo&quot;`,
`<uuid>/folder/subfolder`,
),
Entry("When scope is in the middle of the line",
`+Name:*file* scope:<uuid>/folder/subfolder +Tags:&quot;foo&quot;`,
`+Name:*file* +Tags:&quot;foo&quot;`,
`<uuid>/folder/subfolder`,
),
Entry("When scope is at the end of the line",
`scope:<uuid>/folder/subfolder +Name:*file*`,
`+Name:*file*`,
`<uuid>/folder/subfolder`,
),
Entry("When scope is at the begging of the line",
`scope:<uuid>/folder/subfolder file`,
`file`,
`<uuid>/folder/subfolder`,
),
Entry("When no scope",
`+Name:*file* +Tags:&quot;foo&quot;`,
`+Name:*file* +Tags:&quot;foo&quot;`,
``,
),
)

0 comments on commit 81b884d

Please sign in to comment.