Embedded document text search

47 views
Skip to first unread message

Wei Kuan Chua

unread,
Jul 7, 2014, 11:28:04 AM7/7/14
to mongod...@googlegroups.com
Hi,

I'm using the text search functionality in Mongodb on a document that contains several fields as well as an embedded document. I tried to index all the fields but it seems like fields in the embedded document is not indexed. Does mongodb support text indexing on embedded document as well?

Thanks!

William Berkeley

unread,
Jul 8, 2014, 1:46:43 PM7/8/14
to mongod...@googlegroups.com
Yes, MongoDB does support text indexing on multiple fields including embedded document fields. See below for an example worked in the mongo shell.

> db.test.findOne()
{
 
"_id" : ObjectId("53bc2c6bd519a03e56d6906d"),
 
"title" : "Fancy Sneakers",
 
"desc" : "Really nice sneakers that are fashionable and will make other people jealous",
 
"details" : {
 
"detail" : "white with blue stripes and orange laces"
 
}
}

> db.test.ensureIndex({"title":"text", "desc":"text", "details.detail":"text"})
{
 
"createdCollectionAutomatically" : false,
 
"numIndexesBefore" : 1,
 
"numIndexesAfter" : 2,
 
"ok" : 1
}

> db.test.find({$text:{$search:"white"}}).pretty()
{
 
"_id" : ObjectId("53bc2c6bd519a03e56d6906d"),
 
"title" : "Fancy Sneakers",
 
"desc" : "Really nice sneakers that are fashionable and will make other people jealous",
 
"details" : {
 
"detail" : "white with blue stripes and orange laces"
 
}
}


I also was successful with the same steps but specifying an all field text index, i.e. replace the ensureIndex step with

> db.test.ensureIndex({"$**":"text"})
{
 
"createdCollectionAutomatically" : false,
 
"numIndexesBefore" : 1,
 
"numIndexesAfter" : 2,
 
"ok" : 1
}

-Will
Reply all
Reply to author
Forward
0 new messages