mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-08 13:19:58 -06:00
fix kql-bleve search. wildcards are excluded
This commit is contained in:
7
changelog/unreleased/fix-bleve-search.md
Normal file
7
changelog/unreleased/fix-bleve-search.md
Normal file
@@ -0,0 +1,7 @@
|
||||
Bugfix: Fix the kql-bleve search
|
||||
|
||||
We fixed the issue when 500 on searches that contain ":". Added the characters escaping according to https://blevesearch.com/docs/Query-String-Query/
|
||||
|
||||
|
||||
https://github.com/owncloud/ocis/pull/7290
|
||||
https://github.com/owncloud/ocis/issues/7282
|
||||
@@ -26,6 +26,34 @@ var _fields = map[string]string{
|
||||
"hidden": "Hidden",
|
||||
}
|
||||
|
||||
// The following quoted string enumerates the characters which may be escaped: "+-=&|><!(){}[]^\"~*?:\\/ "
|
||||
// based on bleve docs https://blevesearch.com/docs/Query-String-Query/
|
||||
// Wildcards * and ? are excluded
|
||||
var bleveEscaper = strings.NewReplacer(
|
||||
`+`, `\+`,
|
||||
`-`, `\-`,
|
||||
`=`, `\=`,
|
||||
`&`, `\&`,
|
||||
`|`, `\|`,
|
||||
`>`, `\>`,
|
||||
`<`, `\<`,
|
||||
`!`, `\!`,
|
||||
`(`, `\(`,
|
||||
`)`, `\)`,
|
||||
`{`, `\{`,
|
||||
`}`, `\}`,
|
||||
`{`, `\}`,
|
||||
`[`, `\[`,
|
||||
`]`, `\]`,
|
||||
`^`, `\^`,
|
||||
`"`, `\"`,
|
||||
`~`, `\~`,
|
||||
`:`, `\:`,
|
||||
`\`, `\\`,
|
||||
`/`, `\/`,
|
||||
` `, `\ `,
|
||||
)
|
||||
|
||||
// Compiler represents a KQL query search string to the bleve query formatter.
|
||||
type Compiler struct{}
|
||||
|
||||
@@ -58,7 +86,10 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
|
||||
switch n := nodes[i].(type) {
|
||||
case *ast.StringNode:
|
||||
k := getField(n.Key)
|
||||
v := strings.ReplaceAll(n.Value, " ", `\ `)
|
||||
v := n.Value
|
||||
if k != "ID" && k != "Size" {
|
||||
v = bleveEscaper.Replace(n.Value)
|
||||
}
|
||||
|
||||
if k != "Hidden" {
|
||||
v = strings.ToLower(v)
|
||||
|
||||
@@ -341,6 +341,18 @@ func Test_compile(t *testing.T) {
|
||||
}),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: `John Smith`,
|
||||
args: &ast.Ast{
|
||||
Nodes: []ast.Node{
|
||||
&ast.StringNode{Value: "John Smith +-=&|><!(){}[]^\"~: "},
|
||||
},
|
||||
},
|
||||
want: query.NewConjunctionQuery([]query.Query{
|
||||
query.NewQueryStringQuery(`Name:john\ smith\ \+\-\=\&\|\>\<\!\(\)\{\}\[\]\^\"\~\:\ `),
|
||||
}),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
assert := tAssert.New(t)
|
||||
@@ -357,3 +369,34 @@ func Test_compile(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_escape(t *testing.T) {
|
||||
type args struct {
|
||||
str string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "all escaped",
|
||||
args: args{
|
||||
`+-=&|><!(){}[]^"~:\/ `,
|
||||
},
|
||||
want: `\+\-\=\&\|\>\<\!\(\)\{\}\[\]\^\"\~\:\\\/\ `,
|
||||
},
|
||||
{
|
||||
name: "no one escaped",
|
||||
args: args{
|
||||
`@#$%`,
|
||||
},
|
||||
want: `@#$%`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tAssert.Equalf(t, tt.want, bleveEscaper.Replace(tt.args.str), "bleveEscaper(%v)", tt.args.str)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user