Add search string query #1

Merged
luuvansinh merged 2 commits from add-search-string-query into master 2021-10-13 09:42:05 +00:00
1 changed files with 37 additions and 0 deletions
Showing only changes of commit 48c904d272 - Show all commits

37
search_string.go Normal file
View File

@ -0,0 +1,37 @@
package mongodb
import (
"regexp"
"strings"
"unicode"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/x/bsonx"
"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{
"$regex": bsonx.Regex(NonAccentVietnamese(s), "i"),
}
}
// 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)
}