2021-10-13 09:30:23 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2024-07-09 10:06:15 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2021-10-13 09:30:23 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"golang.org/x/text/runes"
|
|
|
|
"golang.org/x/text/transform"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateQuerySearchString ...
|
|
|
|
func GenerateQuerySearchString(s string) bson.M {
|
|
|
|
return bson.M{
|
2024-07-09 10:06:15 +00:00
|
|
|
"$regex": primitive.Regex{Pattern: NonAccentVietnamese(s), Options: "i"},
|
2021-10-13 09:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NonAccentVietnamese ...
|
|
|
|
func NonAccentVietnamese(str string) string {
|
|
|
|
str = strings.ToLower(str)
|
|
|
|
str = replaceStringWithRegex(str, `đ`, "d")
|
|
|
|
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
|
|
|
|
result, _, _ := transform.String(t, str)
|
|
|
|
result = replaceStringWithRegex(result, `[^a-zA-Z0-9\s]`, "")
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// replaceStringWithRegex ...
|
|
|
|
func replaceStringWithRegex(src string, regex string, replaceText string) string {
|
|
|
|
reg := regexp.MustCompile(regex)
|
|
|
|
return reg.ReplaceAllString(src, replaceText)
|
|
|
|
}
|