Skip to content
Snippets Groups Projects
Commit 36a011b3 authored by Francé Wilke's avatar Francé Wilke
Browse files

Add function to escape OpenSearch search query string

parent a30cb2dc
Branches
Tags
No related merge requests found
......@@ -218,3 +218,25 @@ func RemoveUrlScheme(str string) string {
newStr = strings.Replace(str, "https://", "", 1)
return newStr
}
// EscapeOpenSearchSearchString See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
func EscapeOpenSearchSearchString(str string) string {
searchString := str
// Reserved characters
// NOTE: first char must be "\" to prevent replacing it again after replacing other chars with "\\"
reservedCharacters := []string{"\\", "+", "-", "=", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "/"}
// Remove "<" and ">"
strings.ReplaceAll(searchString, "<", "")
strings.ReplaceAll(searchString, ">", "")
// Escape the reserved characters with double backslashes ("\\")
for _, char := range reservedCharacters {
if strings.Contains(searchString, char) {
re := regexp.MustCompile(char)
searchString = re.ReplaceAllString(searchString, "\\"+char)
}
}
return searchString
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment