Full text search

204 views
Skip to first unread message

Martin Angers

unread,
Jun 4, 2013, 3:25:31 PM6/4/13
to mgo-...@googlegroups.com
Hi,

Is there a way to use the [full text search feature][1] of Mongo 2.4? The `text` command must be run on a specific Collection, which doesn't seem possible with the current mgo API (Run() is only at the DB and Session level, if I've looked at up-to-date doc). Is there a workaround in the meantime?

Thanks,
Martin


Gustavo Niemeyer

unread,
Jun 4, 2013, 4:03:12 PM6/4/13
to mgo-...@googlegroups.com
Hi Martin,

On Tue, Jun 4, 2013 at 4:25 PM, Martin Angers <martin....@gmail.com> wrote:
> Is there a way to use the [full text search feature][1] of Mongo 2.4? The
> `text` command must be run on a specific Collection, which doesn't seem
> possible with the current mgo API (Run() is only at the DB and Session
> level, if I've looked at up-to-date doc). Is there a workaround in the
> meantime?

I was waiting for the text feature to stabilize, but we'll certainly
have nice high-level wrappers to use the feature.

That said, any command you run in the shell can also be run with mgo, today.

Note how the db.collection.runCommand is implemented:

> db.test.runCommand
function ( cmd , params ){
if ( typeof( cmd ) == "object" )
return this._db._dbCommand( cmd );
var c = {};
c[cmd] = this.getName();
if ( params )
Object.extend( c , params );
return this._db._dbCommand( c );
}

Let's run some of this logic:

> var cmd = "text";
> var params = {key: "value"};
> var c = {};
> c[cmd] = db.test.getName();
> Object.extend(c, params);

{ "text" : "test", "key" : "value" }

That document is the actual command that gets sent to the database,
and you can provide that to the db.Run method in mgo as well.

It might make sense to provide a convenience run helper in Collection too.


gustavo @ http://niemeyer.net

Martin Angers

unread,
Jun 4, 2013, 5:52:55 PM6/4/13
to mgo-...@googlegroups.com, mgo-...@googlegroups.com
Awesome, thanks Gustavo.

Sent from Mailbox for iPad


--
You received this message because you are subscribed to a topic in the Google Groups "mgo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mgo-users/goWMZe5BOAQ/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Kamoliddin Mavlonov

unread,
Mar 13, 2014, 9:19:32 AM3/13/14
to mgo-...@googlegroups.com
Hi Gustavo,

How to run db.ad.runCommand("text", { search: "hsjsjd"}) in mgo?

func GetSearch(w http.ResponseWriter, r *http.Request) {
session, err := mgo.Dial(conf.Mongodb)
if err != nil {
log.Fatal("Unable to connect to DB ", err)
}
defer session.Close()

session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
c := session.DB("sa").C("ad")
db := c.Database.With(session)
result := struct {
ErrMsg string
Ok     bool
}{}
        err = db.Run(bson.M{"text": c, "search": "hsjsjd"}, &result)

if err != nil {
w.Write(json.Message("ERROR", "Results not found"))
} else {
w.Write(json.Message("OK", result))
}
}


Thanks,
Kamol

Gustavo Niemeyer

unread,
Mar 13, 2014, 2:13:02 PM3/13/14
to mgo-...@googlegroups.com
On Thu, Mar 13, 2014 at 10:19 AM, Kamoliddin Mavlonov
<kamo...@gmail.com> wrote:
> Hi Gustavo,
>
> How to run db.ad.runCommand("text", { search: "hsjsjd"}) in mgo?
(...)
> err = db.Run(bson.M{"text": c, "search": "hsjsjd"}, &result)

That's about right. The problem you're facing is likely ordering. From
the documentation at [1]:

"""
Note that MongoDB considers the first marshalled key as the command
name, so when providing a command with options, it's important to use
an ordering-preserving document, such as a struct value or an instance
of bson.D. For instance:

err = db.Run(bson.D{{"create", "mycollection"}, {"size", 1024}})
"""

So something along these lines would do:

err = db.Run(bson.D{{"text", c}, {"search", "hsjsjd"}}, &result)


[1] http://labix.org/v2/mgo#Database.Run


gustavo @ http://niemeyer.net

Kamoliddin Mavlonov

unread,
Mar 13, 2014, 10:03:42 PM3/13/14
to mgo-...@googlegroups.com
Thank you Gustavo, you are right, it works now like charm! 

One more, question. Sorry for that.
How to run this MongoDB request:
db.ad.runCommand("text", { search: "hsjsjd", limit: 20, project: { "price" : 1, "image1": 1 }})
through Mgo?

Regards,
Kamol

Kamoliddin Mavlonov

unread,
Mar 14, 2014, 10:53:45 AM3/14/14
to mgo-...@googlegroups.com
Ok, I have figured it out from Gustavo comments and Mgo doc.

func GetSearch(w http.ResponseWriter, r *http.Request) {
session, err := mgo.Dial(conf.Mongodb)
if err != nil {
log.Fatal("Unable to connect to DB ", err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
db := session.DB("sa")
var result interface{}
//db.ad.runCommand("text", { search: "hsjsjd", limit: 20, project: { "price" : 1, "image1": 1 }})
q := bson.D{
{"text", "ad"},
{"search", "hsjsjd"},
{"limit", 2},
{"project",
bson.D{
{"price", 1},
{"image1", 1},
},
},
}
err = db.Run(q, &result)
log.Printf("err = %s\n", err)
if err != nil {
w.Write(json.Message("ERROR", "Ads not found"))
} else {
w.Write(json.Message("OK", result))
}
}


Reply all
Reply to author
Forward
0 new messages