0

I have a document with one field description like this:

{
  "_id": "item0",
  "description": {
    "parlist": [
      {
        "listitem": {
          "text": {
            "child": "page rous lady",
            "keyword": "officer e"
          }
        }
      },
      {
        "listitem": {
          "text": "shepherd noble "
        }
      }
    ]
  }
}

How to create text index on description and search for specific word? I don't know how depth can description go and how many children will description have. I tried with index creation like this:

db.collection.ensureIndex({description:"text"})

and then for query like this:

db.collection.runCommand("text",{$search:"shepherd"})

But it doesn't work.

Prakash Thapa
  • 1,285
  • 14
  • 27

2 Answers2

0

You can just simply build an text index for all text field:

db.collection.ensureIndex({ "$**": "text" })

Then search with keyword $text:

db.collection.find( { $text: { $search: "shepherd" } } )
yaoxing
  • 4,003
  • 2
  • 21
  • 30
  • 1
    There are many other fields also, but I would like to search only from `description` field. if I use `db.collection.ensureIndex({ "$**": "text" })` index, it will return result even it content in other field rather than `description`. – Prakash Thapa Apr 23 '14 at 10:24
  • Did some search and unfortunately can't find a way to do that... people in this post end up using the way above. http://stackoverflow.com/questions/19239298/create-text-index-on-sub-document-field-wildcard – yaoxing Apr 23 '14 at 10:40
0

Text-indexes do not work on full sub-documents, but you can create a text index which includes more than one field:

db.collection.ensureIndex(
     {
         "description.parlist.listitem.text": "text",
         "description.parlist.listitem.text.child": "text",
         "description.parlist.listitem.text.keyword": "text"
     }
)
Philipp
  • 67,764
  • 9
  • 118
  • 153