mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
2479d58262
* Bug 4852: When searching for Labels, breakdown query strings containing '@' in multiple search terms to avoid issues with MySQL FTS.
40 lines
1005 B
Go
40 lines
1005 B
Go
package mysql
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var mysqlFTSSymbolRegexp = regexp.MustCompile("[-+@]+")
|
|
|
|
// queryMinLength returns true if the query argument is longer than a "short" word.
|
|
// What defines a "short" word is MySQL's "ft_min_word_len" VARIABLE, generally set
|
|
// to 4 by default in Fleet deployments.
|
|
//
|
|
// TODO(lucas): Remove this method on #2627.
|
|
func queryMinLength(query string) bool {
|
|
return countLongestTerm(query) >= 3
|
|
}
|
|
|
|
func countLongestTerm(query string) int {
|
|
max := 0
|
|
for _, q := range strings.Split(query, " ") {
|
|
if len(q) > max {
|
|
max = len(q)
|
|
}
|
|
}
|
|
return max
|
|
}
|
|
|
|
// transformQuery replaces occurrences of characters that are treated specially
|
|
// by the MySQL FTS engine to try to make the search more user-friendly
|
|
func transformQuery(query string) string {
|
|
return transformQueryWithSuffix(query, "*")
|
|
}
|
|
|
|
func transformQueryWithSuffix(query, suffix string) string {
|
|
return strings.TrimSpace(
|
|
mysqlFTSSymbolRegexp.ReplaceAllLiteralString(query, " "),
|
|
) + suffix
|
|
}
|