Unescape value for prefix query

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2020-07-31 23:10:36 +02:00
parent e7082d3647
commit 8ebb5ce0ab
2 changed files with 12 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
Bugfix: Unescape value for prefix query
Prefix queries also need to unescape token values like `'some ''ol string'` to `some 'ol string` before using it in a prefix query
https://github.com/owncloud/ocis-accounts/pull/76

View File

@@ -35,8 +35,11 @@ func recursiveBuildQuery(n *godata.ParseNode) (query.Query, error) {
}
if n.Children[1].Token.Type != godata.FilterTokenString {
return nil, errors.New("startswith expected a string as the second param")
}
q := bleve.NewPrefixQuery(n.Children[1].Token.Value)
} // remove enclosing ' of string tokens (looks like 'some ol'' string')
value := n.Children[1].Token.Value[1 : len(n.Children[1].Token.Value)-1]
// unescape '' as '
unescaped := strings.ReplaceAll(value, "''", "'")
q := bleve.NewPrefixQuery(unescaped)
q.SetField(n.Children[0].Token.Value)
return q, nil
// TODO contains as regex?
@@ -59,7 +62,7 @@ func recursiveBuildQuery(n *godata.ParseNode) (query.Query, error) {
// remove enclosing ' of string tokens (looks like 'some ol'' string')
value := n.Children[1].Token.Value[1 : len(n.Children[1].Token.Value)-1]
// unescape '' as '
unascaped := strings.ReplaceAll(value, "''", "'")
unescaped := strings.ReplaceAll(value, "''", "'")
// use a match query, so the field mapping, e.g. lowercase is applied to the value
// remember we defined the field mapping for `preferred_name` to be lowercase
// a term query like `preferred_name eq 'Artur'` would use `Artur` to search in the index and come up empty
@@ -68,7 +71,7 @@ func recursiveBuildQuery(n *godata.ParseNode) (query.Query, error) {
// - LDAP matching rules depend on the attribute: see https://ldapwiki.com/wiki/MatchingRule
// - odata has functions like `startswith`, `contains`, `tolower`, `toupper`, `matchesPattern` andy more: see http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_BuiltinQueryFunctions
// - ocis-glauth should do the mapping between LDAP and odata filter
q := bleve.NewMatchQuery(unascaped)
q := bleve.NewMatchQuery(unescaped)
q.SetField(n.Children[0].Token.Value)
return q, nil
} else if n.Children[1].Token.Type == godata.FilterTokenInteger {