mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 09:18:59 +00:00
c269886389
This PR makes the target search more user-friendly by stripping symbols that have a special interpretation in MySQL FTS. Closes #2017
31 lines
635 B
Go
31 lines
635 B
Go
package mysql
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var mysqlFTSSymbolRegexp = regexp.MustCompile("[-+]+")
|
|
|
|
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 strings.TrimSpace(
|
|
mysqlFTSSymbolRegexp.ReplaceAllLiteralString(query, " "),
|
|
) + "*"
|
|
}
|