How to use full text search for unknown number of children of a field in Mongodb?

前提是你 提交于 2019-12-23 05:42:19

问题


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.


回答1:


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" } } )



回答2:


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"
     }
)


来源:https://stackoverflow.com/questions/23240961/how-to-use-full-text-search-for-unknown-number-of-children-of-a-field-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!