Allow search on all the fields

This commit is contained in:
Las Zenow 2012-08-15 15:39:52 +02:00
parent 9f1204db35
commit 1006f4c5b9

View file

@ -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 {