Allow search on all the fields
This commit is contained in:
parent
9f1204db35
commit
1006f4c5b9
1 changed files with 13 additions and 5 deletions
18
search.go
18
search.go
|
@ -13,13 +13,21 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildQuery(q string) bson.M {
|
func buildQuery(q string) bson.M {
|
||||||
|
var reg []bson.RegEx
|
||||||
|
query := bson.M{}
|
||||||
words := strings.Split(q, " ")
|
words := strings.Split(q, " ")
|
||||||
reg := make([]bson.RegEx, len(words))
|
for _, w := range words {
|
||||||
for i, w := range words {
|
tag := strings.SplitN(w, ":", 2)
|
||||||
reg[i].Pattern = w
|
if len(tag) > 1 {
|
||||||
reg[i].Options = "i"
|
query[tag[0]] = bson.RegEx{tag[1], "i"}
|
||||||
|
} else {
|
||||||
|
reg = append(reg, bson.RegEx{w, "i"})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bson.M{"keywords": bson.M{"$all": reg}}
|
if len(reg) > 0 {
|
||||||
|
query["keywords"] = bson.M{"$all": reg}
|
||||||
|
}
|
||||||
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
type searchData struct {
|
type searchData struct {
|
||||||
|
|
Reference in a new issue