fix kql-bleve search. wildcards are excluded

This commit is contained in:
Roman Perekhod
2023-09-15 13:52:55 +02:00
parent 161e4d3555
commit 911ea1c0f6
3 changed files with 82 additions and 1 deletions

View 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

View File

@@ -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)

View File

@@ -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)
})
}
}