Skip to content

Commit

Permalink
enhancement: introduce query build and compile interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Aug 27, 2023
1 parent 6b58c6b commit 4b0ed10
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 565 deletions.
83 changes: 20 additions & 63 deletions services/search/pkg/engine/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math"
"path"
"path/filepath"
"regexp"
"strings"
"time"

Expand All @@ -18,21 +17,24 @@ import (
"github.com/blevesearch/bleve/v2/analysis/tokenizer/single"
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
"github.com/blevesearch/bleve/v2/mapping"
"github.com/blevesearch/bleve/v2/search/query"
bleveQuery "github.com/blevesearch/bleve/v2/search/query"
storageProvider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"

searchMessage "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/search/v0"
searchService "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/query"
)

// Bleve represents a search engine which utilizes bleve to search and store resources.
type Bleve struct {
searchQuery SearchQuery
index bleve.Index
queryBuilder query.Builder
queryCompiler query.Compiler[bleveQuery.Query]
index bleve.Index
}

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

// NewBleveEngine creates a new Bleve instance
func NewBleveEngine(index bleve.Index, searchQuery SearchQuery) *Bleve {
if searchQuery == nil {
searchQuery = &BaseQuery{}
}
func NewBleveEngine(index bleve.Index, queryBuilder query.Builder, queryCompiler query.Compiler[bleveQuery.Query]) *Bleve {
return &Bleve{
index: index,
searchQuery: searchQuery,
index: index,
queryCompiler: queryCompiler,
queryBuilder: queryBuilder,
}
}

Expand Down Expand Up @@ -121,23 +121,29 @@ 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(_ context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) {
sq, err := b.searchQuery.FormatQuery(sir.Query)
builderAst, err := b.queryBuilder.Build(sir.Query)
if err != nil {
return nil, err
}

compilerQuery, err := b.queryCompiler.Compile(builderAst)
if err != nil {
return nil, err
}

q := bleve.NewConjunctionQuery(
// Skip documents that have been marked as deleted
&query.BoolFieldQuery{
&bleveQuery.BoolFieldQuery{
Bool: false,
FieldVal: "Deleted",
},
sq,
compilerQuery,
)

if sir.Ref != nil {
q.Conjuncts = append(
q.Conjuncts,
&query.TermQuery{
&bleveQuery.TermQuery{
FieldVal: "RootID",
Term: storagespace.FormatResourceID(
storageProvider.ResourceId{
Expand Down Expand Up @@ -372,52 +378,3 @@ func (b *Bleve) setDeleted(id string, deleted bool) error {

return nil
}

// SearchQuery is interface of conversion the query search string to the bleve query.
type SearchQuery interface {
FormatQuery(queryStr string) (query.Query, error)
}

// BaseQuery represents a default bleve query formatter.
type BaseQuery struct{}

// FormatQuery implements the default bleve query formatter which converts the bleve likes query search string to the bleve query.
func (s *BaseQuery) FormatQuery(queryStr string) (query.Query, error) {
return &query.QueryStringQuery{
Query: formatQuery(queryStr),
}, nil
}

func formatQuery(q string) string {
cq := q
fields := []string{"RootID", "Path", "ID", "Name", "Size", "Mtime", "MimeType", "Type"}
for _, field := range fields {
cq = strings.ReplaceAll(cq, strings.ToLower(field)+":", field+":")
}

fieldRe := regexp.MustCompile(`\w+:[^ ]+`)
if fieldRe.MatchString(cq) {
nameTagesRe := regexp.MustCompile(`\+?(Name|Tags)`) // detect "Name", "+Name, "Tags" and "+Tags"
parts := strings.Split(cq, " ")

cq = ""
for _, part := range parts {
fieldParts := strings.SplitN(part, ":", 2)
if len(fieldParts) > 1 {
key := fieldParts[0]
value := fieldParts[1]
if nameTagesRe.MatchString(key) {
value = strings.ToLower(value) // do a lowercase query on the lowercased fields
}
cq += key + ":" + value + " "
} else {
cq += part + " "
}
}
return cq // Sophisticated field based search
}

// this is a basic filename search
cq = strings.ReplaceAll(cq, ":", `\:`)
return "Name:*" + strings.ReplaceAll(strings.ToLower(cq), " ", `\ `) + "*"
}
15 changes: 8 additions & 7 deletions services/search/pkg/engine/bleve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/cs3org/reva/v2/pkg/storagespace"

"github.com/blevesearch/bleve/v2"
blevyV2 "github.com/blevesearch/bleve/v2"
sprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -15,12 +15,13 @@ 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"
)

var _ = Describe("Bleve", func() {
var (
eng *engine.Bleve
idx bleve.Index
idx blevyV2.Index
ctx context.Context

doSearch = func(id string, query, path string) (*searchsvc.SearchIndexResponse, error) {
Expand Down Expand Up @@ -59,10 +60,10 @@ var _ = Describe("Bleve", func() {
mapping, err := engine.BuildBleveMapping()
Expect(err).ToNot(HaveOccurred())

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

eng = engine.NewBleveEngine(idx, nil)
eng = engine.NewBleveEngine(idx, bleve.LegacyBuilder{}, bleve.LegacyCompiler{})
Expect(err).ToNot(HaveOccurred())

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

Describe("New", func() {
It("returns a new index instance", func() {
b := engine.NewBleveEngine(idx, nil)
b := engine.NewBleveEngine(idx, nil, nil)
Expect(b).ToNot(BeNil())
})
})
Expand Down Expand Up @@ -364,8 +365,8 @@ var _ = Describe("Bleve", func() {
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(uint64(1)))

query := bleve.NewMatchQuery("child.pdf")
res, err := idx.Search(bleve.NewSearchRequest(query))
query := blevyV2.NewMatchQuery("child.pdf")
res, err := idx.Search(blevyV2.NewSearchRequest(query))
Expect(err).ToNot(HaveOccurred())
Expect(res.Hits.Len()).To(Equal(1))
})
Expand Down
181 changes: 0 additions & 181 deletions services/search/pkg/engine/kql_bleve_query.go

This file was deleted.

Loading

0 comments on commit 4b0ed10

Please sign in to comment.