用户使用Golang mgo进行搜索。

huangapple go评论113阅读模式
英文:

User search with Golang mgo

问题

获取一个字符串作为输入(来自用户搜索),我正在尝试构建一个bson.M对象,用于mgo在Mongo数据库中搜索并找到x个项目。

类似这样的代码:

  1. func Search(w http.ResponseWriter, r *http.Request) {
  2. q := r.FormValue("q")
  3. filter := bson.M{}
  4. // 在这里进行一些操作
  5. // 进行数据库连接
  6. things := []*thing{}
  7. err := db.Find(&filter).Limit(10).All(&things)
  8. // 检查错误,发送things等等
  9. }

我需要搜索基于以下条件:

  • 忽略大小写(我找到了这个答案,它帮助了我一部分)
  • 存储的数据中的title必须在某个地方包含q中的每个单词。

所以,例如,如果存储的数据看起来像{title: "abcde"},那么

  • Abc会匹配
  • de Bc会匹配
  • ac不会匹配

编辑:解决方案

我最终找到了解决方法。魔法部分如下:

  1. q := r.FormValue("q")
  2. qs := strings.Split(q, " ")
  3. and := make([]bson.M, len(qs))
  4. for i, q := range qs {
  5. and[i] = bson.M{"title": bson.M{
  6. "$regex": bson.RegEx{Pattern: ".*" + q + ".*", Options: "i"},
  7. }}
  8. }
  9. filter := bson.M{"$and": and}

以上是翻译好的内容,请确认是否满意。

英文:

Getting a string as input (from a user search), I am trying to construct a bson.M object for mgo to search through the mongo database and find x number of items.

Something like this

<!-- language: lang-golang -->

  1. func Search (w http.ResponseWriter, r *http.Request) {
  2. q := r.FormValue(&quot;q&quot;)
  3. filter := bson.M{}
  4. // magic happens here
  5. // do db connection stuff
  6. things := []*thing{}
  7. err := db.Find(&amp;filter).Limit(10).All(&amp;things)
  8. // check error, send things, etc
  9. }

What I need the search to be based on is

  • ignore case (I found this answer which takes me part of the way)
  • the title in the stored data must, somewhere, include each of the words in the q.

So for instance if the stored data looks like {title: &quot;abcde&quot;}, then

  • Abc will match
  • de Bc will match
  • ac will not match

Edit: Solution

I finally figured it out. The magic part looks like this:

  1. q := r.FormValue(&quot;q&quot;)
  2. qs := strings.Split(q, &quot; &quot;)
  3. and := make([]bson.M, len(qs))
  4. for i, q := range qs {
  5. and[i] = bson.M{&quot;title&quot;: bson.M{
  6. &quot;$regex&quot;: bson.RegEx{Pattern: &quot;.*&quot; + q + &quot;.*&quot;, Options: &quot;i&quot;},
  7. }}
  8. }
  9. filter := bson.M{&quot;$and&quot;: and}

答案1

得分: 1

mongo过滤器可以使用正则表达式,例如:

  1. bson.M{"title": bson.M{"$regex": bson.RegEx{Pattern: title, Options: "i"}}}

所以在这种情况下,title变量可能是类似于.*abc*.的内容。Options: "i"表示不区分大小写。

至于匹配子字符串(场景2),我不确定如何在正则表达式中实现。

英文:

The mongo filter can take regex, for example;

  1. bson.M{&quot;title&quot;: bson.M{&quot;$regex&quot;: bson.RegEx{Pattern: title, Options: &quot;i&quot;}}}

So in this case the title variable would be something like; .*abc*.. The Options: "i" enables case insensitivity.

As for matching the substring (scenario 2) I am not sure how to achieve within regex.

答案2

得分: 0

简单使用如下:

  1. wordOffset := q
  2. selector := bson.M{"title": bson.M{"$regex": wordOffset}}
英文:

Simple use this,

  1. wordOffset := q
  2. selector:= bson.M{&quot;title&quot;: bson.M{&quot;$regex&quot;: wordOffset}}

huangapple
  • 本文由 发表于 2017年3月14日 02:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/42771398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定