Skip to content

Commit

Permalink
enhancement: remove legacy search query language
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Sep 5, 2023
1 parent cb2b8fb commit 577c1c0
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 125 deletions.
4 changes: 2 additions & 2 deletions .drone.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# The test runner source for UI tests
WEB_COMMITID=a26f7920d4ef8bf57488b18e451525d16e246e9e
WEB_BRANCH=stable-7.1
WEB_COMMITID=f034df64052bbb4e5358490fd20a7f9ff8e6de43
WEB_BRANCH=use-kql
22 changes: 6 additions & 16 deletions services/search/pkg/engine/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/blevesearch/bleve/v2/mapping"
"github.com/blevesearch/bleve/v2/search/query"
storageProvider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"go-micro.dev/v4/metadata"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cs3org/reva/v2/pkg/storagespace"
Expand All @@ -29,12 +28,12 @@ import (
searchService "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0"
"github.com/owncloud/ocis/v2/services/search/pkg/content"
searchQuery "github.com/owncloud/ocis/v2/services/search/pkg/query"
bleveQuery "github.com/owncloud/ocis/v2/services/search/pkg/query/bleve"
)

// Bleve represents a search engine which utilizes bleve to search and store resources.
type Bleve struct {
index bleve.Index
index bleve.Index
queryCreator searchQuery.Creator[query.Query]
}

// NewBleveIndex returns a new bleve index
Expand All @@ -59,9 +58,10 @@ func NewBleveIndex(root string) (bleve.Index, error) {
}

// NewBleveEngine creates a new Bleve instance
func NewBleveEngine(index bleve.Index) *Bleve {
func NewBleveEngine(index bleve.Index, queryCreator searchQuery.Creator[query.Query]) *Bleve {
return &Bleve{
index: index,
index: index,
queryCreator: queryCreator,
}
}

Expand Down Expand Up @@ -119,17 +119,7 @@ func BuildBleveMapping() (mapping.IndexMapping, error) {
// Search executes a search request operation within the index.
// Returns a SearchIndexResponse object or an error.
func (b *Bleve) Search(ctx context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) {
queryType, _ := metadata.Get(ctx, bleveQuery.QueryTypeHeader)

var queryCreator searchQuery.Creator[query.Query]
switch queryType {
case bleveQuery.QueryTypeLegacy:
queryCreator = bleveQuery.LegacyCreator
default:
queryCreator = bleveQuery.DefaultCreator
}

createdQuery, err := queryCreator.Create(sir.Query)
createdQuery, err := b.queryCreator.Create(sir.Query)
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions services/search/pkg/engine/bleve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var _ = Describe("Bleve", func() {
idx, err = bleveSearch.NewMemOnly(mapping)
Expect(err).ToNot(HaveOccurred())

eng = engine.NewBleveEngine(idx)
eng = engine.NewBleveEngine(idx, bleve.DefaultCreator)
Expect(err).ToNot(HaveOccurred())

rootResource = engine.Resource{
Expand Down Expand Up @@ -96,7 +96,7 @@ var _ = Describe("Bleve", func() {

Describe("New", func() {
It("returns a new index instance", func() {
b := engine.NewBleveEngine(idx)
b := engine.NewBleveEngine(idx, bleve.DefaultCreator)
Expect(b).ToNot(BeNil())
})
})
Expand Down Expand Up @@ -136,7 +136,7 @@ var _ = Describe("Bleve", func() {
err := eng.Upsert(parentResource.ID, parentResource)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootResource.ID, `Name:foo\ o*`, 1)
assertDocCount(rootResource.ID, `name:"foo o*"`, 1)
})

It("finds files by digits in the filename", func() {
Expand Down Expand Up @@ -411,14 +411,14 @@ var _ = Describe("Bleve", func() {
err = eng.Upsert(childResource.ID, childResource)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootResource.ID, parentResource.Document.Name, 1)
assertDocCount(rootResource.ID, childResource.Document.Name, 1)
assertDocCount(rootResource.ID, `"`+parentResource.Document.Name+`"`, 1)
assertDocCount(rootResource.ID, `"`+childResource.Document.Name+`"`, 1)

err = eng.Delete(parentResource.ID)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootResource.ID, parentResource.Document.Name, 0)
assertDocCount(rootResource.ID, childResource.Document.Name, 0)
assertDocCount(rootResource.ID, `"`+parentResource.Document.Name+`"`, 0)
assertDocCount(rootResource.ID, `"`+childResource.Document.Name+`"`, 0)
})
})

Expand All @@ -433,14 +433,14 @@ var _ = Describe("Bleve", func() {
err = eng.Delete(parentResource.ID)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootResource.ID, parentResource.Name, 0)
assertDocCount(rootResource.ID, childResource.Name, 0)
assertDocCount(rootResource.ID, `"`+parentResource.Name+`"`, 0)
assertDocCount(rootResource.ID, `"`+childResource.Name+`"`, 0)

err = eng.Restore(parentResource.ID)
Expect(err).ToNot(HaveOccurred())

assertDocCount(rootResource.ID, parentResource.Name, 1)
assertDocCount(rootResource.ID, childResource.Name, 1)
assertDocCount(rootResource.ID, `"`+parentResource.Name+`"`, 1)
assertDocCount(rootResource.ID, `"`+childResource.Name+`"`, 1)
})
})

Expand Down
3 changes: 0 additions & 3 deletions services/search/pkg/query/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ func (c Creator[T]) Create(qs string) (T, error) {
return t, nil
}

// LegacyCreator exposes an ocis legacy bleve query creator.
var LegacyCreator = Creator[bQuery.Query]{LegacyBuilder{}, LegacyCompiler{}}

// DefaultCreator exposes a kql to bleve query creator.
var DefaultCreator = Creator[bQuery.Query]{kql.Builder{}, Compiler{}}
10 changes: 9 additions & 1 deletion services/search/pkg/query/bleve/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var _fields = map[string]string{
"tag": "Tags",
"tags": "Tags",
"content": "Content",
"hidden": "Hidden",
}

// Compiler represents a KQL query search string to the bleve query formatter.
Expand Down Expand Up @@ -53,7 +54,14 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int) {
for i := offset; i < len(nodes); i++ {
switch n := nodes[i].(type) {
case *ast.StringNode:
q := bleveQuery.NewQueryStringQuery(getField(n.Key) + ":" + strings.ReplaceAll(n.Value, " ", `\ `))
k := getField(n.Key)
v := strings.ReplaceAll(n.Value, " ", `\ `)

if k != "Hidden" {
v = strings.ToLower(v)
}

q := bleveQuery.NewQueryStringQuery(k + ":" + v)
if prev == nil {
prev = q
} else {
Expand Down
32 changes: 25 additions & 7 deletions services/search/pkg/query/bleve/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_compile(t *testing.T) {
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name:John\ Smith`),
query.NewQueryStringQuery(`Name:john\ smith`),
}),
wantErr: false,
},
Expand All @@ -50,8 +50,8 @@ func Test_compile(t *testing.T) {
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name:John\ Smith`),
query.NewQueryStringQuery(`Name:Jane`),
query.NewQueryStringQuery(`Name:john\ smith`),
query.NewQueryStringQuery(`Name:jane`),
}),
wantErr: false,
},
Expand Down Expand Up @@ -173,8 +173,8 @@ func Test_compile(t *testing.T) {
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`author:John\ Smith`),
query.NewQueryStringQuery(`author:Jane`),
query.NewQueryStringQuery(`author:john\ smith`),
query.NewQueryStringQuery(`author:jane`),
}),
wantErr: false,
},
Expand All @@ -195,12 +195,30 @@ func Test_compile(t *testing.T) {
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`author:John\ Smith`),
query.NewQueryStringQuery(`author:Jane`),
query.NewQueryStringQuery(`author:john\ smith`),
query.NewQueryStringQuery(`author:jane`),
query.NewQueryStringQuery(`Tags:bestseller`),
}),
wantErr: false,
},
{
name: `StringNode value lowercase`,
args: &ast.Ast{
Nodes: []ast.Node{
&ast.StringNode{Value: "John Smith"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "Hidden", Value: "T"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "hidden", Value: "T"},
},
},
want: query.NewConjunctionQuery([]query.Query{
query.NewQueryStringQuery(`Name:john\ smith`),
query.NewQueryStringQuery(`Hidden:T`),
query.NewQueryStringQuery(`Hidden:T`),
}),
wantErr: false,
},
}

assert := tAssert.New(t)
Expand Down
76 changes: 0 additions & 76 deletions services/search/pkg/query/bleve/legacy.go

This file was deleted.

3 changes: 2 additions & 1 deletion services/search/pkg/service/grpc/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0"
"github.com/owncloud/ocis/v2/services/search/pkg/content"
"github.com/owncloud/ocis/v2/services/search/pkg/engine"
"github.com/owncloud/ocis/v2/services/search/pkg/query/bleve"
"github.com/owncloud/ocis/v2/services/search/pkg/search"
)

Expand All @@ -48,7 +49,7 @@ func NewHandler(opts ...Option) (searchsvc.SearchProviderHandler, func(), error)
_ = idx.Close()
}

eng = engine.NewBleveEngine(idx)
eng = engine.NewBleveEngine(idx, bleve.DefaultCreator)
default:
return nil, teardown, fmt.Errorf("unknown search engine: %s", cfg.Engine.Type)
}
Expand Down
10 changes: 2 additions & 8 deletions services/webdav/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/go-chi/render"
"github.com/riandyrn/otelchi"
merrors "go-micro.dev/v4/errors"
"go-micro.dev/v4/metadata"
grpcmetadata "google.golang.org/grpc/metadata"

"github.com/owncloud/ocis/v2/ocis-pkg/log"
Expand All @@ -28,7 +27,6 @@ import (
thumbnailsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/thumbnails/v0"
searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0"
thumbnailssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/thumbnails/v0"
"github.com/owncloud/ocis/v2/services/search/pkg/query/bleve"
"github.com/owncloud/ocis/v2/services/webdav/pkg/config"
"github.com/owncloud/ocis/v2/services/webdav/pkg/constants"
"github.com/owncloud/ocis/v2/services/webdav/pkg/dav/requests"
Expand Down Expand Up @@ -117,12 +115,8 @@ func NewService(opts ...Option) (Service, error) {
r.Get("/dav/files/{id}", svc.Thumbnail)
r.Get("/dav/files/{id}/*", svc.Thumbnail)

legacySearchHandler := func(w http.ResponseWriter, r *http.Request) {
ctx := metadata.Set(r.Context(), bleve.QueryTypeHeader, bleve.QueryTypeLegacy)
svc.Search(w, r.WithContext(ctx))
}
r.MethodFunc("REPORT", "/remote.php/dav/files*", legacySearchHandler)
r.MethodFunc("REPORT", "/dav/files*", legacySearchHandler)
r.MethodFunc("REPORT", "/remote.php/dav/files*", svc.Search)
r.MethodFunc("REPORT", "/dav/files*", svc.Search)
})

r.Group(func(r chi.Router) {
Expand Down

0 comments on commit 577c1c0

Please sign in to comment.